2

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

1 Answer 1

1

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.

2
  • Thanks for your proposal. I thought about such a pattern, however, I don't really know how to ensure, that the db1-variable is already initialized when I first use it in my app code. This may well happen before the callback stored the connection into the variables. Do I have to wrap an if(db1 == undefined) test around each access to the database connections?
    – opt12
    Sep 13, 2015 at 15:20
  • I admit the solution I proposed here is kind of a 'good enough' solution. for a small app while learning the basics this approach should be ok. You're right that there is the potential that the db hasn't connected before you hit your route but to avoid things like this you would have to get into things like dependency injection to ensure the objects are initialized. Sep 13, 2015 at 15:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.