Skip to content

Commit 8406413

Browse files
committed
Adapt scroll to new Request refactor
2 parents 6d23b14 + 6211c10 commit 8406413

File tree

10 files changed

+82
-83
lines changed

10 files changed

+82
-83
lines changed

.eslintignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

.jshintrc

Lines changed: 0 additions & 13 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"scripts": {
2121
"test": "npm run --silent lint && npm run unit-testing",
2222
"unit-testing": "istanbul cover _mocha -- --recursive",
23-
"lint": "eslint --max-warnings=0 ./lib ./test",
23+
"lint": "eslint --max-warnings=0 ./src ./test",
2424
"build": "node build.js"
2525
},
2626
"browser": "src/kuzzle.js",
@@ -38,14 +38,8 @@
3838
"webpack": "^1.13.1",
3939
"eslint": "^3.7.1",
4040
"eslint-friendly-formatter": "^2.0.6",
41-
"eslint-loader": "^1.5.0",
4241
"browserify": "13.1.0",
4342
"codecov": "^1.0.1",
44-
"grunt": "1.0.1",
45-
"grunt-browserify": "^5.0.0",
46-
"grunt-contrib-uglify": "2.0.0",
47-
"grunt-webpack": "^1.0.14",
48-
"gruntify-eslint": "^3.0.0",
4943
"istanbul": "0.4.5",
5044
"istanbul-middleware": "0.2.2",
5145
"mocha": "3.1.0",

src/kuzzle.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var
2323
* @param {responseCallback} [cb] - Handles connection response
2424
* @constructor
2525
*/
26-
module.exports = Kuzzle = function (host, options, cb) {
26+
function Kuzzle (host, options, cb) {
2727
var self = this;
2828

2929
if (!(this instanceof Kuzzle)) {
@@ -309,7 +309,7 @@ module.exports = Kuzzle = function (host, options, cb) {
309309
}
310310
});
311311
}
312-
};
312+
}
313313

