Skip to content

Commit 8a3f5d4

Browse files
committed
Extended skiplist/hash index opts. Fixes #55.
1 parent 199bb1a commit 8a3f5d4

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,14 +1560,16 @@ db.createCollection('some-collection', function (err, collection) {
15601560

15611561
#### collection.createHashIndex
15621562

1563-
`collection.createHashIndex(fields: Array<string> | string, [unique: boolean,] [callback: Callback]): Promise<Index>`
1563+
`collection.createHashIndex(fields: Array<string> | string, [opts: Object | boolean,] [callback: Callback]): Promise<Index>`
15641564

15651565
Creates a hash index on the collection.
15661566

15671567
*Parameter*
15681568

15691569
* *fields*: an array of document fields on which to create the index.
1570-
* *unique* (optional): whether to constrain the fields to unique values. Default: `false`.
1570+
* *opts* (optional): additional options for this index.
1571+
1572+
If *opts* is a boolean, it will be interpreted as *opts.unique*.
15711573

15721574
If *fields* is a string, it will be wrapped in an array automatically.
15731575

@@ -1597,14 +1599,16 @@ db.createCollection('some-collection', function (err, collection) {
15971599

15981600
#### collection.createSkipList
15991601

1600-
`collection.createSkipList(fields: Array<string> | string, [unique: boolean,] [callback: Callback]): Promise<Index>`
1602+
`collection.createSkipList(fields: Array<string> | string, [opts: Object | boolean,] [opts: Object,] [callback: Callback]): Promise<Index>`
16011603

16021604
Creates a skiplist index on the collection.
16031605

16041606
*Parameter*
16051607

16061608
* *fields*: an array of document fields on which to create the index.
1607-
* *unique* (optional): whether to constrain the fields to unique values. Default: `false`.
1609+
* *opts* (optional): additional options for this index.
1610+
1611+
If *opts* is a boolean, it will be interpreted as *opts.unique*.
16081612

16091613
If *fields* is a string, it will be wrapped in an array automatically.
16101614

src/collection.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -261,39 +261,39 @@ extend(BaseCollection.prototype, {
261261
});
262262
return promise;
263263
},
264-
createHashIndex(fields, unique, cb) {
265-
if (typeof unique === 'function') {
266-
cb = unique;
267-
unique = undefined;
264+
createHashIndex(fields, opts, cb) {
265+
if (typeof opts === 'function') {
266+
cb = opts;
267+
opts = undefined;
268268
}
269269
if (typeof fields === 'string') {
270270
fields = [fields];
271271
}
272+
if (typeof opts === 'boolean') {
273+
opts = {unique: opts};
274+
}
272275
var {promise, callback} = this._connection.promisify(cb);
273-
this._api.post('index', {
274-
type: 'hash',
275-
fields: fields,
276-
unique: Boolean(unique)
277-
}, {collection: this.name}, function (err, res) {
276+
opts = extend({unique: false}, opts, {type: 'hash', fields: fields});
277+
this._api.post('index', opts, {collection: this.name}, function (err, res) {
278278
if (err) callback(err);
279279
else callback(null, res.body);
280280
});
281281
return promise;
282282
},
283-
createSkipList(fields, unique, cb) {
284-
if (typeof unique === 'function') {
285-
cb = unique;
286-
unique = undefined;
283+
createSkipList(fields, opts, cb) {
284+
if (typeof opts === 'function') {
285+
cb = opts;
286+
opts = undefined;
287287
}
288288
if (typeof fields === 'string') {
289289
fields = [fields];
290290
}
291+
if (typeof opts === 'boolean') {
292+
opts = {unique: opts};
293+
}
291294
var {promise, callback} = this._connection.promisify(cb);
292-
this._api.post('index', {
293-
type: 'skiplist',
294-
fields: fields,
295-
unique: Boolean(unique)
296-
}, {collection: this.name}, function (err, res) {
295+
opts = extend({unique: false}, opts, {type: 'skiplist', fields: fields});
296+
this._api.post('index', opts, {collection: this.name}, function (err, res) {
297297
if (err) callback(err);
298298
else callback(null, res.body);
299299
});

0 commit comments

Comments
 (0)