Skip to content

Commit 6deb668

Browse files
committedMar 24, 2021
fix: connection ids are now scoped
Some tests are failing but they were failing before the changes I made.
1 parent ccf5b16 commit 6deb668

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed
 

‎lib/connection.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ function Connection(base) {
7070
this._closeCalled = false;
7171
this._hasOpened = false;
7272
this.plugins = [];
73-
this.id = id++;
73+
// this.id = id++;
74+
if (typeof base === 'undefined' || !base.connections.length) {
75+
this.id = 0;
76+
} else {
77+
this.id = base.connections.length;
78+
}
7479
}
7580

7681
/*!

‎test/connection.test.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1301,9 +1301,19 @@ describe('connections:', function() {
13011301
});
13021302
it('Connection id should be scoped per Mongoose Instance (gh-10025)', function() {
13031303
const m = new mongoose.Mongoose;
1304-
console.log(m.connection.id);
1304+
// console.log('First Instance', m.connection.id); // should be x
1305+
const conn = m.createConnection();
1306+
// console.log(conn.id); // should be x + 1
13051307
const m1 = new mongoose.Mongoose;
1306-
console.log(m1.connection.id);
1308+
// console.log('Second Instance', m1.connection.id); // should be y
1309+
const conn2 = m1.createConnection();
1310+
// console.log(conn2.id); // should be y + 1;
1311+
const conn3 = m.createConnection();
1312+
// console.log('Back to First Instance', conn3.id); // should be x + 2
1313+
assert.deepStrictEqual(m.connection.id, 0);
1314+
assert.deepStrictEqual(conn.id, m.connection.id + 1);
13071315
assert.deepStrictEqual(m1.connection.id, 0);
1316+
assert.deepStrictEqual(conn2.id, m1.connection.id + 1);
1317+
assert.deepStrictEqual(conn3.id, m.connection.id + 2);
13081318
});
13091319
});

0 commit comments

Comments
 (0)
Please sign in to comment.