314314
/**
315315
* Connects to a Kuzzle instance using the provided host name.
@@ -1283,6 +1283,14 @@ Kuzzle.prototype.query = function (queryArgs, query, options, cb) {
12831283
object.refresh = options.refresh;
12841284
}
12851285

1286+
if (options.from) {
1287+
object.from = options.from;
1288+
}
1289+
1290+
if (options.size) {
1291+
object.size = options.size;
1292+
}
1293+
12861294
if (options.metadata) {
12871295
Object.keys(options.metadata).forEach(function (meta) {
12881296
object.metadata[meta] = options.metadata[meta];
@@ -1477,3 +1485,5 @@ Kuzzle.prototype.stopQueuing = function () {
14771485

14781486
return this;
14791487
};
1488+
1489+
module.exports = Kuzzle;

src/kuzzleDataCollection.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ var
1616
/**
1717
* A data collection is a set of data managed by Kuzzle. It acts like a data table for persistent documents,
1818
* or like a room for pub/sub messages.
19+
*
20+
* @property {string} collection
21+
* @property {string} index
22+
* @property {Kuzzle} kuzzle
23+
* @property {Array.<string>} collection
1924
* @param {object} kuzzle - Kuzzle instance to inherit from
2025
* @param {string} collection - name of the data collection to handle
2126
* @param {string} index - Index containing the data collection
@@ -289,25 +294,16 @@ KuzzleDataCollection.prototype.fetchAllDocuments = function (options, cb) {
289294
}
290295

291296
// copying pagination options to the search filter
292-
if (options && options.scroll) {
293-
filters.scroll = options.scroll;
294-
delete options.scroll;
297+
if (!options) {
298+
options = {};
295299
}
296300

297-
if (options && options.from) {
298-
filters.from = options.from;
299-
delete options.from;
300-
}
301-
else {
302-
filters.from = 0;
301+
if (!options.from) {
302+
options.from = 0;
303303
}
304304

305-
if (options && options.size) {
306-
filters.size = options.size;
307-
delete options.size;
308-
}
309-
else {
310-
filters.size = 1000;
305+
if (!options.size) {
306+
options.size = 1000;
311307
}
312308

313309
this.kuzzle.callbackRequired('KuzzleDataCollection.fetchAllDocuments', cb);
@@ -320,7 +316,7 @@ KuzzleDataCollection.prototype.fetchAllDocuments = function (options, cb) {
320316
if (searchResult instanceof KuzzleSearchResult) {
321317
if (searchResult.total > 10000 && !warnEmitted) {
322318
warnEmitted = true;
323-
console.warn('Usage of KuzzleDataCollection.fetchAllDocuments will fetch more than 10 000 document. To avoid performance issues, please use KuzzleDataCollection.search and KuzzleDataCollection.scroll requests')
319+
console.warn('Usage of KuzzleDataCollection.fetchAllDocuments will fetch more than 10 000 document. To avoid performance issues, please use KuzzleDataCollection.search and KuzzleDataCollection.scroll requests'); // eslint-disable-line no-console
324320
}
325321

326322
searchResult.documents.forEach(document => {
@@ -505,18 +501,18 @@ KuzzleDataCollection.prototype.scroll = function (scrollId, options, filters, cb
505501

506502
if (!cb && typeof options === 'function') {
507503
cb = options;
508-
options = null;
504+
options = {};
509505
}
510506

511-
request.body.scrollId = scrollId;
512-
513-
if (options && options.scroll) {
514-
request.body.scroll = options.scroll;
507+
if (!options) {
508+
options = {};
515509
}
516510

511+
options.scrollId = scrollId;
512+
517513
this.kuzzle.callbackRequired('KuzzleDataCollection.scroll', cb);
518514

519-
this.kuzzle.query({controller: 'read', action: 'scroll'}, request, options, function (error, result) {
515+
this.kuzzle.query({controller: 'document', action: 'scroll'}, request, options, function (error, result) {
520516
var documents = [];
521517

522518
if (error) {
@@ -532,7 +528,7 @@ KuzzleDataCollection.prototype.scroll = function (scrollId, options, filters, cb
532528
});
533529

534530
if (result.result._scroll_id) {
535-
filters.scrollId = result.result._scroll_id;
531+
options.scrollId = result.result._scroll_id;
536532
}
537533

538534
cb(null, new KuzzleSearchResult(

src/kuzzleSearchResult.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
var KuzzleDocument = require('./kuzzleDocument.js');
2-
31
/**
42
*
53
* @param {KuzzleDataCollection} dataCollection
@@ -83,18 +81,14 @@ KuzzleSearchResult.prototype.previous = function (cb) {
8381
* @param {function} cb
8482
*/
8583
KuzzleSearchResult.prototype.next = function (cb) {
86-
var self = this;
84+
var
85+
filters,
86+
options = Object.assign({}, this.searchArgs.options),
87+
self = this;
8788

8889
if (!this._next) {
8990
// retrieve next results with scroll if original search use it
90-
if (this.searchArgs.options.scrollId) {
91-
var
92-
options = Object.assign({}, this.searchArgs.options);
93-
94-
if (this.searchArgs.filters.scroll) {
95-
options.scroll = this.searchArgs.filters.scroll;
96-
}
97-
91+
if (options.scrollId) {
9892
if (this.fetchedDocument >= this.total) {
9993
cb(null, null);
10094
return;
@@ -105,31 +99,30 @@ KuzzleSearchResult.prototype.next = function (cb) {
10599
options,
106100
this.searchArgs.filters || {},
107101
function(error, newSearchResults) {
108-
handleNextSearchResults(error, self, newSearchResults, cb)
102+
handleNextSearchResults(error, self, newSearchResults, cb);
109103
}
110104
);
111105

112106
return;
113107
}
114-
// retrieve next results with from/size if original search use it
115-
else if (this.searchArgs.filters.from !== undefined && this.searchArgs.filters.size !== undefined) {
116-
var
117-
filters = Object.assign({}, this.searchArgs.filters);
108+
// retrieve next results with from/size if original search use it
109+
else if (options.from !== undefined && options.size !== undefined) {
110+
filters = Object.assign({}, this.searchArgs.filters);
118111

119112
// check if we need to do next request to fetch all matching documents
120-
filters.from += filters.size;
113+
options.from += options.size;
121114

122-
if (filters.from >= this.total) {
115+
if (options.from >= this.total) {
123116
cb(null, null);
124117

125118
return;
126119
}
127120

128121
this.dataCollection.search(
129122
filters,
130-
this.searchArgs.options,
123+
options,
131124
function(error, newSearchResults) {
132-
handleNextSearchResults(error, self, newSearchResults, cb)
125+
handleNextSearchResults(error, self, newSearchResults, cb);
133126
}
134127
);
135128

test/kuzzle/methods.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,12 @@ describe('Kuzzle methods', function () {
345345
});
346346

347347
it('should handle from option correctly', function (done) {
348-
expectedQuery.body.from = 'foobar';
348+
expectedQuery.from = 'foobar';
349349
kuzzle.listCollections('foo', {from: 'foobar'}, () => done());
350350
});
351351

352352
it('should handle size option correctly', function (done) {
353-
expectedQuery.body.size = 'foobar';
353+
expectedQuery.size = 'foobar';
354354
kuzzle.listCollections('foo', {size: 'foobar'}, () => done());
355355
});
356356

test/kuzzle/query.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,22 @@ describe('Query management', function () {
172172
should(requestObject.refresh).match(refresh);
173173
});
174174

175+
it('should handle option size properly', function () {
176+
var
177+
size = 'foo';
178+
179+
kuzzle.query(queryArgs, { body: { some: 'query'}}, {size: size});
180+
should(requestObject.size).match(size);
181+
});
182+
183+
it('should handle option from properly', function () {
184+
var
185+
from = 'foo';
186+
187+
kuzzle.query(queryArgs, { body: { some: 'query'}}, {from: from});
188+
should(requestObject.from).match(from);
189+
});
190+
175191
it('should exit early if the query is not queuable and the SDK is offline', function () {
176192
kuzzle.state = 'offline';
177193
kuzzle.query(queryArgs, { body: { some: 'query'}}, {queuable: false});

test/kuzzleDataCollection/methods.test.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe('KuzzleDataCollection methods', function () {
139139
index: 'bar',
140140
collection: 'foo',
141141
action: 'scroll',
142-
controller: 'read',
142+
controller: 'document',
143143
body: {}
144144
};
145145
});
@@ -166,11 +166,10 @@ describe('KuzzleDataCollection methods', function () {
166166
};
167167

168168
queryScrollStub = function (args, query, opts, callback) {
169-
should(args.controller).be.exactly('read');
169+
should(args.controller).be.exactly('document');
170170
should(args.action).be.exactly('scroll');
171-
should(query.body.scroll).be.exactly(options.scroll);
172-
should(query.body.scrollId).be.exactly(scrollId);
173-
should(opts).be.exactly(options);
171+
should(opts.scroll).be.exactly(options.scroll);
172+
should(opts.scrollId).be.exactly(scrollId);
174173

175174
callback(null, {
176175
result: {
@@ -201,10 +200,9 @@ describe('KuzzleDataCollection methods', function () {
201200
};
202201

203202
queryScrollStub = function (args, query, opts, callback) {
204-
should(args.controller).be.exactly('read');
203+
should(args.controller).be.exactly('document');
205204
should(args.action).be.exactly('scroll');
206-
should(query.body.scrollId).be.exactly(scrollId);
207-
should(opts).be.null();
205+
should(opts.scrollId).be.exactly(scrollId);
208206

209207
callback(null, {
210208
result: {
@@ -601,6 +599,13 @@ describe('KuzzleDataCollection methods', function () {
601599
beforeEach(function () {
602600
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
603601
emitted = false;
602+
expectedQuery = {
603+
index: 'bar',
604+
collection: 'foo',
605+
action: 'get',
606+
controller: 'document',
607+
body: {}
608+
};
604609
});
605610

606611
it('should forward the query to the search method', function () {
@@ -652,7 +657,7 @@ describe('KuzzleDataCollection methods', function () {
652657

653658
collection.fetchAllDocuments({from: 123, size: 456}, function () {});
654659
should(stub.calledOnce).be.true();
655-
should(stub.calledWithMatch({from: 123, size: 456})).be.true();
660+
should(stub.calledWithMatch({}, {from: 123, size: 456})).be.true();
656661
stub.restore();
657662
});
658663

@@ -663,7 +668,7 @@ describe('KuzzleDataCollection methods', function () {
663668

664669
collection.fetchAllDocuments({scroll: '30s'}, function () {});
665670
should(stub.calledOnce).be.true();
666-
should(stub.calledWithMatch({from: 0, size: 1000, scroll: '30s'})).be.true();
671+
should(stub.calledWithMatch({}, {from: 0, size: 1000, scroll: '30s'})).be.true();
667672
stub.restore();
668673
});
669674

test/kuzzleSearchResult/methods.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('KuzzleSearchResult methods', function () {
2020

2121
beforeEach(function () {
2222
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
23-
searchArgs = {options: {}, filters: {from:0, size: 1}};
23+
searchArgs = {options: {from:0, size: 1}, filters: {}};
2424
dataCollection = kuzzle.dataCollectionFactory('foo');
2525
firstDocument = new KuzzleDocument(dataCollection, 'banana', {foo: 'bar'});
2626
secondDocument = new KuzzleDocument(dataCollection, 'papagayo', {foo: 'bar'});
@@ -47,7 +47,7 @@ describe('KuzzleSearchResult methods', function () {
4747
1,
4848
[new KuzzleDocument(dataCollection, 'papagayo', {foo: 'bar'})],
4949
{},
50-
{options: {scrollId: 'papagayo'}, filters: {from: 0, size: 1}}
50+
{options: {scrollId: 'papagayo', from: 0, size: 1}}
5151
),
5252
firstSearchResult;
5353

@@ -101,7 +101,7 @@ describe('KuzzleSearchResult methods', function () {
101101
should(result).be.an.instanceOf(KuzzleSearchResult);
102102
should(result.documents).be.an.Array();
103103
should(result.documents.length).be.exactly(1);
104-
should(result.searchArgs.filters.from).be.exactly(1);
104+
should(result.searchArgs.options.from).be.exactly(1);
105105
done();
106106
});
107107
});
@@ -151,7 +151,7 @@ describe('KuzzleSearchResult methods', function () {
151151
var
152152
firstSearchResult;
153153

154-
searchArgs.filters = {};
154+
searchArgs.options = {};
155155

156156
firstSearchResult = new KuzzleSearchResult(dataCollection, 1, [firstDocument], {}, searchArgs);
157157

0 commit comments

Comments
 (0)