From 80cba3f3f4483df1285a3675910b7db7271daa55 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 20 Sep 2019 15:00:25 -0700 Subject: [PATCH] fix(mongoose): support `mongoose.set('autoIndex', false)` Fix #8158 --- lib/index.js | 1 + lib/model.js | 5 ++--- lib/utils.js | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index c76914492c7..ad89dffc536 100644 --- a/lib/index.js +++ b/lib/index.js @@ -158,6 +158,7 @@ Mongoose.prototype.driver = require('./driver'); * - 'strict': true by default, may be `false`, `true`, or `'throw'`. Sets the default strict mode for schemas. * - 'selectPopulatedPaths': true by default. Set to false to opt out of Mongoose adding all fields that you `populate()` to your `select()`. The schema-level option `selectPopulatedPaths` overwrites this one. * - '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. * * @param {String} key * @param {String|Function|Boolean} value diff --git a/lib/model.js b/lib/model.js index ca41fc0e99b..238b3d73225 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1184,9 +1184,8 @@ Model.init = function init(callback) { } const Promise = PromiseProvider.get(); - const autoIndex = this.schema.options.autoIndex == null ? - this.db.config.autoIndex : - this.schema.options.autoIndex; + 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; diff --git a/lib/utils.js b/lib/utils.js index 7a34b4376fc..846b68ee0ac 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1072,6 +1072,22 @@ exports.each = function(arr, fn) { } }; +/*! + * ignore + */ + +exports.getOption = function(name) { + const sources = Array.prototype.slice.call(arguments, 1); + + for (const source of sources) { + if (source[name] != null) { + return source[name]; + } + } + + return null; +}; + /*! * ignore */