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