Skip to content

Fir for Add sparse options for index #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,29 @@ 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') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason for not using typeof?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please always use type safe comparisions and only use non type safe where you really need to fallback.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeof [] is Object and typeof {} is also Object. Hence constructor.name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not enough of a reason for me though. [] in javascript is an object just as {} is, the only difference are some specifics, but even with those an array is still an object in javascript, though.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, but for this case we expect the options to be of type {}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still would go for typeof instead, or can you call out a case where a passed array would result in an unwanted behavior?

options = {
name: indexOptions.name,
columns: indexOptions.columns,
unique: indexOptions.unique
};
if (indexOptions.hasOwnProperty('sparse')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the benefit over indexOptions.sparse here? As what I can see here, it is just a simple object being passed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indexOptions.sparse can only be true or false, this option will be considered only if indexOption contains a property sparse. As if(false) will be false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, lets be a bit more specific: What is the default value of sparse? If it is false, then it can just be omitted. If the default is true, well then please check for indexOptions.sparse === false instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sparse itself is optional parameter for addIndex, hence we should use the value as set by user. Hence the presence of sparse is checked before passing them to createIndex call.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But sparse itself does only accept true or false itself. So I would rather check for the non default value in a type safe manner instead of doing hasOwnProperty.

https://docs.mongodb.com/manual/core/index-sparse/#create-a-sparse-index

As false is the default, we can just safely omit it in the case of the value being non true.

options.sparse = indexOptions.sparse
}
} else {
log.warn('addIndex can now accept Object for passing index Options.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove that log, this is an extension of the API not a deprecation though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add, this information is more a target for documentation rather than adding some logs here :)

That piece of information will go here https://github.com/db-migrate/english-docs/blob/master/API/NoSQL.md#addindexcollectionname-indexname-columns-unique-callback

options = {
name: arguments[1],
columns: arguments[2],
unique: arguments[3]
};
callback = arguments[4];
}

return this._run('createIndex', collectionName, options)
.nodeify(callback);
Expand Down Expand Up @@ -313,7 +325,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);
Expand Down
55 changes: 51 additions & 4 deletions test/mongodb_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

}.bind(this));
},

Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last thing missing here: You have deleted the old tests, but to actually cover the old and new functionality, the old and the new tests have to coexist, rather than one gets rewritten.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But these are internal calls made within the test case hence I changed them to use the updates style. I have also added a separate test case for addIndex for the previous style.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this wasn't clear from the diff. Please don't adjust tests in the future and instead add directly new ones instead :)


if(err) {
return this.callback(err);
Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here


if(err) {
return this.callback(err);
Expand Down Expand Up @@ -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);