Redsandro
Redsandro

Reputation: 11356

How to deal with connections and query queues in node.js node-mongodb?

It is unclear to me how to work that connection and query the collection. Because sometimes the connection is already there and there is no problem. But when the connection needs to be established, collection queries aren't queued. You cannot make a new connection all the time either, because you can (and should) make only one. Seems like you need the collection query in a connection callback, but also stand-alone when the connection is already there.

Ofcourse, this isn't undoable, but with all the blah and yaddah overhead, I imagine I am not doing it right. After all, this seems kinda syntactic saccharin to me. Where is the syntactic sugar?

Here's a rudimentary example. Imagine a bunch of requests in the same second:

var util        = require('util'),
    mongo       = require('mongodb'),
    Server      = mongo.Server,
    Db          = mongo.Db,
    mServer     = new Server('localhost', 27017),
    mDb         = new Db('SomeDatabase', mServer),
    db;

if (mDb._state == 'disconnected') {
    util.debug('MONGODB: connecting...');
    mDb.open(function(err, newDb) {
        if (!err) {
            db = newDb;
            // I cannot immediately work on a collection here, 
            // because it takes a moment for the db to connect.
        }
    });
}
else if (mDb._state == 'connecting') {
    // I need to catch this state because I cannot work on collections yet, 
    // but I cannot connect either because it's already connecting.
}
// else if (mDb._state == 'connected') {

// Queries need to be performed after connection is established 
// or when connection was already established by a previous request.
db.createCollection(collection, function(err, collection) {
    if (!err) {
        collection.insert(record, {safe:true}, function(err, result) {
            util.debug('MONGODB: Inserted.');
        });
    }
});

// }

Now, the beginner guides I found are all about the callback on mDb.open, but this gives problems on successive requests when the node is already running. It refuses to open another connection. So, what is actually the correct way to play nice with mongodb in nodejs when a single launch callback isn't enough?

I imagine a module that you pass queries in callback-form, which passes the callbacks around for you, either (1) to where it waits for the connection-callback, (2) directly to the collection, or (3) waiting for the connection, but too late for the connection-callback. But like I said, this is pure syntactic saccharin.

Upvotes: 0

Views: 1281

Answers (1)

freakish
freakish

Reputation: 56467

You misunderstand something. First you create the connection to DB:

var mDb = new Db('SomeDatabase', mServer);

and you hold the reference to this connection. Whenever you want to load/save something from/to DB, you use the same variable. You don't recreate it on every request. Initialization should be done even before creating the web server itself ( create the server on open callback ). Here's a pseudo code:

var webserver = INITIALIZE_NEW_SERVER( );
var mDB = new Db('SomeDatabase', mServer);

// set request handler
WHEN_REQUEST ---> {
    mDB.createCollection( ... );
    // whatever
}

mDb.open(function(error, newDb) {
    // handle errors
    webserver.listen( );
});

Upvotes: 1

Related Questions