I am using Node.Js 8.6 along with Mongoose 4.11 and have multiple database connections. Db connections are established via mongoose.createConnection.
I found out that mongoose object has connections property (array), where I can see established connections. My question is, what is the proper way, to switch between connections when creating db models in separate modules.
index.js
async function db1() {
await mongoose.createConnection(
process.env.MONGODB_URI_1,
{ useMongoClient: true }
);
}
async function db2() {
await mongoose.createConnection(
process.env.MONGODB_URI_2,
{ useMongoClient: true }
);
}
model.js
//connect to db1
const Test1 = mongoose.model('Test1', new mongoose.Schema({ name: String }));
//connect to db2
const Test2 = mongoose.model('Test2', new mongoose.Schema({ name: String }));