Skip to content

Commit

Permalink
fix(mongoose): allow setting useCreateIndex option after creating a m…
Browse files Browse the repository at this point in the history
…odel but before initial connection succeeds

Fix #6890
  • Loading branch information
vkarpov15 committed Sep 2, 2018
1 parent aff6596 commit 15627d3
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/model.js
Expand Up @@ -987,10 +987,7 @@ Model.init = function init(callback) {
const autoIndex = this.schema.options.autoIndex;
this.$init = new Promise((resolve, reject) => {
if (autoIndex || (autoIndex == null && this.db.config.autoIndex)) {
const options = {
_automatic: true,
createIndex: this.base.options.useCreateIndex
};
const options = { _automatic: true };
this.ensureIndexes(options, function(error) {
if (error) {
return reject(error);
Expand Down Expand Up @@ -1211,6 +1208,10 @@ Model.createIndexes = function createIndexes(options, callback) {
return this.ensureIndexes(options, callback);
};

/*!
* ignore
*/

function _ensureIndexes(model, options, callback) {
const indexes = model.schema.indexes();

Expand Down Expand Up @@ -1271,7 +1272,10 @@ function _ensureIndexes(model, options, callback) {
applyWriteConcern(model.schema, indexOptions);

indexSingleStart(indexFields, options);
const methodName = options.createIndex ? 'createIndex' : 'ensureIndex';
const useCreateIndex = 'createIndex' in options ?
options.createIndex :
model.base.options.useCreateIndex;
const methodName = useCreateIndex ? 'createIndex' : 'ensureIndex';
model.collection[methodName](indexFields, indexOptions, utils.tick(function(err, name) {
indexSingleDone(err, indexFields, indexOptions, name);
if (err) {
Expand Down

0 comments on commit 15627d3

Please sign in to comment.