0

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.

Randhir Singh
  • 245
  • 1
  • 7
  • 19
  • Check connection create factory and ensure that newly created instances are different objects. I think that ```DataAccess``` don't create new instance, it only override existent or create new if ```null```. In other words: ensure that connections are separate objects – dorintufar Aug 17 '18 at 07:35
  • I tried to loop the global variable , and on each index 'global.mongoIntances[index].instance.connections[0].name' it shows the same name for database. Looks like it is replacing the previous connections as well when creating new connection. Why is that happening ? – Randhir Singh Aug 17 '18 at 07:46
  • This happeninh because you change and push the same connection ref on global variable instead of creating new connection. In fact, that's the same object that point to same memory address – dorintufar Aug 17 '18 at 07:49
  • I have to use the same object this.mongooseInstance because in all my models, I am accessing the mongo db using 'mongooseInstance' class object. – Randhir Singh Aug 17 '18 at 07:56
  • Yep, right it must be same object but different instances, otherwise it will point to same database url – dorintufar Aug 17 '18 at 08:07
  • Can you please provide me a simple example, how should I do it. Because I spent this whole week to figure out this problem. – Randhir Singh Aug 17 '18 at 08:12
  • Try to replace ```self.mongooseInstance = Mongoose.connect(url);``` with ```self.mongooseInstance = Mongoose.createConnection(url);``` – dorintufar Aug 17 '18 at 08:18
  • Thanks, it is not replacing the connection instance now. But looks like I need to change queries now. https://stackoverflow.com/questions/22786374/queries-hang-when-using-mongoose-createconnection-vs-mongoose-connect – Randhir Singh Aug 17 '18 at 08:41

0 Answers0