Created
December 11, 2019 17:21
-
-
Save abhi11210646/ce64147ad3736dfd999ab2ffbe860688 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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