Skip to content

Commit

Permalink
Merge pull request #12889 from Automattic/vkarpov15/fix-model-while-d…
Browse files Browse the repository at this point in the history
…isconnected

fix(collection): handle creating model when connection disconnected with bufferCommands = false
  • Loading branch information
vkarpov15 authored Jan 17, 2023
2 parents ed8eacf + 743acf6 commit 8b99ca7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
29 changes: 23 additions & 6 deletions lib/drivers/node-mongodb-native/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ Object.setPrototypeOf(NativeCollection.prototype, MongooseCollection.prototype);
*/

NativeCollection.prototype.onOpen = function() {
const _this = this;

_this.collection = _this.conn.db.collection(_this.name);
MongooseCollection.prototype.onOpen.call(_this);
return _this.collection;
this.collection = this.conn.db.collection(this.name);
MongooseCollection.prototype.onOpen.call(this);
return this.collection;
};

/**
Expand All @@ -60,6 +58,25 @@ NativeCollection.prototype.onClose = function(force) {
MongooseCollection.prototype.onClose.call(this, force);
};

/**
* Helper to get the collection, in case `this.collection` isn't set yet.
* May happen if `bufferCommands` is false and created the model when
* Mongoose was disconnected.
*
* @api private
*/

NativeCollection.prototype._getCollection = function _getCollection() {
if (this.collection) {
return this.collection;
}
if (this.conn.db != null) {
this.collection = this.conn.db.collection(this.name);
return this.collection;
}
return null;
};

/*!
* ignore
*/
Expand All @@ -74,7 +91,7 @@ const syncCollectionMethods = { watch: true, find: true, aggregate: true };

function iter(i) {
NativeCollection.prototype[i] = function() {
const collection = this.collection;
const collection = this._getCollection();
const args = Array.from(arguments);
const _this = this;
const globalDebug = _this &&
Expand Down
19 changes: 19 additions & 0 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1495,4 +1495,23 @@ describe('connections:', function() {

assert.equal(m.connection.model('Test2'), Test2);
});

it('creates collection if creating model while connection is disconnected with bufferCommands=false', async function() {
const m = new mongoose.Mongoose();
m.set('bufferCommands', false);
const conn = await m.createConnection(start.uri, { bufferCommands: false }).asPromise();
conn.client.emit('serverDescriptionChanged', { newDescription: { type: 'Unknown' } });

const Test = conn.model('Test', new Schema({ name: String }));

const [res] = await Promise.all([
Test.findOne().exec(),
new Promise.resolve(resolve => setTimeout(resolve, 100)).then(() => {
conn.client.emit('serverDescriptionChanged', { newDescription: { type: 'Single' } });
})
]);
assert.equal(res, null);

await m.disconnect();
});
});

0 comments on commit 8b99ca7

Please sign in to comment.