-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3a8381d
fix(connection): disable createConnection buffering if autoCreate opt…
lpizzinidev f2057dd
Merge branch 'master' into gh-12940
lpizzinidev afd06d5
fix: adjusted createCollection call for concurrent initializations
lpizzinidev a1b1b89
Merge branch 'master' into gh-12940
lpizzinidev fec503c
Merge branch 'master' into gh-12940
lpizzinidev 08b0efc
fix: added autoIndex delayed check and removed _wrapCollHelper function
lpizzinidev File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,9 +60,11 @@ const modifiedPaths = require('./helpers/update/modifiedPaths'); | |
const parallelLimit = require('./helpers/parallelLimit'); | ||
const parentPaths = require('./helpers/path/parentPaths'); | ||
const prepareDiscriminatorPipeline = require('./helpers/aggregate/prepareDiscriminatorPipeline'); | ||
const promiseOrCallback = require('./helpers/promiseOrCallback'); | ||
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'); | ||
|
||
|
@@ -1354,15 +1356,37 @@ 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 _wrapCollHelper = function(fn) { | ||
return function(cb) { | ||
const conn = model.db; | ||
|
||
return promiseOrCallback(cb, cb => { | ||
immediate(() => { | ||
if ((conn.readyState === STATES.connecting || conn.readyState === STATES.disconnected) && conn._shouldBufferCommands()) { | ||
conn._queue.push({ fn: fn, ctx: conn, args: [cb] }); | ||
} else { | ||
try { | ||
fn.apply(conn, [cb]); | ||
} catch (err) { | ||
return cb(err); | ||
} | ||
} | ||
}); | ||
}); | ||
}; | ||
}; | ||
const _createCollection = _wrapCollHelper(function _createCollection(cb) { | ||
const autoCreate = utils.getOption('autoCreate', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, let's not add a separate |
||
model.schema.options, model.db.config, model.db.base.options); | ||
if (autoCreate) model.createCollection({}, cb); | ||
else cb(); | ||
}); | ||
|
||
this.$init = new Promise((resolve, reject) => { | ||
_createCollection(error => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency, let's also delay checking
autoIndex
until after buffering is done. Apply similar changes to_ensureIndexes()
. No need to do_wrapCollHelper
on_ensureIndexes()
though, since_createCollection()
will already wait for buffering.