lib/db.js
function Db(databaseName, topology, options) {
  options = options || {};
  if (!(this instanceof Db)) return new Db(databaseName, topology, options);
  EventEmitter.call(this);

  // Get the promiseLibrary
  const promiseLibrary = options.promiseLibrary || Promise;

  // Filter the options
  options = filterOptions(options, legalOptionNames);

  // Ensure we put the promiseLib in the options
  options.promiseLibrary = promiseLibrary;

  // Internal state of the db object
  this.s = {
    // Database name
    databaseName: databaseName,
    // DbCache
    dbCache: {},
    // Children db's
    children: [],
    // Topology
    topology: topology,
    // Options
    options: options,
    // Logger instance
    logger: Logger('Db', options),
    // Get the bson parser
    bson: topology ? topology.bson : null,
    // Unpack read preference
    readPreference: options.readPreference,
    // Set buffermaxEntries
    bufferMaxEntries: typeof options.bufferMaxEntries === 'number' ? options.bufferMaxEntries : -1,
    // Parent db (if chained)
    parentDb: options.parentDb || null,
    // Set up the primary key factory or fallback to ObjectID
    pkFactory: options.pkFactory || ObjectID,
    // Get native parser
    nativeParser: options.nativeParser || options.native_parser,
    // Promise library
    promiseLibrary: promiseLibrary,
    // No listener
    noListener: typeof options.noListener === 'boolean' ? options.noListener : false,
    // ReadConcern
    readConcern: options.readConcern
  };

  // Ensure we have a valid db name
  validateDatabaseName(this.s.databaseName);

  // Add a read Only property
  getSingleProperty(this, 'serverConfig', this.s.topology);
  getSingleProperty(this, 'bufferMaxEntries', this.s.bufferMaxEntries);
  getSingleProperty(this, 'databaseName', this.s.databaseName);

  // This is a child db, do not register any listeners
  if (options.parentDb) return;
  if (this.s.noListener) return;

  // Add listeners
  topology.on('error', createListener(this, 'error', this));
  topology.on('timeout', createListener(this, 'timeout', this));
  topology.on('close', createListener(this, 'close', this));
  topology.on('parseError', createListener(this, 'parseError', this));
  topology.once('open', createListener(this, 'open', this));
  topology.once('fullsetup', createListener(this, 'fullsetup', this));
  topology.once('all', createListener(this, 'all', this));
  topology.on('reconnect', createListener(this, 'reconnect', this));
}
function Db(databaseName, topology, options) {
  options = options || {};
  if (!(this instanceof Db)) return new Db(databaseName, topology, options);
  EventEmitter.call(this);

  // Get the promiseLibrary
  const promiseLibrary = options.promiseLibrary || Promise;

  // Filter the options
  options = filterOptions(options, legalOptionNames);

  // Ensure we put the promiseLib in the options
  options.promiseLibrary = promiseLibrary;

  // Internal state of the db object
  this.s = {
    // Database name
    databaseName: databaseName,
    // DbCache
    dbCache: {},
    // Children db's
    children: [],
    // Topology
    topology: topology,
    // Options
    options: options,
    // Logger instance
    logger: Logger('Db', options),
    // Get the bson parser
    bson: topology ? topology.bson : null,
    // Unpack read preference
    readPreference: options.readPreference,
    // Set buffermaxEntries
    bufferMaxEntries: typeof options.bufferMaxEntries === 'number' ? options.bufferMaxEntries : -1,
    // Parent db (if chained)
    parentDb: options.parentDb || null,
    // Set up the primary key factory or fallback to ObjectID
    pkFactory: options.pkFactory || ObjectID,
    // Get native parser
    nativeParser: options.nativeParser || options.native_parser,
    // Promise library
    promiseLibrary: promiseLibrary,
    // No listener
    noListener: typeof options.noListener === 'boolean' ? options.noListener : false,
    // ReadConcern
    readConcern: options.readConcern
  };

  // Ensure we have a valid db name
  validateDatabaseName(this.s.databaseName);

  // Add a read Only property
  getSingleProperty(this, 'serverConfig', this.s.topology);
  getSingleProperty(this, 'bufferMaxEntries', this.s.bufferMaxEntries);
  getSingleProperty(this, 'databaseName', this.s.databaseName);

  // This is a child db, do not register any listeners
  if (options.parentDb) return;
  if (this.s.noListener) return;

  // Add listeners
  topology.on('error', createListener(this, 'error', this));
  topology.on('timeout', createListener(this, 'timeout', this));
  topology.on('close', createListener(this, 'close', this));
  topology.on('parseError', createListener(this, 'parseError', this));
  topology.once('open', createListener(this, 'open', this));
  topology.once('fullsetup', createListener(this, 'fullsetup', this));
  topology.once('all', createListener(this, 'all', this));
  topology.on('reconnect', createListener(this, 'reconnect', this));
}
function Db ( databaseName , topology , options ) { options = options || { } ; if ( ! ( this instanceof Db ) ) return new Db ( databaseName , topology , options ) ; EventEmitter . call ( this ) ; // Get the promiseLibrary const promiseLibrary = options . promiseLibrary || Promise ; // Filter the options options = filterOptions ( options , legalOptionNames ) ; // Ensure we put the promiseLib in the options options . promiseLibrary = promiseLibrary ; // Internal state of the db object this . s = { // Database name databaseName : databaseName , // DbCache dbCache : { } , // Children db's children : [ ] , // Topology topology : topology , // Options options : options , // Logger instance logger : Logger ( 'Db' , options ) , // Get the bson parser bson : topology ? topology . bson : null , // Unpack read preference readPreference : options . readPreference , // Set buffermaxEntries bufferMaxEntries : typeof options . bufferMaxEntries === 'number' ? options . bufferMaxEntries : - 1 , // Parent db (if chained) parentDb : options . parentDb || null , // Set up the primary key factory or fallback to ObjectID pkFactory : options . pkFactory || ObjectID , // Get native parser nativeParser : options . nativeParser || options . native_parser , // Promise library promiseLibrary : promiseLibrary , // No listener noListener : typeof options . noListener === 'boolean' ? options . noListener : false , // ReadConcern readConcern : options . readConcern } ; // Ensure we have a valid db name validateDatabaseName ( this . s . databaseName ) ; // Add a read Only property getSingleProperty ( this , 'serverConfig' , this . s . topology ) ; getSingleProperty ( this , 'bufferMaxEntries' , this . s . bufferMaxEntries ) ; getSingleProperty ( this , 'databaseName' , this . s . databaseName ) ; // This is a child db, do not register any listeners if ( options . parentDb ) return ; if ( this . s . noListener ) return ; // Add listeners topology . on ( 'error' , createListener ( this , 'error' , this ) ) ; topology . on ( 'timeout' , createListener ( this , 'timeout' , this ) ) ; topology . on ( 'close' , createListener ( this , 'close' , this ) ) ; topology . on ( 'parseError' , createListener ( this , 'parseError' , this ) ) ; topology . once ( 'open' , createListener ( this , 'open' , this ) ) ; topology . once ( 'fullsetup' , createListener ( this , 'fullsetup' , this ) ) ; topology . once ( 'all' , createListener ( this , 'all' , this ) ) ; topology . on ( 'reconnect' , createListener ( this , 'reconnect' , this ) ) ; }
Creates a new Db instance
@class
@param {string} databaseName The name of the database this instance represents.
@param {(Server|ReplSet|Mongos)} topology The server topology for the database.
@param {object} [options] Optional settings.
@param {string} [options.authSource] If the database authentication is dependent on another databaseName.
@param {(number|string)} [options.w] The write concern.
@param {number} [options.wtimeout] The write concern timeout.
@param {boolean} [options.j=false] Specify a journal write concern.
@param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
@param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
@param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
@param {boolean} [options.raw=false] Return document results as raw BSON buffers.
@param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
@param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
@param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
@param {number} [options.bufferMaxEntries=-1] Sets a cap on how many operations the driver will buffer up before giving up on getting a working connection, default is -1 which is unlimited.
@param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
@param {object} [options.pkFactory] A primary key factory object for generation of custom _id keys.
@param {object} [options.promiseLibrary] A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
@param {object} [options.readConcern] Specify a read concern for the collection. (only MongoDB 3.2 or higher supported)
@param {ReadConcernLevel} [options.readConcern.level='local'] Specify a read concern level for the collection operations (only MongoDB 3.2 or higher supported)
@property {(Server|ReplSet|Mongos)} serverConfig Get the current db topology.
@property {number} bufferMaxEntries Current bufferMaxEntries value for the database
@property {string} databaseName The name of the database this instance represents.
@property {object} options The options associated with the db instance.
@property {boolean} native_parser The current value of the parameter native_parser.
@property {boolean} slaveOk The current slaveOk value for the db instance.
@property {object} writeConcern The current write concern values.
@property {object} topology Access the topology object (single server, replicaset or mongos).
@fires Db#close
@fires Db#reconnect
@fires Db#error
@fires Db#timeout
@fires Db#parseError
@fires Db#fullsetup
@return {Db} a Db instance.
Creates a new Db instance