diff --git a/.travis.yml b/.travis.yml index 8623517fbe0..d8ab5987c52 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,3 +18,5 @@ script: - npm test - npm run lint - npm run nsp +notifications: + email: false diff --git a/History.md b/History.md index cfd80ff6aea..7df838a038a 100644 --- a/History.md +++ b/History.md @@ -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 diff --git a/lib/drivers/node-mongodb-native/collection.js b/lib/drivers/node-mongodb-native/collection.js index 6b190338c85..a5378a72e09 100644 --- a/lib/drivers/node-mongodb-native/collection.js +++ b/lib/drivers/node-mongodb-native/collection.js @@ -4,7 +4,6 @@ var MongooseCollection = require('../../collection'); var Collection = require('mongodb').Collection; -var util = require('util'); var utils = require('../../utils'); /** @@ -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 * diff --git a/lib/model.js b/lib/model.js index c6d5b28c083..ecf2ff52eee 100644 --- a/lib/model.js +++ b/lib/model.js @@ -936,7 +936,7 @@ Model.init = function init(callback) { * * ####Example: * - * Event.createIndexes(function (err) { + * Event.ensureIndexes(function (err) { * if (err) return handleError(err); * }); * @@ -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; } @@ -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); @@ -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) { @@ -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(); @@ -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);