From a3e4c880ae2f6dc5fb1ce1b3ef1b0d8bd5e969a3 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 22 Sep 2025 11:15:34 -0400 Subject: [PATCH] Remove noListener option from useDb --- lib/connection.js | 1 - lib/drivers/node-mongodb-native/connection.js | 11 ++--------- test/types/connection.test.ts | 1 - types/connection.d.ts | 2 +- 4 files changed, 3 insertions(+), 12 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 5a8b2c8ca55..973f7caa114 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -1798,7 +1798,6 @@ Connection.prototype.syncIndexes = async function syncIndexes(options = {}) { * @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 connection object will not make the db listen to events on the original connection. See [issue #9961](https://github.com/Automattic/mongoose/issues/9961). * @return {Connection} New Connection Object * @api public */ diff --git a/lib/drivers/node-mongodb-native/connection.js b/lib/drivers/node-mongodb-native/connection.js index 9e9f8952f6f..e2fc506ed94 100644 --- a/lib/drivers/node-mongodb-native/connection.js +++ b/lib/drivers/node-mongodb-native/connection.js @@ -55,7 +55,6 @@ Object.setPrototypeOf(NativeConnection.prototype, MongooseConnection.prototype); * @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 */ @@ -107,11 +106,7 @@ NativeConnection.prototype.useDb = function(name, options) { function wireup() { newConn.client = _this.client; - const _opts = {}; - if (options.hasOwnProperty('noListener')) { - _opts.noListener = options.noListener; - } - newConn.db = _this.client.db(name, _opts); + newConn.db = _this.client.db(name); newConn._lastHeartbeatAt = _this._lastHeartbeatAt; newConn.onOpen(); } @@ -119,9 +114,7 @@ NativeConnection.prototype.useDb = function(name, options) { newConn.name = name; // push onto the otherDbs stack, this is used when state changes - if (options.noListener !== true) { - this.otherDbs.push(newConn); - } + this.otherDbs.push(newConn); newConn.otherDbs.push(this); // push onto the relatedDbs cache, this is used when state changes diff --git a/test/types/connection.test.ts b/test/types/connection.test.ts index 77ca1787685..f69f2b0065a 100644 --- a/test/types/connection.test.ts +++ b/test/types/connection.test.ts @@ -76,7 +76,6 @@ expectType>(conn.syncIndexes({ background: expectType(conn.useDb('test')); expectType(conn.useDb('test', {})); -expectType(conn.useDb('test', { noListener: true })); expectType(conn.useDb('test', { useCache: true })); expectType>( diff --git a/types/connection.d.ts b/types/connection.d.ts index d7ee4638edd..76b36d17e6f 100644 --- a/types/connection.d.ts +++ b/types/connection.d.ts @@ -273,7 +273,7 @@ declare module 'mongoose' { transaction(fn: (session: mongodb.ClientSession) => Promise, options?: mongodb.TransactionOptions): Promise; /** Switches to a different database using the same connection pool. */ - useDb(name: string, options?: { useCache?: boolean, noListener?: boolean }): Connection; + useDb(name: string, options?: { useCache?: boolean }): Connection; /** The username specified in the URI */ readonly user: string;