From f6e47e7827ec6fc89a5c7b02c28ab6840789cc2e Mon Sep 17 00:00:00 2001 From: Ramakrishnan Date: Mon, 3 Oct 2016 14:27:44 +0530 Subject: [PATCH 1/5] Fix for passing sparse options for createIndex --- index.js | 13 ++++++++----- test/mongodb_test.js | 6 +++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 38c93eb..a11c43a 100644 --- a/index.js +++ b/index.js @@ -158,13 +158,16 @@ var MongodbDriver = Base.extend({ * @param columns - The columns to add an index on * @param unique - A boolean whether this creates a unique index */ - addIndex: function(collectionName, indexName, columns, unique, callback) { + addIndex: function(collectionName, indexOptions, callback) { var options = { - indexName: indexName, - columns: columns, - unique: unique + name: indexOptions.name, + columns: indexOptions.columns, + unique: indexOptions.unique }; + if (indexOptions.hasOwnProperty('sparse')) { + options.sparse = indexOptions.sparse + } return this._run('createIndex', collectionName, options) .nodeify(callback); @@ -313,7 +316,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..8a4c2ae 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); From ce222c600089bba872974f7a29188fe6e52aeaef Mon Sep 17 00:00:00 2001 From: Ramakrishnan Date: Tue, 4 Oct 2016 13:14:39 +0530 Subject: [PATCH 2/5] Fix for addIndex backward compatibility --- index.js | 26 +++++++++++++++-------- test/mongodb_test.js | 49 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index a11c43a..a322c94 100644 --- a/index.js +++ b/index.js @@ -159,14 +159,24 @@ var MongodbDriver = Base.extend({ * @param unique - A boolean whether this creates a unique index */ addIndex: function(collectionName, indexOptions, callback) { - - var options = { - name: indexOptions.name, - columns: indexOptions.columns, - unique: indexOptions.unique - }; - if (indexOptions.hasOwnProperty('sparse')) { - options.sparse = indexOptions.sparse + 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 { + log.warn('addIndex can now accept Object for passing index Options.'); + options = { + name: arguments[1], + columns: arguments[2], + unique: arguments[3] + }; + callback = arguments[4]; } return this._run('createIndex', collectionName, options) diff --git a/test/mongodb_test.js b/test/mongodb_test.js index 8a4c2ae..a2393fa 100644 --- a/test/mongodb_test.js +++ b/test/mongodb_test.js @@ -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); From 7e797059a5377d64e1171a1b9be0d7cc355f977c Mon Sep 17 00:00:00 2001 From: Ramakrishnan Date: Fri, 7 Oct 2016 16:48:35 +0530 Subject: [PATCH 3/5] Adjusted @params tags --- index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index a322c94..88d8a3d 100644 --- a/index.js +++ b/index.js @@ -154,9 +154,8 @@ 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, indexOptions, callback) { var options = {}; From 7b0f4ad91bcfbf8d5c72ce8f4c87c2c6a158bd11 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Date: Thu, 13 Oct 2016 15:49:31 +0530 Subject: [PATCH 4/5] Review comments --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 88d8a3d..ba1117c 100644 --- a/index.js +++ b/index.js @@ -159,14 +159,14 @@ var MongodbDriver = Base.extend({ */ addIndex: function(collectionName, indexOptions, callback) { var options = {}; - if (indexOptions.constructor.name == 'Object') { + if (indexOptions.constructor.name === 'Object') { options = { name: indexOptions.name, columns: indexOptions.columns, unique: indexOptions.unique }; if (indexOptions.hasOwnProperty('sparse')) { - options.sparse = indexOptions.sparse + options.sparse = indexOptions.sparse; } } else { log.warn('addIndex can now accept Object for passing index Options.'); From f71fdc354a7d0d9c342b9990ca4683e77b94771e Mon Sep 17 00:00:00 2001 From: Ramakrishnan Date: Thu, 13 Oct 2016 15:54:44 +0530 Subject: [PATCH 5/5] Remove warning log --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index ba1117c..6e7e459 100644 --- a/index.js +++ b/index.js @@ -169,7 +169,6 @@ var MongodbDriver = Base.extend({ options.sparse = indexOptions.sparse; } } else { - log.warn('addIndex can now accept Object for passing index Options.'); options = { name: arguments[1], columns: arguments[2],