Skip to content

Commit

Permalink
Merge pull request #11315 from Automattic/revert-11222-syncIndexes-gl…
Browse files Browse the repository at this point in the history
…obal-option

Revert "Sync indexes global option"
  • Loading branch information
vkarpov15 committed Feb 2, 2022
2 parents 7fe18af + 950ba35 commit 7f3a43e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 63 deletions.
4 changes: 1 addition & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ function Mongoose(options) {
this.options = Object.assign({
pluralization: true,
autoIndex: true,
autoCreate: true,
syncIndexes: false
autoCreate: true
}, options);
const conn = this.createConnection(); // default connection
conn.models = this.models;
Expand Down Expand Up @@ -168,7 +167,6 @@ Mongoose.prototype.driver = driver;
* - 'strict': true by default, may be `false`, `true`, or `'throw'`. Sets the default strict mode for schemas.
* - 'strictQuery': same value as 'strict' by default (`true`), may be `false`, `true`, or `'throw'`. Sets the default [strictQuery](/docs/guide.html#strictQuery) mode for schemas.
* - 'selectPopulatedPaths': true by default. Set to false to opt out of Mongoose adding all fields that you `populate()` to your `select()`. The schema-level option `selectPopulatedPaths` overwrites this one.
* - 'syncIndexes': false by default. Set to true to run syncIndexes on every model.
* - 'maxTimeMS': If set, attaches [maxTimeMS](https://docs.mongodb.com/manual/reference/operator/meta/maxTimeMS/) to every query
* - 'autoIndex': true by default. Set to false to disable automatic index creation for all models associated with this Mongoose instance.
* - 'autoCreate': Set to `true` to make Mongoose call [`Model.createCollection()`](/docs/api/model.html#model_Model.createCollection) automatically when you create a model with `mongoose.model()` or `conn.model()`. This is useful for testing transactions, change streams, and other features that require the collection to exist.
Expand Down
24 changes: 6 additions & 18 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1271,37 +1271,25 @@ Model.init = function init(callback) {
this.schema.options, this.db.config, this.db.base.options);
const autoCreate = utils.getOption('autoCreate',
this.schema.options, this.db.config, this.db.base.options);
const syncIndexes = utils.getOption('syncIndexes', this.schema.options, this.db.config, this.db.base.options);

const _ensureIndexes = autoIndex ?
cb => this.ensureIndexes({ _automatic: true }, cb) :
cb => cb();
const _createCollection = autoCreate ?
cb => this.createCollection({}, cb) :
cb => cb();
const _syncIndexes = syncIndexes ?
cb => this.syncIndexes({}, cb) :
cb => cb();

this.$init = new Promise((resolve, reject) => {
_createCollection(error => {
if (error) {
return reject(error);
} else if (!syncIndexes) {
_ensureIndexes(error => {
if (error) {
return reject(error);
}
resolve(this);
});
} else {
_syncIndexes(error => {
if (error) {
return reject(error);
}
resolve(this);
});
}
_ensureIndexes(error => {
if (error) {
return reject(error);
}
resolve(this);
});
});
});

Expand Down
1 change: 0 additions & 1 deletion lib/validoptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const VALID_OPTIONS = Object.freeze([
'strict',
'strictPopulate',
'strictQuery',
'syncIndexes',
'toJSON',
'toObject'
]);
Expand Down
41 changes: 0 additions & 41 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,46 +901,5 @@ describe('mongoose module:', function() {
const m = new mongoose.Mongoose();
assert.deepEqual(await m.syncIndexes(), {});
});
it('Allows a syncIndexes global option (gh-11030)', async function() {
const m = new mongoose.Mongoose();

const db = await m.connect('mongodb://localhost:27017/mongoose_test_11030');

m.set('syncIndexes', true);

const initialSchema = new m.Schema({ location: { type: String, index: true } });
const initialModel = db.model('Sync', initialSchema, 'Sync');
await initialModel.init();
const initialIndexes = await initialModel.listIndexes();

assert.equal(initialIndexes.length, 2);

await m.deleteModel('Sync');

m.set('syncIndexes', false);

const schema = new m.Schema({
title: { type: String, index: true }
});
const syncFalse = db.model('Sync', schema, 'Sync');
await syncFalse.init();
const Indexes = await syncFalse.listIndexes();

assert.equal(Indexes.length, 3);

await syncFalse.collection.createIndex({ name: 1 });
await m.deleteModel('Sync');

m.set('syncIndexes', true);

const syncSchema = new m.Schema({ nickname: { type: String, index: true } });
const syncTrue = db.model('Sync', syncSchema, 'Sync');
await syncTrue.init();
const Index = await syncTrue.listIndexes();

assert.equal(Index[0].name, '_id_');

assert.equal(Index.length, 2);
});
});
});

0 comments on commit 7f3a43e

Please sign in to comment.