lib/manager.js
function Manager (uri, opts, fn) {
  if (!uri) {
    throw Error('No connection URI provided.')
  }

  if (!(this instanceof Manager)) {
    return new Manager(uri, opts, fn)
  }

  if (typeof opts === 'function') {
    fn = opts
    opts = {}
  }

  opts = opts || {}

  this._collectionOptions = objectAssign({}, DEFAULT_OPTIONS, opts.collectionOptions || {})
  delete opts.collectionOptions

  if (Array.isArray(uri)) {
    if (!opts.database) {
      for (var i = 0, l = uri.length; i < l; i++) {
        if (!opts.database) {
          opts.database = uri[i].replace(/([^\/])+\/?/, '') // eslint-disable-line
        }
        uri[i] = uri[i].replace(/\/.*/, '')
      }
    }
    uri = uri.join(',') + '/' + opts.database
    monkDebug('repl set connection "%j" to database "%s"', uri, opts.database)
  }

  if (typeof uri === 'string') {
    if (!/^mongodb:\/\//.test(uri)) {
      uri = 'mongodb://' + uri
    }
  }

  this._state = STATE.OPENING

  this._queue = []
  this.on('open', function (db) {
    monkDebug('connection opened')
    monkDebug('emptying queries queue (%s to go)', this._queue.length)
    this._queue.forEach(function (cb) {
      cb(db)
    })
  }.bind(this))

  this._connectionURI = uri
  this._connectionOptions = opts

  this.open(uri, opts, fn && function (err) {
    fn(err, this)
  }.bind(this))

  this.helper = {
    id: ObjectId
  }

  this.collections = {}

  this.open = this.open.bind(this)
  this.close = this.close.bind(this)
  this.executeWhenOpened = this.executeWhenOpened.bind(this)
  this.collection = this.col = this.get = this.get.bind(this)
  this.oid = this.id
  this.setDefaultCollectionOptions = this.setDefaultCollectionOptions.bind(this)
  this.addMiddleware = this.addMiddleware.bind(this)
}
function Manager (uri, opts, fn) {
  if (!uri) {
    throw Error('No connection URI provided.')
  }

  if (!(this instanceof Manager)) {
    return new Manager(uri, opts, fn)
  }

  if (typeof opts === 'function') {
    fn = opts
    opts = {}
  }

  opts = opts || {}

  this._collectionOptions = objectAssign({}, DEFAULT_OPTIONS, opts.collectionOptions || {})
  delete opts.collectionOptions

  if (Array.isArray(uri)) {
    if (!opts.database) {
      for (var i = 0, l = uri.length; i < l; i++) {
        if (!opts.database) {
          opts.database = uri[i].replace(/([^\/])+\/?/, '') // eslint-disable-line
        }
        uri[i] = uri[i].replace(/\/.*/, '')
      }
    }
    uri = uri.join(',') + '/' + opts.database
    monkDebug('repl set connection "%j" to database "%s"', uri, opts.database)
  }

  if (typeof uri === 'string') {
    if (!/^mongodb:\/\//.test(uri)) {
      uri = 'mongodb://' + uri
    }
  }

  this._state = STATE.OPENING

  this._queue = []
  this.on('open', function (db) {
    monkDebug('connection opened')
    monkDebug('emptying queries queue (%s to go)', this._queue.length)
    this._queue.forEach(function (cb) {
      cb(db)
    })
  }.bind(this))

  this._connectionURI = uri
  this._connectionOptions = opts

  this.open(uri, opts, fn && function (err) {
    fn(err, this)
  }.bind(this))

  this.helper = {
    id: ObjectId
  }

  this.collections = {}

  this.open = this.open.bind(this)
  this.close = this.close.bind(this)
  this.executeWhenOpened = this.executeWhenOpened.bind(this)
  this.collection = this.col = this.get = this.get.bind(this)
  this.oid = this.id
  this.setDefaultCollectionOptions = this.setDefaultCollectionOptions.bind(this)
  this.addMiddleware = this.addMiddleware.bind(this)
}
function Manager ( uri , opts , fn ) { if ( ! uri ) { throw Error ( 'No connection URI provided.' ) } if ( ! ( this instanceof Manager ) ) { return new Manager ( uri , opts , fn ) } if ( typeof opts === 'function' ) { fn = opts opts = { } } opts = opts || { } this . _collectionOptions = objectAssign ( { } , DEFAULT_OPTIONS , opts . collectionOptions || { } ) delete opts . collectionOptions if ( Array . isArray ( uri ) ) { if ( ! opts . database ) { for ( var i = 0 , l = uri . length ; i < l ; i ++ ) { if ( ! opts . database ) { opts . database = uri [ i ] . replace ( / ([^\/])+\/? / , '' ) // eslint-disable-line } uri [ i ] = uri [ i ] . replace ( / \/.* / , '' ) } } uri = uri . join ( ',' ) + '/' + opts . database monkDebug ( 'repl set connection "%j" to database "%s"' , uri , opts . database ) } if ( typeof uri === 'string' ) { if ( ! / ^mongodb:\/\/ / . test ( uri ) ) { uri = 'mongodb://' + uri } } this . _state = STATE . OPENING this . _queue = [ ] this . on ( 'open' , function ( db ) { monkDebug ( 'connection opened' ) monkDebug ( 'emptying queries queue (%s to go)' , this . _queue . length ) this . _queue . forEach ( function ( cb ) { cb ( db ) } ) } . bind ( this ) ) this . _connectionURI = uri this . _connectionOptions = opts this . open ( uri , opts , fn && function ( err ) { fn ( err , this ) } . bind ( this ) ) this . helper = { id : ObjectId } this . collections = { } this . open = this . open . bind ( this ) this . close = this . close . bind ( this ) this . executeWhenOpened = this . executeWhenOpened . bind ( this ) this . collection = this . col = this . get = this . get . bind ( this ) this . oid = this . id this . setDefaultCollectionOptions = this . setDefaultCollectionOptions . bind ( this ) this . addMiddleware = this . addMiddleware . bind ( this ) }
Monk constructor.

@param {Array|String} uri replica sets can be an array or
comma-separated
@param {Object|Function} opts or connect callback
@param {Function} fn connect callback
@return {Promise} resolve when the connection is opened
Monk constructor .