Skip to content

Commit 92e9047

Browse files
committed
added listIndexes and getServerInfo routes
1 parent 7ac8159 commit 92e9047

File tree

4 files changed

+217
-16
lines changed

4 files changed

+217
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

src/kuzzle.js

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ module.exports = Kuzzle = function (url, options, cb) {
254254
return this.bluebird.promisifyAll(this, {
255255
suffix: 'Promise',
256256
filter: function (name, func, target, passes) {
257-
var whitelist = ['getAllStatistics', 'getStatistics', 'listCollections', 'now', 'query'];
257+
var whitelist = ['getAllStatistics', 'getServerInfo', 'getStatistics', 'listCollections', 'listIndexes', 'now', 'query'];
258258

259259
return passes && whitelist.indexOf(name) !== -1;
260260
}
@@ -686,16 +686,39 @@ Kuzzle.prototype.flushQueue = function () {
686686
/**
687687
* Returns the list of known persisted data collections.
688688
*
689+
* @param {string} [index] - Index containing collections to be listed
689690
* @param {object} [options] - Optional parameters
690691
* @param {responseCallback} cb - Handles the query response
691692
* @returns {object} this
692693
*/
693-
Kuzzle.prototype.listCollections = function (options, cb) {
694-
var collectionType = 'all';
694+
Kuzzle.prototype.listCollections = function () {
695+
var
696+
collectionType = 'all',
697+
index,
698+
options,
699+
cb,
700+
args = Array.prototype.slice.call(arguments);
701+
702+
args.forEach(function(arg) {
703+
switch (typeof arg) {
704+
case 'string':
705+
index = arg;
706+
break;
707+
case 'object':
708+
options = arg;
709+
break;
710+
case 'function':
711+
cb = arg;
712+
break;
713+
}
714+
});
695715

696-
if (!cb && typeof options === 'function') {
697-
cb = options;
698-
options = null;
716+
if (!index) {
717+
if (!this.defaultIndex) {
718+
throw new Error('Kuzzle.listCollections: index required');
719+
}
720+
721+
index = this.defaultIndex;
699722
}
700723

701724
this.callbackRequired('Kuzzle.listCollections', cb);
@@ -704,7 +727,7 @@ Kuzzle.prototype.listCollections = function (options, cb) {
704727
collectionType = options.type;
705728
}
706729

707-
this.query({controller: 'read', action: 'listCollections'}, {body: {type: collectionType}}, options, function (err, res) {
730+
this.query({index: index, controller: 'read', action: 'listCollections'}, {body: {type: collectionType}}, options, function (err, res) {
708731
if (err) {
709732
return cb(err);
710733
}
@@ -715,6 +738,32 @@ Kuzzle.prototype.listCollections = function (options, cb) {
715738
return this;
716739
};
717740

741+
/**
742+
* Returns the list of existing indexes in Kuzzle
743+
*
744+
* @param {object} [options] - Optional arguments
745+
* @param {responseCallback} cb - Handles the query response
746+
* @returns {object} this
747+
*/
748+
Kuzzle.prototype.listIndexes = function (options, cb) {
749+
if (!cb && typeof options === 'function') {
750+
cb = options;
751+
options = null;
752+
}
753+
754+
this.callbackRequired('Kuzzle.listIndexes', cb);
755+
756+
this.query({controller: 'read', action: 'listIndexes'}, {}, options, function (err, res) {
757+
if (err) {
758+
return cb(err);
759+
}
760+
761+
return cb(null, res.result.indexes);
762+
});
763+
764+
return this;
765+
};
766+
718767
/**
719768
* Disconnects from Kuzzle and invalidate this instance.
720769
*/
@@ -734,6 +783,32 @@ Kuzzle.prototype.disconnect = function () {
734783
}
735784
};
736785

786+
/**
787+
* Returns the server informations
788+
*
789+
* @param {object} [options] - Optional arguments
790+
* @param {responseCallback} cb - Handles the query response
791+
* @returns {object} this
792+
*/
793+
Kuzzle.prototype.getServerInfo = function (options, cb) {
794+
if (!cb && typeof options === 'function') {
795+
cb = options;
796+
options = null;
797+
}
798+
799+
this.callbackRequired('Kuzzle.getServerInfo', cb);
800+
801+
this.query({controller: 'read', action: 'serverInfo'}, {}, options, function (err, res) {
802+
if (err) {
803+
return cb(err);
804+
}
805+
806+
cb(null, res.result.serverInfo);
807+
});
808+
809+
return this;
810+
};
811+
737812
/**
738813
* Return the current Kuzzle's UTC Epoch time, in milliseconds
739814
* @param {object} [options] - Optional parameters
@@ -907,7 +982,6 @@ Kuzzle.prototype.replayQueue = function () {
907982
return this;
908983
};
909984

910-
911985
/**
912986
* Sets the default Kuzzle index
913987
*

test/kuzzle/constructor.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,10 @@ describe('Kuzzle constructor', () => {
176176
should.not.exist(kuzzle.dataCollectionFactoryPromise);
177177
should.not.exist(kuzzle.flushQueuePromise);
178178
should.exist(kuzzle.getAllStatisticsPromise);
179+
should.exist(kuzzle.getServerInfoPromise);
179180
should.exist(kuzzle.getStatisticsPromise);
180181
should.exist(kuzzle.listCollectionsPromise);
182+
should.exist(kuzzle.listIndexesPromise);
181183
should.not.exist(kuzzle.logoutPromise);
182184
should.exist(kuzzle.nowPromise);
183185
should.exist(kuzzle.queryPromise);

test/kuzzle/methods.test.js

Lines changed: 132 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Kuzzle methods', function () {
1313
queryStub = function (args, query, options, cb) {
1414
emitted = true;
1515
should(args.collection).be.undefined();
16-
should(args.index).be.undefined();
16+
should(args.index).be.eql(expectedQuery.index);
1717
should(args.controller).be.exactly(expectedQuery.controller);
1818
should(args.action).be.exactly(expectedQuery.action);
1919

@@ -207,6 +207,70 @@ describe('Kuzzle methods', function () {
207207
});
208208
});
209209

210+
describe('#getServerInfo', function () {
211+
beforeEach(function () {
212+
kuzzle = new Kuzzle('foo');
213+
kuzzle.query = queryStub;
214+
emitted = false;
215+
passedOptions = null;
216+
error = null;
217+
result = {result: {serverInfo: {
218+
kuzzle:
219+
{ version: '0.9.2',
220+
api: { version: '1.0', routes: {} },
221+
nodeVersion: 'v4.2.1',
222+
memoryUsed: 100020224,
223+
uptime: '161089.628s',
224+
plugins:
225+
{ 'kuzzle-plugin-logger': {},
226+
'kuzzle-plugin-socketio': {},
227+
'kuzzle-plugin-auth-passport-local': {} },
228+
system: { memory: {}, cpus: {} } },
229+
services: {
230+
writeEngine: {
231+
type: 'elasticsearch',
232+
api: '1.7',
233+
version: '1.5.2',
234+
lucene: '4.10.4',
235+
status: 'red',
236+
nodes: {},
237+
spaceUsed: '14.5kb'
238+
}
239+
}
240+
}}};
241+
expectedQuery = {
242+
controller: 'read',
243+
action: 'serverInfo'
244+
};
245+
});
246+
247+
it('should behave correctly when invoked', function () {
248+
should(kuzzle.getServerInfo(function (err, res) {
249+
should(err).be.null();
250+
should(res).be.eql(result.result.serverInfo);
251+
})).be.eql(kuzzle);
252+
});
253+
254+
it('should throw an error if no callback is provided', function () {
255+
should(function () {kuzzle.getServerInfo()}).throw(Error);
256+
should(emitted).be.false();
257+
258+
should(function () {kuzzle.getServerInfo({some: 'options'})}).throw(Error);
259+
should(emitted).be.false();
260+
});
261+
262+
it('should execute the callback with an error if one occurs', function (done) {
263+
this.timeout(50);
264+
error = 'foobar';
265+
266+
kuzzle.getServerInfo('foo', function (err, res) {
267+
should(err).be.exactly('foobar');
268+
should(res).be.undefined();
269+
done();
270+
});
271+
});
272+
});
273+
210274
describe('#listCollections', function () {
211275
beforeEach(function () {
212276
kuzzle = new Kuzzle('foo');
@@ -216,28 +280,36 @@ describe('Kuzzle methods', function () {
216280
error = null;
217281
result = {result: {collections: {stored: [], realtime: []}}};
218282
expectedQuery = {
283+
index: 'foo',
219284
controller: 'read',
220285
action: 'listCollections',
221286
body: {type: 'all'}
222287
};
223288
});
224289

225290
it('should return the kuzzle instance when called', function () {
226-
should(kuzzle.listCollections(function () {})).be.exactly(kuzzle);
291+
should(kuzzle.listCollections('foo', function () {})).be.exactly(kuzzle);
227292
});
228293

229294
it('should throw an error if no callback has been provided', function () {
230-
should(function () { kuzzle.listCollections(); }).throw(Error);
295+
should(function () { kuzzle.listCollections('foo'); }).throw(Error);
231296
should(emitted).be.false();
232-
should(function () { kuzzle.listCollections({}); }).throw(Error);
297+
should(function () { kuzzle.listCollections('foo', {}); }).throw(Error);
298+
should(emitted).be.false();
299+
});
300+
301+
it('should throw an error if no index has been provided', function () {
302+
should(function () {kuzzle.listCollections(function () {})}).throw(Error);
303+
should(emitted).be.false();
304+
should(function () {kuzzle.listCollections({}, function () {})}).throw(Error);
233305
should(emitted).be.false();
234306
});
235307

236308
it('should call query with the right arguments', function (done) {
237309
this.timeout(50);
238310
result = { result: {collections: {stored: ['foo', 'bar', 'baz'], realtime: ['qux'] } } };
239311

240-
kuzzle.listCollections(function (err, res) {
312+
kuzzle.listCollections('foo', function (err, res) {
241313
should(err).be.null();
242314
should(res).be.an.Object().and.match(result.result.collections);
243315
done();
@@ -248,7 +320,7 @@ describe('Kuzzle methods', function () {
248320
this.timeout(50);
249321
error = 'foobar';
250322

251-
kuzzle.listCollections(function (err, res) {
323+
kuzzle.listCollections('foo', function (err, res) {
252324
should(err).be.exactly('foobar');
253325
should(res).be.undefined();
254326
done();
@@ -257,8 +329,61 @@ describe('Kuzzle methods', function () {
257329

258330
it('should handle options correctly', function (done) {
259331
expectedQuery.body.type = 'foobar';
260-
kuzzle.listCollections({type: 'foobar'}, () => done());
332+
kuzzle.listCollections('foo', {type: 'foobar'}, () => done());
333+
});
334+
335+
it('should use the default index if none is provided', function () {
336+
kuzzle.setDefaultIndex('foo');
337+
should(kuzzle.listCollections(function () {})).be.exactly(kuzzle);
338+
should(emitted).be.true();
339+
340+
emitted = false;
341+
should(kuzzle.listCollections({some: 'options'}, function () {})).be.exactly(kuzzle);
342+
should(emitted).be.true();
343+
});
344+
});
345+
346+
describe('#listIndexes', function () {
347+
beforeEach(function () {
348+
kuzzle = new Kuzzle('foo');
349+
kuzzle.query = queryStub;
350+
emitted = false;
351+
passedOptions = null;
352+
error = null;
353+
result = {result: {indexes: ['foo', 'bar']}};
354+
expectedQuery = {
355+
controller: 'read',
356+
action: 'listIndexes'
357+
};
261358
});
359+
360+
it('should return the kuzzle instance when called', function () {
361+
should(kuzzle.listIndexes(function (err, res) {
362+
should(err).be.null();
363+
should(res).be.eql(result.result.indexes);
364+
})).be.eql(kuzzle);
365+
});
366+
367+
it('should throw an error if no callback is provided', function () {
368+
should(function () { kuzzle.listIndexes(); }).throw(Error);
369+
should(emitted).be.false();
370+
371+
should(function () {kuzzle.listIndexes({some: 'options'})}).throw(Error);
372+
should(emitted).be.false();
373+
});
374+
375+
it('should execute the callback with an error if one occurs', function (done) {
376+
this.timeout(50);
377+
error = 'foobar';
378+
379+
kuzzle.listIndexes(function (err, res) {
380+
should(err).be.exactly('foobar');
381+
should(res).be.undefined();
382+
done();
383+
});
384+
});
385+
386+
262387
});
263388

264389
describe('#disconnect', function () {

0 commit comments

Comments
 (0)