Skip to content

Commit

Permalink
added; support for disabled auto-indexing
Browse files Browse the repository at this point in the history
closes #984
  • Loading branch information
aheckmann committed Jul 14, 2012
1 parent 43b28a5 commit 0cfb1ab
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/model.js
Expand Up @@ -682,7 +682,9 @@ for (var i in EventEmitter.prototype)
*/

Model.init = function init () {
this.ensureIndexes()
if (this.schema.options.autoIndex)
this.ensureIndexes();

this.schema.emit('init', this);
};

Expand All @@ -698,7 +700,9 @@ Model.init = function init () {

Model.ensureIndexes = function ensureIndexes (cb) {
var indexes = this.schema.indexes();
if (!indexes.length) return cb && cb();
if (!indexes.length) {
return cb && cb();
}

var self = this
, safe = self.schema.options.safe
Expand Down
1 change: 1 addition & 0 deletions lib/schema.js
Expand Up @@ -39,6 +39,7 @@ function Schema (obj, options) {
, capped: false // { size, max, autoIndexId }
, versionKey: '__v'
, minimize: true
, autoIndex: true
}, options);

// build paths
Expand Down
21 changes: 21 additions & 0 deletions test/model.indexes.test.js
Expand Up @@ -152,6 +152,27 @@ describe('model', function(){
});
});

describe('auto creation', function(){
it('can be disabled', function(done){
var db = start();
var schema = new Schema({ name: { type: String, index: true }})
schema.set('autoIndex', false);

var Test = db.model('AutoIndexing', schema, "x"+random());
Test.on('index', function(err){
assert.ok(false, 'Model.ensureIndexes() was called');
});

setTimeout(function () {
Test.collection.getIndexes(function(err, indexes){
assert.ifError(err);
assert.equal(0, Object.keys(indexes).length);
done();
});
}, 100);
})
})

it('can be manually triggered', function(done){
var db = start();
var schema = new Schema({ name: { type: String } })
Expand Down

0 comments on commit 0cfb1ab

Please sign in to comment.