From 0335578a9cd6528ac2bc1f2325e8a4400c68df1c Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 6 Oct 2020 16:26:44 -0400 Subject: [PATCH] fix(mongoose): allow setting `autoCreate` as a global option to be consistent with `autoIndex` Fix #9466 --- lib/index.js | 1 + lib/model.js | 5 ++--- lib/validoptions.js | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index b76b831b848..fccb625bd62 100644 --- a/lib/index.js +++ b/lib/index.js @@ -166,6 +166,7 @@ Mongoose.prototype.driver = require('./driver'); * - 'typePojoToMixed': true by default, may be `false` or `true`. Sets the default typePojoToMixed for schemas. * - 'maxTimeMS': If set, attaches [maxTimeMS](https://docs.mongodb.com/manual/reference/operator/meta/maxTimeMS/) to every query * - 'autoIndex': true by default. Set to false to disable automatic index creation for all models associated with this Mongoose instance. + * - 'autoCreate': Set to `true` to make Mongoose call [`Model.createCollection()`](/docs/api/model.html#model_Model.createCollection) automatically when you create a model with `mongoose.model()` or `conn.model()`. This is useful for testing transactions, change streams, and other features that require the collection to exist. * * @param {String} key * @param {String|Function|Boolean} value diff --git a/lib/model.js b/lib/model.js index 2295e0437de..0c1e8ccb701 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1236,9 +1236,8 @@ 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 = this.schema.options.autoCreate == null ? - this.db.config.autoCreate : - this.schema.options.autoCreate; + const autoCreate = utils.getOption('autoCreate', + this.schema.options, this.db.config, this.db.base.options); const _ensureIndexes = autoIndex ? cb => this.ensureIndexes({ _automatic: true }, cb) : diff --git a/lib/validoptions.js b/lib/validoptions.js index 6e50ec6b69d..510d7e277e7 100644 --- a/lib/validoptions.js +++ b/lib/validoptions.js @@ -8,6 +8,7 @@ const VALID_OPTIONS = Object.freeze([ 'applyPluginsToChildSchemas', 'applyPluginsToDiscriminators', + 'autoCreate', 'autoIndex', 'bufferCommands', 'cloneSchemas',