Hi,
Looking to move away from Mongoose to using the native driver directly in NodeJS.
We currently have a per database authentication - so each database has a different user / password - in Mongoose we connect each model using a different connection string - where the username and password is passed - along with the database.
Looking at the native driver example - the MongoClient is created once per replica set - so a cached client - but I am unsure then how to connect each database using a different username and password.
Are there any examples handy?
My current code for a test local instance of mongo looks like this:
const { MongoClient } = require("mongodb");
let tmMapsDB = null;
let staffDB = null;
module.exports = async () => {
// Create a new MongoClient
MongoClient.connect('mongodb://localhost:27017', function (err, client) {
if (err) throw err;
tmMapsDB = client.db('tmmaps')
staffDB = client.db('staff')
});
}
module.exports.returnDB = (name) => {
if (name === 'TMMAPS') {
return tmMapsDB
}
if (name === 'STAFF') {
return staffDB
}
return null
}```