0

please help me to get out of it. with login api i want to set mongoose connection string. is it possible that with login api i set connection string and it works for other api also(which will call after login api)?? like first user login at that time i set db and for further api call works for that db? i had tried How to connect multiple mongodb database dynamically using mongoose? but in this solution in each api they specifies requested param. Can we set connection string of mongo by api call?

this is my app.js

 var mongoose = require('mongoose');
    var connections = [test = mongoose.createConnection('mongodb://localhost:27017/test'), 
demo = mongoose.createConnection('mongodb://localhost:27017/demo')];

    exports.getDatabaseConnection = function (dbName) {

      if (connections[dbName]) {
        //database connection already exist. Return connection object
        return connections['dbName'];
      } else {
        connections[dbName] = mongoose.createConnection('mongodb://localhost:27017/' + dbName);
        return connections['dbName'];
      }
    }

and this is my user.js

exports.authenticate = function (req, res) {
 var db = app.getDatabaseConnection(req.body.keydb);
 var userDetail = db.model('userdetail', UserSchema); 

  userDetail.findOne({
    email: req.body.email,
    userType: req.body.userType
  }, function (err, user) {
    if (err) throw err;
    if (!user) {
      res.send({ status: 400, message: req.i18n.__("user.authIncorrectEmail") });
    } else {...}
});
}

when i am requesting keydb value as demo..then it should select demo string which is in connection array. but it is not working.what i am doing wrong??

1 Answer 1

0

i am trying to connect mongoDb as per user login request comes. i want to set db once only not on each api call. if user logout then db should be disconnect and when again user login it should again connect.

You need to keep a list of database connections, one per session. One way to do it would be to have an object where the keys are the session IDs and the values are the database connections. Alternatively you can keep one connection per user ID if you want to allow a situation where the same user has multiple sessions and they each share the database connection. Now when you create a session on user login you also create a new database connection. When you destroy a session on user logout you have to close the connection and remove it from the object where it was stored. For all other endpoints except login and logout you just look up the connection and use it.

This is how you can achieve your goal but keep in mind that keeping a separate database connection per logged in user has many disadvantages and wastes a lot of resources, both in your application and in the database.

Can we set connection string of mongo by api call?

Yes, of course. But this will mean that people will be able to trick your app into e.g. breaking into other people's databases and you will be responsible for all of the legal consequences of that.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your response. @rsp please consider my updated question. now could you suggest me something for this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.