Node.js & Typescript Singleton pattern : seems like singleton is not shared between singleton users

百般思念 提交于 2019-12-11 15:11:31

问题


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 :

DataBase ( main class ) :

  • which is a singleton ( see export let DB = DataBase.Instance; below )
  • 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.

ModelParametersDataBase ( one of the other classes ) :

  • this class imports the main class ( see import { DB } from './DataBase' ; below )
  • gets the connection status through the main class.

Here is my issue : My issue is that even though the connection is established once and for all (DB.dataBaseStatus = true is called), the ModelParametersDataBase gets false when calling the singleton's getter.

Here is an snippet from the DataBase.ts :

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(() => {
        DB.dataBaseStatus = true ;

        debug('CONNECTED TO DATABASE ');
      },
      err => {
        DB.dataBaseStatus = false ;
        debug('ERROR - CANNOT CONNECT TO DATABASE');
      }
      );
  }
}

/** Instantiate singleton */
export let DB = DataBase.Instance;

Ane here is how I use this singleton in the ModelParametersDataBase class :

import { DB } from './DataBase' ;

export class ModelParametersDataBase {

  public static getModelParameters(): Promise<IModelParameters> {
    return new Promise((resolve, reject) => {

      if (DB.dataBaseStatus) {
        // do stuff 
      } else {
        reject(Error(CANNOT_REACH_DB));
      }
    });
  }

}

Can you please point out where my mistake is ? Thanks.


回答1:


You are trying to instantiate class with this keyword from static context. Why not to do something more like this:

class DataBase {
    private static instance = new DataBase();
    public static getInstance = () => DataBase.instance;
}

export default DataBase;

And then use it like this:

import DataBase from './DataBase';
Database.getInstance();

Or even you could export single instance from file:

class DataBase {};
export default new DataBase();

EDIT: In addition you should use this keyword when setting instance variables like databaseStatus.



来源:https://stackoverflow.com/questions/47617538/node-js-typescript-singleton-pattern-seems-like-singleton-is-not-shared-betw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!