diff --git a/index.js b/index.js index 38c93eb..6e7e459 100644 --- a/index.js +++ b/index.js @@ -154,17 +154,28 @@ var MongodbDriver = Base.extend({ * Adds an index to a collection * * @param collectionName - The collection to add the index to - * @param indexName - The name of the index to add - * @param columns - The columns to add an index on - * @param unique - A boolean whether this creates a unique index + * @param indexOptions - An object of options to be used as Index options + * @param callback */ - addIndex: function(collectionName, indexName, columns, unique, callback) { - - var options = { - indexName: indexName, - columns: columns, - unique: unique - }; + addIndex: function(collectionName, indexOptions, callback) { + var options = {}; + if (indexOptions.constructor.name === 'Object') { + options = { + name: indexOptions.name, + columns: indexOptions.columns, + unique: indexOptions.unique + }; + if (indexOptions.hasOwnProperty('sparse')) { + options.sparse = indexOptions.sparse; + } + } else { + options = { + name: arguments[1], + columns: arguments[2], + unique: arguments[3] + }; + callback = arguments[4]; + } return this._run('createIndex', collectionName, options) .nodeify(callback); @@ -313,7 +324,7 @@ var MongodbDriver = Base.extend({ db[command](collection, options.newCollection, callbackFunction); break; case 'createIndex': - db[command](collection, options.columns, {name: options.indexName, unique: options.unique}, callbackFunction); + db[command](collection, options.columns, options, callbackFunction); break; case 'dropIndex': db.collection(collection)[command](options.indexName, callbackFunction); diff --git a/test/mongodb_test.js b/test/mongodb_test.js index cfa8b1b..a2393fa 100644 --- a/test/mongodb_test.js +++ b/test/mongodb_test.js @@ -122,7 +122,7 @@ vows.describe('mongodb').addBatch({ return this.callback(err); } - db.addIndex('event', 'event_title', 'title', false, this.callback); + db.addIndex('event', { name: 'event_title', columns: 'title', unique: false, sparse: true }, this.callback); }.bind(this)); }, @@ -230,7 +230,7 @@ vows.describe('mongodb').addBatch({ return this.callback(err); } - db.addIndex('event', 'event_title', 'title', false, function(err, data) { + db.addIndex('event', { name: 'event_title', columns: 'title', unique: false }, function(err, data) { if(err) { return this.callback(err); @@ -299,7 +299,7 @@ vows.describe('mongodb').addBatch({ return this.callback(err); } - db.addIndex('event', 'event_title', 'title', false, function(err, data) { + db.addIndex('event', { name: 'event_title', columns: 'title', unique: false }, function(err, data) { if(err) { return this.callback(err); @@ -360,4 +360,51 @@ vows.describe('mongodb').addBatch({ } } } -}).export(module); +}) +.addBatch({ + 'addIndex-backward-compatibility': { + topic: function() { + db.createCollection('event', function(err, collection) { + if(err) { + return this.callback(err); + } + + db.addIndex('event', 'event_title','title', false, this.callback); + }.bind(this)); + }, + + teardown: function() { + db.dropCollection('event', this.callback); + }, + + 'preserves case': { + topic: function() { + db._getCollectionNames(this.callback); + }, + + 'of the functions original table': function(err, tables) { + var index = 0; + assert.isNotNull(tables); + assert.equal(tables.length, 2); // Should be 2 b/c of the system collection + + if( tables[0].collectionName === 'system.indexes' ) + index = 1; + + assert.equal(tables[index].collectionName, 'event'); + } + }, + + 'has resulting index metadata': { + topic: function() { + db._getIndexes('event', this.callback); + }, + + 'with additional index': function(err, indexes) { + assert.isDefined(indexes); + assert.isNotNull(indexes); + assert.include(indexes, 'event_title'); + } + } + } +}) +.export(module);