Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

connection.js 4.9KB

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