Permalink
Browse files

indexes can be manually created

  • Loading branch information...
1 parent 9b1c942 commit 43b28a5821be8d2d9f7c02de6c6f07748bf5eb22 @aheckmann aheckmann committed Jul 14, 2012
Showing with 183 additions and 6 deletions.
  1. +7 −6 lib/model.js
  2. +176 −0 test/model.indexes.test.js
View
@@ -687,6 +687,8 @@ Model.init = function init () {
};
/**
+ * ensureIndexes
+ *
* Sends `ensureIndex` commands to mongo for
* each index declared in the schema.
*
@@ -695,9 +697,11 @@ Model.init = function init () {
*/
Model.ensureIndexes = function ensureIndexes (cb) {
+ var indexes = this.schema.indexes();
+ if (!indexes.length) return cb && cb();
+
var self = this
- , indexes = this.schema.indexes()
- , safe = this.schema.options.safe
+ , safe = self.schema.options.safe
, count = indexes.length
, error
@@ -709,10 +713,7 @@ Model.ensureIndexes = function ensureIndexes (cb) {
if (--count) return;
self.emit('index', error);
- if (cb) cb(error);
-
- //if (err) return self.prototype.db.emit('error', err);
- //--count || self.emit('index');
+ cb && cb(error);
}));
});
}
View
@@ -0,0 +1,176 @@
+
+/**
+ * Test dependencies.
+ */
+
+var start = require('./common')
+ , assert = require('assert')
+ , mongoose = start.mongoose
+ , random = require('../lib/utils').random
+ , Query = require('../lib/query')
+ , Schema = mongoose.Schema
+ , SchemaType = mongoose.SchemaType
+ , CastError = SchemaType.CastError
+ , ValidatorError = SchemaType.ValidatorError
+ , ValidationError = mongoose.Document.ValidationError
+ , ObjectId = Schema.ObjectId
+ , DocumentObjectId = mongoose.Types.ObjectId
+ , DocumentArray = mongoose.Types.DocumentArray
+ , EmbeddedDocument = mongoose.Types.Embedded
+ , MongooseArray = mongoose.Types.Array
+ , MongooseError = mongoose.Error;
+
+describe('model', function(){
+ describe('indexes', function(){
+ it('are created when model is compiled', function(done){
+ var Indexed = new Schema({
+ name : { type: String, index: true }
+ , last : String
+ , email : String
+ });
+
+ Indexed.index({ last: 1, email: 1 }, { unique: true });
+
+ var db = start()
+ , IndexedModel = db.model('IndexedModel', Indexed, 'indexedmodel' + random())
+ , assertions = 0;
+
+ IndexedModel.on('index', function(){
+ IndexedModel.collection.getIndexes(function(err, indexes){
+ assert.ifError(err);
+
+ for (var i in indexes) {
+ indexes[i].forEach(function(index){
+ if (index[0] == 'name')
+ assertions++;
+ if (index[0] == 'last')
+ assertions++;
+ if (index[0] == 'email')
+ assertions++;
+ });
+ }
+
+ db.close();
+ assert.equal(3, assertions);
+ done();
+ });
+ });
+ });
+
+ it('of embedded documents', function(done){
+ var BlogPosts = new Schema({
+ _id : { type: ObjectId, index: true }
+ , title : { type: String, index: true }
+ , desc : String
+ });
+
+ var User = new Schema({
+ name : { type: String, index: true }
+ , blogposts : [BlogPosts]
+ });
+
+ var db = start()
+ , UserModel = db.model('DeepIndexedModel', User, 'deepindexedmodel' + random())
+ , assertions = 0;
+
+ UserModel.on('index', function () {
+ UserModel.collection.getIndexes(function (err, indexes) {
+ assert.ifError(err);
+
+ for (var i in indexes) {
+ indexes[i].forEach(function(index){
+ if (index[0] == 'name')
+ assertions++;
+ if (index[0] == 'blogposts._id')
+ assertions++;
+ if (index[0] == 'blogposts.title')
+ assertions++;
+ });
+ }
+
+ db.close();
+ assert.equal(3, assertions);
+ done();
+ });
+ });
+ });
+
+ it('compound: on embedded docs', function(done){
+ var BlogPosts = new Schema({
+ title : String
+ , desc : String
+ });
+
+ BlogPosts.index({ title: 1, desc: 1 });
+
+ var User = new Schema({
+ name : { type: String, index: true }
+ , blogposts : [BlogPosts]
+ });
+
+ var db = start()
+ , UserModel = db.model('DeepCompoundIndexModel', User, 'deepcompoundindexmodel' + random())
+ , found = 0;
+
+ UserModel.on('index', function () {
+ UserModel.collection.getIndexes(function (err, indexes) {
+ assert.ifError(err);
+
+ for (var index in indexes) {
+ switch (index) {
+ case 'name_1':
+ case 'blogposts.title_1_blogposts.desc_1':
+ ++found;
+ break;
+ }
+ }
+
+ db.close();
+ assert.equal(2, found);
+ done();
+ });
+ });
+ });
+
+ it('error should emit on the model', function(done){
+ var db = start();
+
+ var schema = new Schema({ name: { type: String } })
+ , Test = db.model('IndexError', schema, "x"+random());
+
+ Test.on('index', function (err) {
+ db.close();
+ assert.ok(/^E11000 duplicate key error index:/.test(err.message), err);
+ done();
+ });
+
+ Test.create({ name: 'hi' }, { name: 'hi' }, function (err) {
+ assert.strictEqual(err, null);
+ Test.schema.index({ name: 1 }, { unique: true });
+ Test.schema.index({ other: 1 });
+ Test.init();
+ });
+ });
+
+ it('can be manually triggered', function(done){
+ var db = start();
+ var schema = new Schema({ name: { type: String } })
+ , Test = db.model('ManualIndexing', schema, "x"+random());
+
+ assert.equal('function', typeof Test.ensureIndexes);
+
+ Test.schema.index({ name: 1 }, { sparse: true });
+
+ var called = false;
+ Test.on('index', function(err){
+ called= true;
+ });
+
+ Test.ensureIndexes(function (err) {
+ assert.ifError(err);
+ assert.ok(called);
+ done();
+ });
+ })
+ });
+});

0 comments on commit 43b28a5

Please sign in to comment.