Skip to content

Instantly share code, notes, and snippets.

@abhi11210646
Created December 11, 2019 17:21
Show Gist options
  • Save abhi11210646/ce64147ad3736dfd999ab2ffbe860688 to your computer and use it in GitHub Desktop.
Save abhi11210646/ce64147ad3736dfd999ab2ffbe860688 to your computer and use it in GitHub Desktop.
class customDB {
constructor() {
this.models = {};
this.readConnection = null;
// {model:collectionName} // this needed as collection names are not same as Model
this.model_list = {
'User':'users'
};
}
async createConnection(app, config) {
try {
require('./db.primary')(config.dbConfig); //load default database connection
this.readConnection = await require('./db.secondary')(config.dbConfig); // Create new connection
Object.keys(this.model_list).map(collect => {
this.models[collect] = this.readConnection.model(collect, this.model_list[collect]);
});
} catch (err) {
console.log('connection error =>', err);
}
}
getConnection() {
return this.readConnection;
}
getModels() {
return this.models;
}
}
module.exports = new customDB();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment