Skip to content

Commit caebd3e

Browse files
author
David Bengsch
authored
Merge pull request #190 from kuzzleio/topic-18-align-search-result
Align SearchResult and scroll with other SDKs
2 parents 6c3eee3 + e5d7801 commit caebd3e

File tree

20 files changed

+315
-237
lines changed

20 files changed

+315
-237
lines changed

src/Collection.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var
33
Document = require('./Document'),
44
CollectionMapping = require('./CollectionMapping'),
55
Room = require('./Room'),
6-
KuzzleSubscribeResult = require('./SubscribeResult');
6+
SubscribeResult = require('./SubscribeResult');
77

88
/**
99
* This is a global callback pattern, called by all asynchronous functions of the Kuzzle object.
@@ -314,7 +314,7 @@ Collection.prototype.fetchAllDocuments = function (options, cb) {
314314

315315
this.kuzzle.callbackRequired('Collection.fetchAllDocuments', cb);
316316

317-
this.search(filters, options, function getNextDocuments (error, searchResult) {
317+
this.search(filters, options, function fetchNextDocuments (error, searchResult) {
318318
if (error) {
319319
return cb(error);
320320
}
@@ -328,7 +328,7 @@ Collection.prototype.fetchAllDocuments = function (options, cb) {
328328
searchResult.documents.forEach(function(document) {
329329
documents.push(document);
330330
});
331-
searchResult.next(getNextDocuments);
331+
searchResult.fetchNext(fetchNextDocuments);
332332
}
333333
else {
334334
cb(null, documents);
@@ -477,8 +477,9 @@ Collection.prototype.search = function (filters, options, cb) {
477477
self,
478478
result.result.total,
479479
documents,
480-
result.result.aggregations ? result.result.aggregations : [],
481-
{options: options, filters: filters},
480+
result.result.aggregations ? result.result.aggregations : {},
481+
options,
482+
filters,
482483
options.previous || null
483484
));
484485
});
@@ -545,8 +546,9 @@ Collection.prototype.scroll = function (scrollId, options, filters, cb) {
545546
self,
546547
result.result.total,
547548
documents,
548-
result.result.aggregations ? result.result.aggregations : [],
549-
{options: options, filters: filters},
549+
{},
550+
options,
551+
filters,
550552
options.previous || null
551553
));
552554
});
@@ -575,7 +577,7 @@ Collection.prototype.subscribe = function (filters, options, cb) {
575577

576578
this.kuzzle.callbackRequired('Collection.subscribe', cb);
577579

578-
subscribeResult = new KuzzleSubscribeResult();
580+
subscribeResult = new SubscribeResult();
579581
room = new Room(this, options);
580582

581583
room.renew(filters, cb, subscribeResult.done.bind(subscribeResult));

src/Document.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@
1515
* - providing a documentID to the constructor will automatically call refresh, unless a content is also provided
1616
*
1717
*
18-
* @param {object} kuzzleDataCollection - an instanciated Collection object
18+
* @param {Collection} collection - an instanciated Collection object
1919
* @param {string} [documentId] - ID of an existing document
2020
* @param {object} [content] - Initializes this document with the provided content
2121
* @constructor
2222
*/
23-
function Document(kuzzleDataCollection, documentId, content) {
23+
function Document(collection, documentId, content) {
2424
Object.defineProperties(this, {
2525
// read-only properties
2626
collection: {
27-
value: kuzzleDataCollection.collection,
27+
value: collection.collection,
2828
enumerable: true
2929
},
3030
dataCollection: {
31-
value: kuzzleDataCollection,
31+
value: collection,
3232
enumerable: false
3333
},
3434
kuzzle: {
35-
value: kuzzleDataCollection.kuzzle,
35+
value: collection.kuzzle,
3636
enumerable: false
3737
},
3838
// writable properties
@@ -47,7 +47,7 @@ function Document(kuzzleDataCollection, documentId, content) {
4747
enumerable: true
4848
},
4949
headers: {
50-
value: JSON.parse(JSON.stringify(kuzzleDataCollection.headers)),
50+
value: JSON.parse(JSON.stringify(collection.headers)),
5151
enumerable: true,
5252
writable: true
5353
},

src/Room.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ var
1919
* Once you have subscribed, if a pub/sub message is published matching your filters, or if a matching stored
2020
* document change (because it is created, updated or deleted), then you’ll receive a notification about it.
2121
*
22-
* @param {object} kuzzleDataCollection - an instantiated and valid kuzzle object
22+
* @param {object} collection - an instantiated and valid kuzzle object
2323
* @param {object} [options] - subscription optional configuration
2424
* @constructor
2525
*/
26-
function Room(kuzzleDataCollection, options) {
26+
function Room(collection, options) {
2727
// Define properties
2828
Object.defineProperties(this, {
2929
// private properties
@@ -73,11 +73,11 @@ function Room(kuzzleDataCollection, options) {
7373
},
7474
// read-only properties
7575
collection: {
76-
value: kuzzleDataCollection,
76+
value: collection,
7777
enumerable: true
7878
},
7979
kuzzle: {
80-
value: kuzzleDataCollection.kuzzle,
80+
value: collection.kuzzle,
8181
enumerable: true
8282
},
8383
// writable properties
@@ -87,7 +87,7 @@ function Room(kuzzleDataCollection, options) {
8787
writable: true
8888
},
8989
headers: {
90-
value: JSON.parse(JSON.stringify(kuzzleDataCollection.headers)),
90+
value: JSON.parse(JSON.stringify(collection.headers)),
9191
enumerable: true,
9292
writable: true
9393
},

src/SearchResult.js

Lines changed: 82 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
/**
2-
* @param {Collection} dataCollection
2+
* @param {Collection} collection
33
* @param {int} total
44
* @param {Document[]} documents
5-
* @param {object} [aggregations]
6-
* @param {object} [searchArgs]
7-
* @param previous
8-
* @property {Collection} dataCollection
5+
* @param {object} aggregations
6+
* @param {object} options
7+
* @param {object} filters
8+
* @param {SearchResult} previous
9+
* @property {Collection} collection
10+
* @property {number} total
11+
* @property {Document[]} documents
12+
* @property {object} aggregations
13+
* @property {object} options
14+
* @property {object} filters
915
* @property {number} fetchedDocument
1016
* @constructor
1117
*/
12-
function KuzzleSearchResult (dataCollection, total, documents, aggregations, searchArgs, previous) {
18+
function SearchResult (collection, total, documents, aggregations, options, filters, previous) {
1319
Object.defineProperties(this, {
1420
// read-only properties
15-
dataCollection: {
16-
value: dataCollection,
21+
collection: {
22+
value: collection,
23+
enumerable: true
1724
},
1825
total: {
1926
value: total,
@@ -27,24 +34,28 @@ function KuzzleSearchResult (dataCollection, total, documents, aggregations, sea
2734
value: aggregations || {},
2835
enumerable: true
2936
},
30-
searchArgs: {
31-
value: searchArgs || {},
37+
options: {
38+
value: options || {},
39+
enumerable: true
40+
},
41+
filters: {
42+
value: filters || {},
3243
enumerable: true
3344
},
3445
// writable properties
3546
fetchedDocument: {
36-
value: previous instanceof KuzzleSearchResult ? documents.length + previous.fetchedDocument : documents.length,
47+
value: previous instanceof SearchResult ? documents.length + previous.fetchedDocument : documents.length,
3748
enumerable: true,
3849
writable: true
3950
}
4051
});
4152

4253
// promisifying
43-
if (this.dataCollection.kuzzle.bluebird) {
44-
return this.dataCollection.kuzzle.bluebird.promisifyAll(this, {
54+
if (this.collection.kuzzle.bluebird) {
55+
return this.collection.kuzzle.bluebird.promisifyAll(this, {
4556
suffix: 'Promise',
4657
filter: function (name, func, target, passes) {
47-
var whitelist = ['next'];
58+
var whitelist = ['fetchNext'];
4859

4960
return passes && whitelist.indexOf(name) !== -1;
5061
}
@@ -57,16 +68,16 @@ function KuzzleSearchResult (dataCollection, total, documents, aggregations, sea
5768
/**
5869
* @param {function} cb
5970
*/
60-
KuzzleSearchResult.prototype.next = function (cb) {
71+
SearchResult.prototype.fetchNext = function (cb) {
6172
var
6273
filters,
63-
options = Object.assign({}, this.searchArgs.options);
74+
options = Object.assign({}, this.options);
6475

6576
options.previous = this;
6677

6778
// retrieve next results with scroll if original search use it
6879
if (options.scrollId) {
69-
if (this.fetchedDocument >= this.total) {
80+
if (this.fetchedDocument >= this.getTotal()) {
7081
cb(null, null);
7182
return;
7283
}
@@ -80,30 +91,79 @@ KuzzleSearchResult.prototype.next = function (cb) {
8091
delete options.size;
8192
}
8293

83-
this.dataCollection.scroll(options.scrollId, options, this.searchArgs.filters || {}, cb);
94+
this.collection.scroll(options.scrollId, options, this.filters || {}, cb);
8495

8596
return;
8697
}
8798

8899
// retrieve next results with from/size if original search use it
89100
if (options.from !== undefined && options.size !== undefined) {
90-
filters = Object.assign({}, this.searchArgs.filters);
101+
filters = Object.assign({}, this.filters);
91102

92103
// check if we need to do next request to fetch all matching documents
93104
options.from += options.size;
94105

95-
if (options.from >= this.total) {
106+
if (options.from >= this.getTotal()) {
96107
cb(null, null);
97108

98109
return;
99110
}
100111

101-
this.dataCollection.search(filters, options, cb);
112+
this.collection.search(filters, options, cb);
102113

103114
return;
104115
}
105116

106117
cb(new Error('Unable to retrieve next results from search: missing scrollId or from/size params'));
107118
};
108119

109-
module.exports = KuzzleSearchResult;
120+
/**
121+
* @returns {Document[]}
122+
*/
123+
SearchResult.prototype.getDocuments = function () {
124+
return this.documents;
125+
};
126+
127+
/**
128+
* @returns {number}
129+
*/
130+
SearchResult.prototype.getTotal = function () {
131+
return this.total;
132+
};
133+
134+
/**
135+
* @returns {object}
136+
*/
137+
SearchResult.prototype.getAggregations = function () {
138+
return this.aggregations;
139+
};
140+
141+
/**
142+
* @returns {Object}
143+
*/
144+
SearchResult.prototype.getOptions = function() {
145+
return this.options;
146+
};
147+
148+
/**
149+
* @returns {object}
150+
*/
151+
SearchResult.prototype.getFilters = function() {
152+
return this.filters;
153+
};
154+
155+
/**
156+
* @returns {object}
157+
*/
158+
SearchResult.prototype.getCollection = function () {
159+
return this.collection;
160+
};
161+
162+
/**
163+
* @returns {number}
164+
*/
165+
SearchResult.prototype.getFetchedDocument = function () {
166+
return this.fetchedDocument;
167+
};
168+
169+
module.exports = SearchResult;

src/SubscribeResult.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Sugar-code handling the result of a Room.renew call
33
* @constructor
44
*/
5-
function KuzzleSubscribeResult() {
5+
function SubscribeResult() {
66
this.cbs = [];
77
this.error = null;
88
this.room = null;
@@ -12,7 +12,7 @@ function KuzzleSubscribeResult() {
1212
* Registers a callback to be called with a subscription result
1313
* @param {Function} cb
1414
*/
15-
KuzzleSubscribeResult.prototype.onDone = function (cb) {
15+
SubscribeResult.prototype.onDone = function (cb) {
1616
if (this.error || this.room) {
1717
cb(this.error, this.room);
1818
}
@@ -29,7 +29,7 @@ KuzzleSubscribeResult.prototype.onDone = function (cb) {
2929
* @param {Object} error object
3030
* @param {Room} room
3131
*/
32-
KuzzleSubscribeResult.prototype.done = function (error, room) {
32+
SubscribeResult.prototype.done = function (error, room) {
3333
this.error = error;
3434
this.room = room;
3535

@@ -38,4 +38,4 @@ KuzzleSubscribeResult.prototype.done = function (error, room) {
3838
});
3939
};
4040

41-
module.exports = KuzzleSubscribeResult;
41+
module.exports = SubscribeResult;

src/security/Profile.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
var
2-
SecurityDocument = require('./SecurityDocument');
1+
var SecurityDocument = require('./SecurityDocument');
32

43
function Profile(Security, id, content) {
54

src/security/Role.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var KuzzleSecurityDocument = require('./SecurityDocument');
1+
var SecurityDocument = require('./SecurityDocument');
22

33
function Role(Security, id, content) {
44

5-
KuzzleSecurityDocument.call(this, Security, id, content);
5+
SecurityDocument.call(this, Security, id, content);
66

77
// Define properties
88
Object.defineProperties(this, {
@@ -29,7 +29,7 @@ function Role(Security, id, content) {
2929

3030
}
3131

32-
Role.prototype = Object.create(KuzzleSecurityDocument.prototype, {
32+
Role.prototype = Object.create(SecurityDocument.prototype, {
3333
constructor: {
3434
value: Role
3535
}

0 commit comments

Comments
 (0)