I have an application where each user has it's own sub domain and different mongo database for each sub domain. I am trying to connect to particular database based on the referrer sub domain, First I am checking which is the sub domain from where the request came and then I am creating dynamic mongo connection string.
let Url = 'mongodb://DB_USER:DB_PASS@DB_HOST:DB_PORT/DYNAMIC_DB_NAME';
DataAccess.setConnectionString(req, Url, domain);
After that I am saving the connection instance to global variables, I have declared a global array to save this information. In server.js:
global.mongoInstances = [];
Below is my code which checks first If the connection is already made then serve the mongoose connection instance from global array, but if the request came first time from that sub domain then create a new connection and push the instance to global array.
static setConnectionString(req, url, domain){
this.mongooseInstance = '';
let self = this;
if(domain && domain !== '') {
var inArray = false;
for (var k in global.mongoIntances) {
if (global.mongoIntances.hasOwnProperty(k)) {
if( global.mongoIntances[k].domain === domain) {
console.log('domain is aready in the array ', global.mongoIntances[k].domain, ' === request domain ', domain);
self.mongooseInstance = '';
console.log('from session to global.instance db name', global.mongoIntances[k].connections[0].name);
self.mongooseInstance = global.mongoIntances[k].instance;
inArray = true;
console.log('typeof ', global.mongoIntances[k]);
console.log('after swithching the db, new db name is ', self.mongooseInstance.connections[0].name);
return self.mongooseInstance;
}
}
}
if(!inArray) {
Mongoose.connection.close(function () {
if (global.mongoIntances.length > 0) {
console.log('before push db name is', util.inspect(global.mongoIntances[0].instance.connections[0].name, {depth: 3}));
}
self.mongooseInstance = Mongoose.connect(url);
console.log('connection url is ', url, ' and connected db is ', self.mongooseInstance.connections[0].name);
global.mongoIntances.push({domain: domain, instance: self.mongooseInstance});
if (global.mongoIntances.length > 0) {
console.log('after push db name is from array ', util.inspect(global.mongoIntances[0].instance.connections[0].name, {depth: 3}) , ' db name after push from self.mongooseInstance ', self.mongooseInstance.connections[0].name);
}
for (var k in global.mongoIntances) {
if (global.mongoIntances.hasOwnProperty(k)) {
console.log('loop global array ', global.mongoIntances[k].instance.connections[0].name , ' domian name ', global.mongoIntances[k].domain);
}
}
return self.mongooseInstance;
});
}
}
}
But the issue is, every time the request came from any sub domain, it is replacing the already added instances to global array with the latest connection instance. Like if request came from demo1 sub domain the the array value will be:
{domain: demo1, instance: demo1}
If the request came from demo2 sub domain the array value will be:
{domain: demo1, instance: demo2}
{domain: demo2, instance: demo2}
Every time it is replacing all connection instance with latest connection instance, I have tried many things in past couple of days, but I am not sure what is wrong here, Please let know if any one know the answer.