This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -6,6 +6,8 @@
const MongooseConnection = require('../../connection');
const STATES = require('../../connectionstate');
const immediate = require('../../helpers/immediate');
const setTimeout = require('../../helpers/timers').setTimeout;
/**
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) connection implementation.
@@ -38,16 +40,20 @@ NativeConnection.prototype.__proto__ = MongooseConnection.prototype;
* Returns a new connection object, with the new db. If you set the `useCache`
* option, `useDb()` will cache connections by `name`.
*
* **Note:** Calling `close()` on a `useDb()` connection will close the base connection as well.
*
* @param {String} name The database name
* @param {Object} [options]
* @param {Boolean} [options.useCache=false] If true, cache results so calling `useDb()` multiple times with the same name only creates 1 connection object.
* @param {Boolean} [options.noListener=false] If true, the new connection object won't listen to any events on the base connection. This is better for memory usage in cases where you're calling `useDb()` for every request.
* @return {Connection} New Connection Object
* @api public
*/
NativeConnection.prototype.useDb = function(name, options) {
// Return immediately if cached
if (options && options.useCache && this.relatedDbs[name]) {
options = options || {};
if (options.useCache && this.relatedDbs[name]) {
return this.relatedDbs[name];
}
@@ -58,7 +64,7 @@ NativeConnection.prototype.useDb = function(name, options) {
newConn.collections = {};
newConn.models = {};
newConn.replica = this.replica;
newConn.config = Object.assign({}, this.base.connection.config, newConn.config);
newConn.config = Object.assign({}, this.config, newConn.config);
newConn.name = this.name;
newConn.options = this.options;
newConn._readyState = this._readyState;
@@ -90,16 +96,24 @@ NativeConnection.prototype.useDb = function(name, options) {
function wireup() {
newConn.client = _this.client;
newConn.db = _this.client.db(name);
const _opts = {};
if (options.hasOwnProperty('noListener')) {
_opts.noListener = options.noListener;
}
newConn.db = _this.client.db(name, _opts);
newConn.onOpen();
// setup the events appropriately
listen(newConn);
if (options.noListener !== true) {
listen(newConn);
}
}
newConn.name = name;
// push onto the otherDbs stack, this is used when state changes
this.otherDbs.push(newConn);
if (options.noListener !== true) {
this.otherDbs.push(newConn);
}
newConn.otherDbs.push(this);
// push onto the relatedDbs cache, this is used when state changes
@@ -116,13 +130,16 @@ NativeConnection.prototype.useDb = function(name, options) {
*/
function listen(conn) {
if (conn.db._listening) {
if (conn._listening) {
return;
}
conn.db._listening = true;
conn._listening = true;
conn.db.on('close', function(force) {
if (conn._closeCalled) return;
conn.client.on('close', function(force) {
if (conn._closeCalled) {
return;
}
conn._closeCalled = conn.client._closeCalled;
// the driver never emits an `open` event. auto_reconnect still
// emits a `close` event but since we never get another
@@ -134,26 +151,30 @@ function listen(conn) {
}
conn.onClose(force);
});
conn.db.on('error', function(err) {
conn.client.on('error', function(err) {
conn.emit('error', err);
});
conn.db.on('reconnect', function() {
conn.readyState = STATES.connected;
conn.emit('reconnect');
conn.emit('reconnected');
conn.onOpen();
});
conn.db.on('timeout', function(err) {
conn.emit('timeout', err);
});
conn.db.on('open', function(err, db) {
if (STATES.disconnected === conn.readyState && db && db.databaseName) {
if (!conn.client.s.options.useUnifiedTopology) {
conn.db.on('reconnect', function() {
conn.readyState = STATES.connected;
conn.emit('reconnect');
conn.emit('reconnected');
}
conn.onOpen();
});
conn.db.on('open', function(err, db) {
if (STATES.disconnected === conn.readyState && db && db.databaseName) {
conn.readyState = STATES.connected;
conn.emit('reconnect');
conn.emit('reconnected');
}
});
}
conn.client.on('timeout', function(err) {
conn.emit('timeout', err);
});
conn.db.on('parseError', function(err) {
conn.client.on('parseError', function(err) {
conn.emit('parseError', err);
});
}
@@ -168,6 +189,11 @@ function listen(conn) {
*/
NativeConnection.prototype.doClose = function(force, fn) {
if (this.client == null) {
immediate(() => fn());
return this;
}
this.client.close(force, (err, res) => {
// Defer because the driver will wait at least 1ms before finishing closing
// the pool, see https://github.com/mongodb-js/mongodb-core/blob/a8f8e4ce41936babc3b9112bf42d609779f03b39/lib/connection/pool.js#L1026-L1030.