src/sal/MongoDBProvider.js
/**
   * @summary **Provides file-like operations on MongoDB database**
   * @description
   * A MongoDBProvider is plugged into a StorageEngine when running under
   * Node.js to provide access to collections in a MongoDB database.
   * These very limited operations use mongo much like a file system,
   * although objects in collections must contain `name` and `owner` fields
   * for indexing.
   * @constructor
   * @param {StorageEngine} engine context for this engine
   */
@summary **Provides file-like operations on MongoDB database
@description
A MongoDBProvider is plugged into a StorageEngine when running under
Node.js to provide access to collections in a MongoDB database.
These very limited operations use mongo much like a file system,
although objects in collections must contain `name` and `owner` fields
for indexing.
@constructor
@param {StorageEngine} engine context for this engine
@summary ** Provides file - like operations on MongoDB database @description A MongoDBProvider is plugged into a StorageEngine when running under Node . js to provide access to collections in a MongoDB database . These very limited operations use mongo much like a file system although objects in collections must contain ` name ` and ` owner ` fields for indexing . @constructor @param { StorageEngine } engine context for this engine
function MongoDBProvider(engine, url) {
    var self = this;
    self.engine = engine;
    self.dbOpen = false; 
    self.operationTimeout = 60000; 
    self.url = url;
    self.opTimer = new EggTimer(self.operationTimeout);
    self.connectDB = function connectDB() {
      self.opTimer.onDing(self.close.bind(self));
      self.opTimer.reset();
      if (self.dbOpen || self.dbOpenP) {
        return self.dbOpenP;
      }
      mm.log.debug('- Connecting to MongoDB database: ', url);
      var mongodb = mm.check(require('mongodb'));
      var MongoClient = mongodb.MongoClient;
      var dbOpenDeferred = qq.defer(); 
      self.dbOpenP = dbOpenDeferred.promise;
      MongoClient.connect(url, function (err, db) {
        if (err) {
          if (err.message === 'connect ECONNREFUSED') {
            mm.log.warn('MongoDB: Not available');
          }
          else {
            mm.log.error('MongoDB: Connection failure:', err);
          }
          dbOpenDeferred.reject(err);
          return;
        }
        mm.log.debug('- Connection established to:', url);
        mm.log.status('- Database opened: ', url);
        self.dbOpen = true;
        self.db = db;
        self.opTimer.reset();
        dbOpenDeferred.resolve(db);
      });
      return dbOpenDeferred.promise;
    }
  }
function MongoDBProvider ( engine , url ) { var self = this ; self . engine = engine ; self . dbOpen = false ; self . operationTimeout = 60000 ; self . url = url ; self . opTimer = new EggTimer ( self . operationTimeout ) ; self . connectDB = function connectDB ( ) { self . opTimer . onDing ( self . close . bind ( self ) ) ; self . opTimer . reset ( ) ; if ( self . dbOpen || self . dbOpenP ) { return self . dbOpenP ; } mm . log . debug ( '- Connecting to MongoDB database: ' , url ) ; var mongodb = mm . check ( require ( 'mongodb' ) ) ; var MongoClient = mongodb . MongoClient ; var dbOpenDeferred = qq . defer ( ) ; self . dbOpenP = dbOpenDeferred . promise ; MongoClient . connect ( url , function ( err , db ) { if ( err ) { if ( err . message === 'connect ECONNREFUSED' ) { mm . log . warn ( 'MongoDB: Not available' ) ; } else { mm . log . error ( 'MongoDB: Connection failure:' , err ) ; } dbOpenDeferred . reject ( err ) ; return ; } mm . log . debug ( '- Connection established to:' , url ) ; mm . log . status ( '- Database opened: ' , url ) ; self . dbOpen = true ; self . db = db ; self . opTimer . reset ( ) ; dbOpenDeferred . resolve ( db ) ; } ) ; return dbOpenDeferred . promise ; } }
@summary **Provides file-like operations on MongoDB database
 @description
 A MongoDBProvider is plugged into a StorageEngine when running under
 Node.js to provide access to collections in a MongoDB database.
@summary ** Provides file - like operations on MongoDB database @description A MongoDBProvider is plugged into a StorageEngine when running under Node . js to provide access to collections in a MongoDB database .
// Database starts out closed. // Time to keep DB open. // The database is open or being opened, return it to the caller. //mm.log.status('- Connecting to MongoDB database: ', url); // Add a promise and resolver. // Use connect method to connect to the Server /* istanbul ignore if */ // Tested independently and often.