Skip to content

Commit

Permalink
Merge branch 'master' into 4.13
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Nov 2, 2017
2 parents a980af8 + a48219b commit 4348fa1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ script:
- npm test
- npm run lint
- npm run nsp
notifications:
email: false
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
4.12.6 / 2017-11-01
===================
* fix(schema): make clone() copy query helpers correctly #5752
* fix: undeprecate `ensureIndex()` and use it by default #3280

4.12.5 / 2017-10-29
===================
* fix(query): correctly handle `$in` and required for $pull and update validators #5744
Expand Down
8 changes: 0 additions & 8 deletions lib/drivers/node-mongodb-native/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

var MongooseCollection = require('../../collection');
var Collection = require('mongodb').Collection;
var util = require('util');
var utils = require('../../utils');

/**
Expand Down Expand Up @@ -156,13 +155,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 @@ -936,7 +936,7 @@ Model.init = function init(callback) {
*
* ####Example:
*
* Event.createIndexes(function (err) {
* Event.ensureIndexes(function (err) {
* if (err) return handleError(err);
* });
*
Expand All @@ -959,14 +959,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 @@ -977,7 +977,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 @@ -988,8 +988,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 @@ -1016,7 +1039,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 @@ -1031,7 +1054,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 4348fa1

Please sign in to comment.