Skip to content

Commit

Permalink
fix(connection): allow specifying useCreateIndex at the connection …
Browse files Browse the repository at this point in the history
…level, overrides global-level

Fix #6922
  • Loading branch information
vkarpov15 committed Sep 19, 2018
1 parent 97171e3 commit 4aa22a1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,11 @@ Connection.prototype.openUri = function(uri, options, callback) {
delete options.autoIndex;
}

if ('useCreateIndex' in options) {
this.config.useCreateIndex = !!options.useCreateIndex;
delete options.useCreateIndex;
}

// Backwards compat
if (options.user || options.pass) {
options.auth = options.auth || {};
Expand Down
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ Mongoose.prototype.createConnection = function(uri, options, callback) {
* @param {String} [options.pass] password for authentication, equivalent to `options.auth.password`. Maintained for backwards compatibility.
* @param {Boolean} [options.autoIndex=true] Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.
* @param {Boolean} [options.bufferCommands=true] Mongoose specific option. Set to false to [disable buffering](http://mongoosejs.com/docs/faq.html#callback_never_executes) on all models associated with this connection.
* @param {Boolean} [options.useCreateIndex=true] Mongoose-specific option. If `true`, this connection will use [`createIndex()` instead of `ensureIndex()``true`, this connection will use [`createIndex()` instead of `ensureIndex()`](/docs/deprecations.html#-ensureindex-) for automatic index builds via [`Model.init()`](/docs/api.html#model_Model.init).
* @param {Function} [callback]
* @see Mongoose#createConnection #index_Mongoose-createConnection
* @api public
Expand Down Expand Up @@ -542,7 +543,7 @@ const driver = global.MONGOOSE_DRIVER_PATH || './drivers/node-mongodb-native';
* Connection
*/

var Connection = require(driver + '/connection');
const Connection = require(driver + '/connection');

/*!
* Collection
Expand Down Expand Up @@ -791,4 +792,4 @@ Mongoose.prototype.mquery = require('mquery');
* @api public
*/

var mongoose = module.exports = exports = new Mongoose;
const mongoose = module.exports = exports = new Mongoose;
11 changes: 8 additions & 3 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1299,9 +1299,14 @@ function _ensureIndexes(model, options, callback) {
applyWriteConcern(model.schema, indexOptions);

indexSingleStart(indexFields, options);
const useCreateIndex = 'createIndex' in options ?
options.createIndex :
model.base.options.useCreateIndex;
let useCreateIndex = !!model.base.options.useCreateIndex;
if ('useCreateIndex' in model.db.config) {
useCreateIndex = !!model.db.config.useCreateIndex;
}
if ('createIndex' in options) {
useCreateIndex = !!options.useCreateIndex;
}

const methodName = useCreateIndex ? 'createIndex' : 'ensureIndex';
model.collection[methodName](indexFields, indexOptions, utils.tick(function(err, name) {
indexSingleDone(err, indexFields, indexOptions, name);
Expand Down
17 changes: 17 additions & 0 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ describe('connections:', function() {
}).catch(done);
});

it('useCreateIndex (gh-6922)', function(done) {
const conn = mongoose.createConnection('mongodb://localhost:27017/mongoosetest', {
useCreateIndex: true,
useNewUrlParser: true
});

const M = conn.model('Test', new Schema({
name: { type: String, index: true }
}));

M.collection.ensureIndex = function() {
throw new Error('Fail');
};

conn.then(() => done(), err => done(err));
});

it('throws helpful error with legacy syntax (gh-6756)', function(done) {
assert.throws(function() {
mongoose.createConnection('localhost', 'dbname', 27017);
Expand Down

0 comments on commit 4aa22a1

Please sign in to comment.