Skip to content

Commit

Permalink
fix: undeprecate ensureIndex() and use it by default
Browse files Browse the repository at this point in the history
Fix #3280
  • Loading branch information
vkarpov15 committed Oct 31, 2017
1 parent 5ec995c commit 64fd968
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
7 changes: 0 additions & 7 deletions lib/drivers/node-mongodb-native/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,6 @@ for (var i in Collection.prototype) {
iter(i);
}

/*!
* ignore
*/

Collection.prototype.ensureIndex = util.deprecate(Collection.prototype.ensureIndex,
'`ensureIndex()` is deprecated in Mongoose >= 4.12.0, use `createIndex()` instead');

/**
* Debug print helper
*
Expand Down
38 changes: 31 additions & 7 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ Model.init = function init(callback) {
*
* ####Example:
*
* Event.createIndexes(function (err) {
* Event.ensureIndexes(function (err) {
* if (err) return handleError(err);
* });
*
Expand All @@ -958,14 +958,14 @@ Model.init = function init(callback) {
* @api public
*/

Model.createIndexes = Model.ensureIndexes = function createIndexes(options, callback) {
Model.ensureIndexes = function ensureIndexes(options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}

if (options && options.__noPromise) {
_createIndexes(this, options, callback);
_ensureIndexes(this, options, callback);
return;
}

Expand All @@ -976,7 +976,7 @@ Model.createIndexes = Model.ensureIndexes = function createIndexes(options, call
var _this = this;
var Promise = PromiseProvider.get();
return new Promise.ES6(function(resolve, reject) {
_createIndexes(_this, options || {}, function(error) {
_ensureIndexes(_this, options || {}, function(error) {
if (error) {
callback && callback(error);
reject(error);
Expand All @@ -987,8 +987,31 @@ Model.createIndexes = Model.ensureIndexes = function createIndexes(options, call
});
};

function _createIndexes(model, options, callback) {
/**
* Similar to `ensureIndexes()`, except for it uses the [`createIndex`](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#createIndex)
* function. The `ensureIndex()` function checks to see if an index with that
* name already exists, and, if not, does not attempt to create the index.
* `createIndex()` bypasses this check.
*
* @param {Object} [options] internal options
* @param {Function} [cb] optional callback
* @return {Promise}
* @api public
*/

Model.createIndexes = function createIndexes(options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
options.createIndex = true;
return this.ensureIndexes(options, callback);
};

function _ensureIndexes(model, options, callback) {
var indexes = model.schema.indexes();
options = options || {};

var done = function(err) {
if (err && model.schema.options.emitIndexErrors) {
Expand All @@ -1015,7 +1038,7 @@ function _createIndexes(model, options, callback) {
};

var create = function() {
if (options && options._automatic) {
if (options._automatic) {
if (model.schema.options.autoIndex === false ||
(model.schema.options.autoIndex == null && model.db.config.autoIndex === false)) {
return done();
Expand All @@ -1030,7 +1053,8 @@ function _createIndexes(model, options, callback) {
_handleSafe(options);

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

0 comments on commit 64fd968

Please sign in to comment.