From 90f45cd23c519829c210420d6d3720c7b0ea48ce Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Thu, 2 Feb 2023 09:12:54 +0100 Subject: [PATCH 1/2] fix(connection): handles unique autoincrement ID for connections fix #12966 --- lib/connection.js | 2 +- lib/index.js | 23 +++++++++++++++++++++++ test/connection.test.js | 12 ++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/connection.js b/lib/connection.js index 9339f138a05..1a3e817eaeb 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -72,7 +72,7 @@ function Connection(base) { if (typeof base === 'undefined' || !base.connections.length) { this.id = 0; } else { - this.id = base.connections.length; + this.id = base.nextConnectionId; } this._queue = []; } diff --git a/lib/index.js b/lib/index.js index 2109d3eec6d..7acb305ce1f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -61,6 +61,7 @@ const objectIdHexRegexp = /^[0-9A-Fa-f]{24}$/; */ function Mongoose(options) { this.connections = []; + this.nextConnectionId = 0; this.models = {}; this.events = new EventEmitter(); this.__driver = driver.get(); @@ -345,6 +346,7 @@ Mongoose.prototype.createConnection = function(uri, options, callback) { options = null; } _mongoose.connections.push(conn); + _mongoose.nextConnectionId++; _mongoose.events.emit('createConnection', conn); if (arguments.length > 0) { @@ -789,6 +791,27 @@ Mongoose.prototype.__defineSetter__('connection', function(v) { Mongoose.prototype.connections; +/** + * An integer containing the value of the next connection id. Calling + * [`createConnection()`](#mongoose_Mongoose-createConnection) increments + * this value. + * + * #### Example: + * + * const mongoose = require('mongoose'); + * mongoose.createConnection(); // id `1`, `nextConnectionId` becomes `2` + * mongoose.createConnection(); // id `2`, `nextConnectionId` becomes `3` + * mongoose.connections[0].destroy() // Removes connection with id `1` + * mongoose.createConnection(); // id `3`, `nextConnectionId` becomes `4` + * + * @memberOf Mongoose + * @instance + * @property {Number} nextConnectionId + * @api private + */ + +Mongoose.prototype.nextConnectionId; + /** * The Mongoose Aggregate constructor * diff --git a/test/connection.test.js b/test/connection.test.js index 3d6ef8f795a..d27c7879605 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -1514,4 +1514,16 @@ describe('connections:', function() { await m.disconnect(); }); + + it('should create connections with unique IDs also if one has been destroyed (gh-12966)', function() { + const m = new mongoose.Mongoose(); + m.createConnection(); + m.createConnection(); + m.connections[0].destroy(); + m.createConnection(); + m.createConnection(); + m.createConnection(); + const connectionIds = m.connections.map(c => c.id); + assert.deepEqual(connectionIds, [1, 2, 3, 4, 5]); + }); }); From 9e10a5edbaf177eb15310f8969b1306aa56327d6 Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Thu, 2 Feb 2023 09:15:04 +0100 Subject: [PATCH 2/2] fixed example id values --- lib/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 7acb305ce1f..0d1dd18f4b2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -799,10 +799,10 @@ Mongoose.prototype.connections; * #### Example: * * const mongoose = require('mongoose'); + * mongoose.createConnection(); // id `0`, `nextConnectionId` becomes `1` * mongoose.createConnection(); // id `1`, `nextConnectionId` becomes `2` + * mongoose.connections[0].destroy() // Removes connection with id `0` * mongoose.createConnection(); // id `2`, `nextConnectionId` becomes `3` - * mongoose.connections[0].destroy() // Removes connection with id `1` - * mongoose.createConnection(); // id `3`, `nextConnectionId` becomes `4` * * @memberOf Mongoose * @instance