Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 7 additions & 8 deletions src/kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -873,20 +873,19 @@ Kuzzle.prototype.getStatistics = function (timestamp, options, cb) {
* Create a new instance of a KuzzleDataCollection object.
* If no index is specified, takes the default index.
*
* @param {string} [index] - The name of the data index containing the data collection
* @param {string} collection - The name of the data collection you want to manipulate
* @param {string} [index] - The name of the data index containing the data collection
* @returns {object} A KuzzleDataCollection instance
*/
Kuzzle.prototype.dataCollectionFactory = function(index, collection) {
Kuzzle.prototype.dataCollectionFactory = function(collection, index) {
this.isValid();

if (arguments.length === 1) {
collection = arguments[0];
index = this.defaultIndex;
}

if (!index) {
throw new Error('Unable to create a new data collection object: no index specified');
if (!this.defaultIndex) {
throw new Error('Unable to create a new data collection object: no index specified');
}

index = this.defaultIndex;
}

if (typeof index !== 'string') {
Expand Down
18 changes: 6 additions & 12 deletions test/kuzzle/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ describe('Kuzzle methods', function () {
it('should throw an error if arguments are not strings', () => {
kuzzle.defaultIndex = 'foobar';
should(function () { kuzzle.dataCollectionFactory(undefined); }).throw(/string expected/);
should(function () { kuzzle.dataCollectionFactory('foo', undefined); }).throw(/string expected/);
should(function () { kuzzle.dataCollectionFactory(undefined, 'foo'); }).throw(/string expected/);
should(function () { kuzzle.dataCollectionFactory(null); }).throw(/string expected/);
should(function () { kuzzle.dataCollectionFactory('foo', null); }).throw(/string expected/);
should(function () { kuzzle.dataCollectionFactory(null, 'foo'); }).throw(/string expected/);
should(function () { kuzzle.dataCollectionFactory(123); }).throw(/string expected/);
should(function () { kuzzle.dataCollectionFactory(123, 'foo'); }).throw(/string expected/);
should(function () { kuzzle.dataCollectionFactory('foo', 123); }).throw(/string expected/);
Expand All @@ -203,13 +203,13 @@ describe('Kuzzle methods', function () {
it('should create and store the data collection instance if needed', function () {
var collection = kuzzle.dataCollectionFactory('foo', 'bar');

should(kuzzle.collections['foo']['bar']).not.be.undefined().and.be.instanceof(KuzzleDataCollection);
should(kuzzle.collections['bar']['foo']).not.be.undefined().and.be.instanceof(KuzzleDataCollection);
should(collection).be.instanceof(KuzzleDataCollection);
});

it('should simply pull the collection from the collection history if reinvoked', function () {
kuzzle.collections['foo'] = { bar: 'qux'};
should(kuzzle.dataCollectionFactory('foo', 'bar')).be.a.String().and.be.exactly('qux');
should(kuzzle.dataCollectionFactory('bar', 'foo')).be.a.String().and.be.exactly('qux');
});

it('should use the default index if no index is provided', function () {
Expand All @@ -223,14 +223,8 @@ describe('Kuzzle methods', function () {
should(collection.index).be.eql(defaultIndex);
});

it('should throw an error if no index is provided and no default index has been set', function (done) {
try {
kuzzle.dataCollectionFactory('foo');
done(new Error('Should have thrown an error'));
}
catch (e) {
done();
}
it('should throw an error if no index is provided and no default index has been set', () => {
should(function () { kuzzle.dataCollectionFactory('foo'); }).throw(/no index specified/);
});
});

Expand Down