Skip to content

Commit

Permalink
added support for ensuring indexes in model definitions, simple and c…
Browse files Browse the repository at this point in the history
…ompound indexes
  • Loading branch information
nw committed May 12, 2010
1 parent 8c3bd8c commit 976f138
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions examples/models/User.js
Expand Up @@ -12,6 +12,13 @@ Mongoose.Model.define('User', {
age: Number
}
},

indexes : [
'username',
'bio.age',
[['first'],['last']]
],

setters: {
first: function(v){
return v.toUpperCase();
Expand Down
1 change: 0 additions & 1 deletion examples/user.js
Expand Up @@ -6,7 +6,6 @@ var sys = require('sys'),
sys.puts(err);
});

// Mongoose.load(__dirname+'/../lib/model/plugins/behaviors.js');
Mongoose.load(__dirname+'/models/');
db = Mongoose.connect('mongodb://localhost/test');

Expand Down
16 changes: 15 additions & 1 deletion lib/model/index.js
Expand Up @@ -220,7 +220,7 @@ var object = require('../utils/object'),
return this;
}
})();

model.name = collection; // collection = collection event loop... hmmm...FIXME

addMethods(model,{ parent : parent, __meta__ : {type : {} } },false,model);
Expand Down Expand Up @@ -256,6 +256,20 @@ var object = require('../utils/object'),
store.collection(collection,function(aCollection){
model.collection = aCollection;
model.loaded = true;

model.indexInformation().execute(function(info){
def.indexes.forEach(function(idx){
var key = '';
if(Object.prototype.toString.call(idx) === '[object Array]'){
for(i=0,l=idx.length; i < l; i++){
if(i) key += '_';
key += idx[i][0] +'_1';
}
} else key = idx + '_1';
if(!info[key]) aCollection.createIndex(idx,function(){});
});
});

model.dequeue();
});
}
Expand Down
1 change: 1 addition & 0 deletions lib/model/static.js
Expand Up @@ -22,6 +22,7 @@ this.Static = {
save : function(){ return this._cmd('save', Array.prototype.slice.call(arguments,0)); },
drop : function(fn){ return this.store.dropCollection(this.name, ((typeof fn == 'function') ? fn : function(){}) ); },
close : function(){ this.store.db.close(); },
indexInformation : function(){ return this._cmd('indexInformation', Array.prototype.slice.call(arguments,0)); },

_cmd : function(cmd,args){ return new QueryPromise(cmd,args,this); },

Expand Down

0 comments on commit 976f138

Please sign in to comment.