Skip to content

Commit f5605ed

Browse files
committed
fix(connection+index): correctly handle setting driver
1 parent 44e4c88 commit f5605ed

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

lib/connection.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
const ChangeStream = require('./cursor/ChangeStream');
88
const EventEmitter = require('events').EventEmitter;
99
const Schema = require('./schema');
10-
const Collection = require('./driver').get().Collection;
1110
const STATES = require('./connectionstate');
1211
const MongooseError = require('./error/index');
1312
const SyncIndexesError = require('./error/syncIndexes');
1413
const PromiseProvider = require('./promise_provider');
1514
const ServerSelectionError = require('./error/serverSelection');
1615
const applyPlugins = require('./helpers/schema/applyPlugins');
16+
const driver = require('./driver');
1717
const promiseOrCallback = require('./helpers/promiseOrCallback');
1818
const get = require('./helpers/get');
1919
const immediate = require('./helpers/immediate');
@@ -1026,6 +1026,7 @@ Connection.prototype.collection = function(name, options) {
10261026
};
10271027
options = Object.assign({}, defaultOptions, options ? utils.clone(options) : {});
10281028
options.$wasForceClosed = this.$wasForceClosed;
1029+
const Collection = driver.get().Collection;
10291030
if (!(name in this.collections)) {
10301031
this.collections[name] = new Collection(name, this, options);
10311032
}

lib/driver.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@
66

77
let driver = null;
88

9+
const _mongooseInstances = [];
10+
module.exports._mongooseInstances = _mongooseInstances;
11+
912
module.exports.get = function() {
1013
return driver;
1114
};
1215

1316
module.exports.set = function(v) {
1417
driver = v;
18+
19+
for (const mongoose of _mongooseInstances) {
20+
const Connection = driver.getConnection();
21+
mongoose.Connection = Connection;
22+
mongoose.connections = [new Connection(mongoose)];
23+
mongoose.Collection = driver.Collection;
24+
}
1525
};

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ function Mongoose(options) {
7070
}, options);
7171
const conn = this.createConnection(); // default connection
7272
conn.models = this.models;
73+
driver._mongooseInstances.push(this);
7374

7475
if (this.options.pluralization) {
7576
this._pluralize = legacyPluralize;
@@ -275,6 +276,7 @@ Mongoose.prototype.get = Mongoose.prototype.set;
275276
Mongoose.prototype.createConnection = function(uri, options, callback) {
276277
const _mongoose = this instanceof Mongoose ? this : mongoose;
277278

279+
const Connection = driver.get().getConnection();
278280
const conn = new Connection(_mongoose);
279281
if (typeof options === 'function') {
280282
callback = options;

0 commit comments

Comments
 (0)