opt12
opt12

Reputation: 320

How to open and maintain connections to multiple Mongo databases correctly in Node.js?

I'm completely new to Node.js and the Mongo db. However, I'd like to start a first project to get acquainted to all that stuff.

The server app will be express based and I figured out to use three distinct mongo databases for appDdata, sessionData and userData. Now I have a really hard time to figure out how to connect to these databases and how to maintain the connections and pass around access objects.

In most of the express/mongo tutorials, there are toy examples doing it like this:

MongoClient.connect('mongodb://localhost:27017/blog', function(err, db) {
"use strict";
if(err) throw err;

// Register our templating engine
app.engine('html', cons.swig);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');

// Express middleware to populate 'req.cookies' so we can access cookies
app.use(express.cookieParser());

// Express middleware to populate 'req.body' so we can access POST variables
app.use(express.bodyParser());

// Application routes
routes(app, db);

app.listen(8082);
console.log('Express server listening on port 8082');
});

That menas, I connect to the database and all my app lives in the callback of that connection call. Besides I really don't kike the idea of my whole app living in the callback, it get's worse when I have multiple database connections: The app will live in the callback of a callback of a callback... (And this will lead to a real indentation nightmare.)

My first idea was to place the connection calls and then export the db objects I receive in the callback to some (module-)globals.

But how to handle the situation, when the connection is not yet established or died?

All in all, I'm still lacking a good idea of how to connect the databases and reuse these connections later in my app logic.

I'm sure this is not a very unusual situation, so please, please help me out and point me to some advice how to handle this.

Big Big Thanx!

Felix

Upvotes: 2

Views: 4294

Answers (1)

ThrowsException
ThrowsException

Reputation: 2636

Don't store your entire application logic in the connect callback. Instead assign the db object to something that your entire application can see

var db1;
var db2;
var db3;

MongoClient.connect('mongodb://localhost:27017/blog', function(err, db) {
   db1= db
});
MongoClient.connect('mongodb://otherdb:27017/', function(err, db) {
   db2= db
});
MongoClient.connect('mongodb://someotherdb:27017/', function(err, db) {
   db3= db
});
// application logic
// Register our templating engine
app.engine('html', cons.swig);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');

// Express middleware to populate 'req.cookies' so we can access cookies
app.use(express.cookieParser());

// Express middleware to populate 'req.body' so we can access POST variables
app.use(express.bodyParser());

// Application routes
routes(app, db1);

Although I'm not sure why you would want three separate databases like this for the kind of data you're storing. Unless there was some really compelling reason to split them up I feel like they could all easily live as separate collections in the same database to cut down on on complexity.

Upvotes: 1

Related Questions