Permalink
Browse files

added; support for disabled auto-indexing

closes #984
  • Loading branch information...
1 parent 43b28a5 commit 0cfb1ab2581e115df6e84374ca3023d8f92542e8 @aheckmann aheckmann committed Jul 14, 2012
Showing with 28 additions and 2 deletions.
  1. +6 −2 lib/model.js
  2. +1 −0 lib/schema.js
  3. +21 −0 test/model.indexes.test.js
View
8 lib/model.js
@@ -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);
};
@@ -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
View
1 lib/schema.js
@@ -39,6 +39,7 @@ function Schema (obj, options) {
, capped: false // { size, max, autoIndexId }
, versionKey: '__v'
, minimize: true
+ , autoIndex: true
}, options);
// build paths
View
21 test/model.indexes.test.js
@@ -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 } })

0 comments on commit 0cfb1ab

Please sign in to comment.