The smallest layer around node-mongodb-native

I wrote what is possibly the smallest wrapper around node-mongodb-native wrapper. But, I feel that it needs improving. It's so small it fits here comfortably:

function MongoWrapper() {
  this.db = null;
};

var mongoWrapper;
module.exports = exports = mongoWrapper = new MongoWrapper;

// This means that you can do `new include('mongoWrapper').MongoWrapper()`
mongoWrapper.MongoWrapper = MongoWrapper;

// ObjectId is the most handy method of all. This will work with
// native BSON or Pure BSON
mongoWrapper.ObjectId = function() {
    if (!mongo.BSONNative || !mongo.BSONNative.ObjectID) {
      return function(id) {
            return mongo.BSONPure.ObjectID.createFromHexString(id);
      };
    }
    return function(id) {
            return new mongo.BSONNative.ObjectID(id);
    };
}();

MongoWrapper.prototype.connect = function(url, options, cb ){

  var that = this;
  var MongoClient = mongo.MongoClient;

  MongoClient.connect( url, function( err, db ){

    if( err ) {
      console.log( err );
    } else {
      that.db = db; 
    }
    cb( err, db );
  });
}

Now... The "problem" with this is that I need to wrap my whole server in a callback:

mw.connect('mongodb://localhost/hotplate', {}, function( err, db ){
    app.configure(function(){
    app.set('port', process.env.PORT || 3000);
    app.set('views', __dirname + '/views');
    ...
    app.use(express.session({
      // secret: settings.cookie_secret,
      secret: 'woodchucks are nasty animals',
      store: new MongoStore({
        // db: settings.db
        // db: hotplate.get('db').client
         db: db
      })
    }));

Other drivers (like Mongoose, or even mongojs) manage not to force to use the callback. I looked at their code, and... well, I didn't quite get it. Mongojs in particular seems to use a library for promises, but I am having trouble understanding it. Note that express.session for example wants, as a parameter, a fully working connection (which is what I do here). Without using the connection, you cannot actually be sure that the connection will be set.

So: what's the easiest way to get rid of the need for a callback?

The basic idea, I suppose, would be to "clone" the mongodb API calls, wrapping them with code to handle the possibility that the "db" variable is not set. But... how would that work?

Any help would be greatly appreciated!

Merc.

Eventually, you'll hit the situation where you absolutely need to wait for the connection to complete before continuing as it is async. And without a callback, it won't work (as the MongoClient requires a callback).

You could use an Event to wrap it -- but that's just a different type of callback really (conceptually). That's what Mongoose does -- it raises an event when the connection is ready, open.

Using Node.js, there isn't a solution that doesn't involve either an event or callback, somewhere (that's an intentional design choice of Node and the MongoDB driver). It's an async connection in the driver. You just need to delay some of the express setup until after the connection is opened. It only needs to happen at app startup.

Realize this question is a little old, but I use this tiny little wrapper to do the "lifting" and for a small amount of sugar so my db code is a little less verbose. Things like findById without having to wrap the ObjectId, and findArray without having to toArray() a query. Check it out:

https://github.com/dmcaulay/mongo-wrapper