PageRenderTime 15ms CodeModel.GetById 8ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 166 lines | 86 code | 27 blank | 53 comment | 14 complexity | 5629d854f8149effaa45b12f9fd41c51 MD5 | raw file
  1/*!
  2 * Module dependencies.
  3 */
  4
  5var MongooseConnection = require('../../connection');
  6var STATES = require('../../connectionstate');
  7
  8/**
  9 * A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) connection implementation.
 10 *
 11 * @inherits Connection
 12 * @api private
 13 */
 14
 15function NativeConnection() {
 16  MongooseConnection.apply(this, arguments);
 17  this._listening = false;
 18}
 19
 20/**
 21 * Expose the possible connection states.
 22 * @api public
 23 */
 24
 25NativeConnection.STATES = STATES;
 26
 27/*!
 28 * Inherits from Connection.
 29 */
 30
 31NativeConnection.prototype.__proto__ = MongooseConnection.prototype;
 32
 33/**
 34 * Switches to a different database using the same connection pool.
 35 *
 36 * Returns a new connection object, with the new db.
 37 *
 38 * @param {String} name The database name
 39 * @return {Connection} New Connection Object
 40 * @api public
 41 */
 42
 43NativeConnection.prototype.useDb = function(name, options) {
 44  // Return immediately if cached
 45  if (options && options.useCache && this.relatedDbs[name]) return this.relatedDbs[name];
 46
 47  // we have to manually copy all of the attributes...
 48  var newConn = new this.constructor();
 49  newConn.name = name;
 50  newConn.base = this.base;
 51  newConn.collections = {};
 52  newConn.models = {};
 53  newConn.replica = this.replica;
 54  newConn.name = this.name;
 55  newConn.options = this.options;
 56  newConn._readyState = this._readyState;
 57  newConn._closeCalled = this._closeCalled;
 58  newConn._hasOpened = this._hasOpened;
 59  newConn._listening = false;
 60
 61  // First, when we create another db object, we are not guaranteed to have a
 62  // db object to work with. So, in the case where we have a db object and it
 63  // is connected, we can just proceed with setting everything up. However, if
 64  // we do not have a db or the state is not connected, then we need to wait on
 65  // the 'open' event of the connection before doing the rest of the setup
 66  // the 'connected' event is the first time we'll have access to the db object
 67
 68  var _this = this;
 69
 70  newConn.client = _this.client;
 71
 72  if (this.db && this._readyState === STATES.connected) {
 73    wireup();
 74  } else {
 75    this.once('connected', wireup);
 76  }
 77
 78  function wireup() {
 79    newConn.client = _this.client;
 80    newConn.db = _this.client.db(name);
 81    newConn.onOpen();
 82    // setup the events appropriately
 83    listen(newConn);
 84  }
 85
 86  newConn.name = name;
 87
 88  // push onto the otherDbs stack, this is used when state changes
 89  this.otherDbs.push(newConn);
 90  newConn.otherDbs.push(this);
 91
 92  // push onto the relatedDbs cache, this is used when state changes
 93  if (options && options.useCache) {
 94    this.relatedDbs[newConn.name] = newConn;
 95    newConn.relatedDbs = this.relatedDbs;
 96  }
 97
 98  return newConn;
 99};
100
101/*!
102 * Register listeners for important events and bubble appropriately.
103 */
104
105function listen(conn) {
106  if (conn.db._listening) {
107    return;
108  }
109  conn.db._listening = true;
110
111  conn.db.on('close', function(force) {
112    if (conn._closeCalled) return;
113
114    // the driver never emits an `open` event. auto_reconnect still
115    // emits a `close` event but since we never get another
116    // `open` we can't emit close
117    if (conn.db.serverConfig.autoReconnect) {
118      conn.readyState = STATES.disconnected;
119      conn.emit('close');
120      return;
121    }
122    conn.onClose(force);
123  });
124  conn.db.on('error', function(err) {
125    conn.emit('error', err);
126  });
127  conn.db.on('reconnect', function() {
128    conn.readyState = STATES.connected;
129    conn.emit('reconnect');
130    conn.emit('reconnected');
131    conn.onOpen();
132  });
133  conn.db.on('timeout', function(err) {
134    conn.emit('timeout', err);
135  });
136  conn.db.on('open', function(err, db) {
137    if (STATES.disconnected === conn.readyState && db && db.databaseName) {
138      conn.readyState = STATES.connected;
139      conn.emit('reconnect');
140      conn.emit('reconnected');
141    }
142  });
143  conn.db.on('parseError', function(err) {
144    conn.emit('parseError', err);
145  });
146}
147
148/**
149 * Closes the connection
150 *
151 * @param {Boolean} [force]
152 * @param {Function} [fn]
153 * @return {Connection} this
154 * @api private
155 */
156
157NativeConnection.prototype.doClose = function(force, fn) {
158  this.client.close(force, fn);
159  return this;
160};
161
162/*!
163 * Module exports.
164 */
165
166module.exports = NativeConnection;