Skip to content

Commit

Permalink
fix(mongoose): support mongoose.set('autoIndex', false)
Browse files Browse the repository at this point in the history
Fix #8158
  • Loading branch information
vkarpov15 committed Sep 20, 2019
1 parent 97c8114 commit 80cba3f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 80cba3f

Please sign in to comment.