title: Node.js & Typescript Singleton pattern : seems like singleton is not shared between singleton users
body: <p>I am using Typescript (2.5.3) in a backend Node.js application, and trying to figure out where my coding mistake is, in the following code. Briefly, I have one main class, and several other classes that use this main class :</p>
  
  <p><strong>DataBase ( main class ) :</strong></p>
  
  <ul>
  <li>which is a singleton ( see <code>export let DB = DataBase.Instance;</code> below )</li>
  <li>whose role is simply to connect to a Mongo database, and set a flag if connection successful or not.  This flag will be used by other classes to know whether connection was established.</li>
  </ul>
  
  <p><strong>ModelParametersDataBase ( one of the other classes ) :</strong></p>
  
  <ul>
  <li>this class imports the main class ( see <code>import { DB } from './DataBase' ;</code> below )</li>
  <li>gets the connection status through the main class.</li>
  </ul>
  
  <p><strong>Here is my issue :</strong>
   My issue is that even though the connection <em>is</em> established once and for all (<code>DB.dataBaseStatus = true</code> is called), the ModelParametersDataBase gets <code>false</code> when calling the singleton's getter.</p>
  
  <p>Here is an snippet from the <code>DataBase.ts</code>  :</p>
  
  <pre><code>class DataBase {
  
    private static _instance: DataBase = undefined;
  
    private dataBaseStatus_  = false ;
  
  
    public static get Instance() {
      return this._instance || (this._instance = new this());
    }
    public get dataBaseStatus() {
      return this.dataBaseStatus_ ;
    }
  
    public set dataBaseStatus(status :boolean) {
      this.dataBaseStatus_ = status ;
    }
  
    public configDatabase() {
  
      /* Connect to Mongo DB */
      mongoose.connect(MONGODB_CONNECTION_ROOT + MONGODB_DB_NAME, options)
        .then(() =&gt; {
          DB.dataBaseStatus = true ;
  
          debug('CONNECTED TO DATABASE ');
        },
        err =&gt; {
          DB.dataBaseStatus = false ;
          debug('ERROR - CANNOT CONNECT TO DATABASE');
        }
        );
    }
  }
  
  /** Instantiate singleton */
  export let DB = DataBase.Instance;
  </code></pre>
  
  <p>Ane here is how I use this singleton in the <code>ModelParametersDataBase</code> class :</p>
  
  <pre><code>import { DB } from './DataBase' ;
  
  export class ModelParametersDataBase {
  
    public static getModelParameters(): Promise&lt;IModelParameters&gt; {
      return new Promise((resolve, reject) =&gt; {
  
        if (DB.dataBaseStatus) {
          // do stuff 
        } else {
          reject(Error(CANNOT_REACH_DB));
        }
      });
    }
  
  }
  </code></pre>
  
  <p>Can you please point out where my mistake is ? Thanks.</p>