storages/mongodb.js
function MongoStorage(config) {
  
   var self = this;
   
   this.events = new EventEmitter();
   
   config = protos.extend({
     host: 'localhost',
     port: 27017,
     database: 'store',
     collection: 'keyvalue',
     username: '',
     password: '',
     safe: true
   }, config);
   
   if (typeof config.port != 'number') config.port = parseInt(config.port, 10);
   
   this.config = config;
   
   this.className = this.constructor.name;
   
   app.debug(util.format('Initializing MongoStorage for %s@%s:%s', config.username, config.host, config.port));
   
   var reportError = function(err) {
     app.log(util.format("MongoStore [%s:%s] %s", config.host, config.port, err.code));
     self.client = err;
   }
   
   // Set db
   self.db = new Db(config.database, new Server(config.host, config.port, {}), {safe: config.safe});

   // Add async task
   app.addReadyTask();

   // Get client
   self.db.open(function(err, client) {
     
     if (err) {
       reportError(err);
     } else {
       // Set client
       self.client = client;

       // Get collection
       client.collection(config.collection, function(err, collection) {

         // Set collection
         self.collection = collection;
         
         // Authenticate
         if (config.username && config.password) {

           self.db.authenticate(config.username, config.password, function(err, success) {
             
             if (err) {
               
               app.log(util.format('MongoStorage: unable to authenticate %s@%s', config.username, config.host));
               throw err;
               
             } else {
               
                // Emit initialization event
                self.events.emit('init', self.db, self.client);

             }
           });

         } else {
           
           // Emit initialization event
           self.events.emit('init', self.db, self.client);
           
         }

       });

     }
   });
   
   // Flush async task
   this.events.on('init', function() {
     app.flushReadyTask();
   });
   
   // Set enumerable properties
   protos.util.onlySetEnumerable(this, ['className', 'db']);
}
function MongoStorage(config) {
  
   var self = this;
   
   this.events = new EventEmitter();
   
   config = protos.extend({
     host: 'localhost',
     port: 27017,
     database: 'store',
     collection: 'keyvalue',
     username: '',
     password: '',
     safe: true
   }, config);
   
   if (typeof config.port != 'number') config.port = parseInt(config.port, 10);
   
   this.config = config;
   
   this.className = this.constructor.name;
   
   app.debug(util.format('Initializing MongoStorage for %s@%s:%s', config.username, config.host, config.port));
   
   var reportError = function(err) {
     app.log(util.format("MongoStore [%s:%s] %s", config.host, config.port, err.code));
     self.client = err;
   }
   
   // Set db
   self.db = new Db(config.database, new Server(config.host, config.port, {}), {safe: config.safe});

   // Add async task
   app.addReadyTask();

   // Get client
   self.db.open(function(err, client) {
     
     if (err) {
       reportError(err);
     } else {
       // Set client
       self.client = client;

       // Get collection
       client.collection(config.collection, function(err, collection) {

         // Set collection
         self.collection = collection;
         
         // Authenticate
         if (config.username && config.password) {

           self.db.authenticate(config.username, config.password, function(err, success) {
             
             if (err) {
               
               app.log(util.format('MongoStorage: unable to authenticate %s@%s', config.username, config.host));
               throw err;
               
             } else {
               
                // Emit initialization event
                self.events.emit('init', self.db, self.client);

             }
           });

         } else {
           
           // Emit initialization event
           self.events.emit('init', self.db, self.client);
           
         }

       });

     }
   });
   
   // Flush async task
   this.events.on('init', function() {
     app.flushReadyTask();
   });
   
   // Set enumerable properties
   protos.util.onlySetEnumerable(this, ['className', 'db']);
}
function MongoStorage ( config ) { var self = this ; this . events = new EventEmitter ( ) ; config = protos . extend ( { host : 'localhost' , port : 27017 , database : 'store' , collection : 'keyvalue' , username : '' , password : '' , safe : true } , config ) ; if ( typeof config . port != 'number' ) config . port = parseInt ( config . port , 10 ) ; this . config = config ; this . className = this . constructor . name ; app . debug ( util . format ( 'Initializing MongoStorage for %s@%s:%s' , config . username , config . host , config . port ) ) ; var reportError = function ( err ) { app . log ( util . format ( "MongoStore [%s:%s] %s" , config . host , config . port , err . code ) ) ; self . client = err ; } // Set db self . db = new Db ( config . database , new Server ( config . host , config . port , { } ) , { safe : config . safe } ) ; // Add async task app . addReadyTask ( ) ; // Get client self . db . open ( function ( err , client ) { if ( err ) { reportError ( err ) ; } else { // Set client self . client = client ; // Get collection client . collection ( config . collection , function ( err , collection ) { // Set collection self . collection = collection ; // Authenticate if ( config . username && config . password ) { self . db . authenticate ( config . username , config . password , function ( err , success ) { if ( err ) { app . log ( util . format ( 'MongoStorage: unable to authenticate %s@%s' , config . username , config . host ) ) ; throw err ; } else { // Emit initialization event self . events . emit ( 'init' , self . db , self . client ) ; } } ) ; } else { // Emit initialization event self . events . emit ( 'init' , self . db , self . client ) ; } } ) ; } } ) ; // Flush async task this . events . on ( 'init' , function ( ) { app . flushReadyTask ( ) ; } ) ; // Set enumerable properties protos . util . onlySetEnumerable ( this , [ 'className' , 'db' ] ) ; }
MongoDB Storage class

@class MongoStorage
@extends Storage
@constructor
@param {object} app Application instance
@param {object} config Storage configuration
MongoDB Storage class