Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(connection): disable createConnection buffering if autoCreate option is changed #13007

Merged
merged 6 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const prepareDiscriminatorPipeline = require('./helpers/aggregate/prepareDiscrim
const pushNestedArrayPaths = require('./helpers/model/pushNestedArrayPaths');
const removeDeselectedForeignField = require('./helpers/populate/removeDeselectedForeignField');
const setDottedPath = require('./helpers/path/setDottedPath');
const STATES = require('./connectionstate');
const util = require('util');
const utils = require('./utils');

Expand Down Expand Up @@ -1352,17 +1353,30 @@ Model.init = function init(callback) {
}

const Promise = PromiseProvider.get();
const autoIndex = utils.getOption('autoIndex',
this.schema.options, this.db.config, this.db.base.options);
const autoCreate = utils.getOption('autoCreate',
this.schema.options, this.db.config, this.db.base.options);

const _ensureIndexes = autoIndex ?
cb => this.ensureIndexes({ _automatic: true }, cb) :
cb => cb();
const _createCollection = autoCreate ?
cb => this.createCollection({}, cb) :
cb => cb();

const model = this;
const _ensureIndexes = function(cb) {
const autoIndex = utils.getOption('autoIndex',
model.schema.options, model.db.config, model.db.base.options);
if (autoIndex) model.ensureIndexes({ _automatic: true }, cb);
else cb();
};
const _createCollection = function(cb) {
const conn = model.db;

if ((conn.readyState === STATES.connecting || conn.readyState === STATES.disconnected) && conn._shouldBufferCommands()) {
conn._queue.push({ fn: _createCollection, ctx: conn, args: [cb] });
} else {
try {
const autoCreate = utils.getOption('autoCreate',
model.schema.options, model.db.config, model.db.base.options);
if (autoCreate) model.createCollection({}, cb);
else cb();
} catch (err) {
return cb(err);
}
}
};

this.$init = new Promise((resolve, reject) => {
_createCollection(error => {
Expand Down
19 changes: 19 additions & 0 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1526,4 +1526,23 @@ describe('connections:', function() {
const connectionIds = m.connections.map(c => c.id);
assert.deepEqual(connectionIds, [1, 2, 3, 4, 5]);
});

it('with autoCreate = false after schema create (gh-12940)', async function() {
const m = new mongoose.Mongoose();

const schema = new Schema({ name: String }, {
collation: { locale: 'en_US', strength: 1 },
collection: 'gh12940_Conn'
});
const Model = m.model('gh12940_Conn', schema);

await m.connect(start.uri, {
autoCreate: false
});

await Model.init();

const res = await m.connection.db.listCollections().toArray();
assert.ok(!res.map(c => c.name).includes('gh12940_Conn'));
});
});