Skip to content

Commit f6ea188

Browse files
committed
Merge remote-tracking branch 'origin/4.x' into standardize-memory-storage
2 parents ac7dcff + d544133 commit f6ea188

File tree

8 files changed

+74
-143
lines changed

8 files changed

+74
-143
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": "3.4.0",
3+
"version": "4.0.0",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <support@kuzzle.io>",
66
"repository": {

src/Collection.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,10 @@ Collection.prototype.create = function (options, cb) {
136136
* Takes an optional argument object with the following properties:
137137
* - metadata (object, default: null):
138138
* Additional information passed to notifications to other users
139-
* - updateIfExist (boolean, default: false):
140-
* If the same document already exists: throw an error if sets to false.
141-
* Update the existing document otherwise
139+
* - ifExist (string, allowed values: "error" (default), "replace"):
140+
* If the same document already exists:
141+
* - resolves with an error if set to "error".
142+
* - replaces the existing document if set to "replace"
142143
*
143144
* @param {string} [id] - (optional) document identifier
144145
* @param {object} document - either an instance of a Document object, or a document
@@ -170,8 +171,13 @@ Collection.prototype.createDocument = function (id, document, options, cb) {
170171
data.body = document;
171172
}
172173

173-
if (options) {
174-
action = options.updateIfExist ? 'createOrReplace' : 'create';
174+
if (options && options.ifExist) {
175+
if (options.ifExist === 'replace') {
176+
action = 'createOrReplace';
177+
}
178+
else if (options.ifExist !== 'error') {
179+
throw new Error('Invalid value for the "ifExist" option: ' + options.ifExist);
180+
}
175181
}
176182

177183
if (id) {
@@ -472,7 +478,8 @@ Collection.prototype.search = function (filters, options, cb) {
472478
result.result.total,
473479
documents,
474480
result.result.aggregations ? result.result.aggregations : [],
475-
{options: options, filters: filters}
481+
{options: options, filters: filters},
482+
options.previous || null
476483
));
477484
});
478485
};
@@ -543,7 +550,8 @@ Collection.prototype.scroll = function (scrollId, options, filters, cb) {
543550
result.result.total,
544551
documents,
545552
result.result.aggregations ? result.result.aggregations : [],
546-
{options: options, filters: filters}
553+
{options: options, filters: filters},
554+
options.previous || null
547555
));
548556
});
549557

src/Kuzzle.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ function Kuzzle (host, options, cb) {
9595
},
9696
writable: true
9797
},
98-
// read-only properties
98+
// configuration properties
9999
autoReconnect: {
100100
value: (options && typeof options.autoReconnect === 'boolean') ? options.autoReconnect : true,
101+
writable: true,
101102
enumerable: true
102103
},
103104
defaultIndex: {
@@ -107,6 +108,7 @@ function Kuzzle (host, options, cb) {
107108
},
108109
reconnectionDelay: {
109110
value: (options && typeof options.reconnectionDelay === 'number') ? options.reconnectionDelay : 1000,
111+
writable: true,
110112
enumerable: true
111113
},
112114
host: {
@@ -121,6 +123,7 @@ function Kuzzle (host, options, cb) {
121123
},
122124
sslConnection: {
123125
value: (options && typeof options.sslConnection === 'boolean') ? options.sslConnection : false,
126+
writable: true,
124127
enumerable: true
125128
},
126129
autoQueue: {

src/SearchResult.js

Lines changed: 33 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/**
2-
*
32
* @param {Collection} dataCollection
43
* @param {int} total
54
* @param {Document[]} documents
65
* @param {object} [aggregations]
76
* @param {object} [searchArgs]
8-
* @param {KuzzleSearchResult} [previous]
7+
* @param previous
8+
* @property {Collection} dataCollection
9+
* @property {number} fetchedDocument
910
* @constructor
1011
*/
1112
function KuzzleSearchResult (dataCollection, total, documents, aggregations, searchArgs, previous) {
@@ -32,31 +33,18 @@ function KuzzleSearchResult (dataCollection, total, documents, aggregations, sea
3233
},
3334
// writable properties
3435
fetchedDocument: {
35-
value: documents.length,
36+
value: previous instanceof KuzzleSearchResult ? documents.length + previous.fetchedDocument : documents.length,
3637
enumerable: true,
3738
writable: true
38-
},
39-
_previous: {
40-
value: previous || null,
41-
writable: true
42-
},
43-
_next: {
44-
value: null,
45-
writable: true
4639
}
4740
});
4841

49-
if (this._previous instanceof KuzzleSearchResult) {
50-
this._previous._next = this;
51-
this.fetchedDocument += this._previous.fetchedDocument;
52-
}
53-
5442
// promisifying
5543
if (this.dataCollection.kuzzle.bluebird) {
5644
return this.dataCollection.kuzzle.bluebird.promisifyAll(this, {
5745
suffix: 'Promise',
5846
filter: function (name, func, target, passes) {
59-
var whitelist = ['previous', 'next'];
47+
var whitelist = ['next'];
6048

6149
return passes && whitelist.indexOf(name) !== -1;
6250
}
@@ -66,107 +54,56 @@ function KuzzleSearchResult (dataCollection, total, documents, aggregations, sea
6654
return this;
6755
}
6856

69-
70-
/**
71-
* @param cb
72-
* @returns {*}
73-
*/
74-
KuzzleSearchResult.prototype.previous = function (cb) {
75-
cb(null, this._previous);
76-
77-
return this;
78-
};
79-
8057
/**
8158
* @param {function} cb
8259
*/
8360
KuzzleSearchResult.prototype.next = function (cb) {
8461
var
8562
filters,
86-
options = Object.assign({}, this.searchArgs.options),
87-
self = this;
88-
89-
if (!this._next) {
90-
// retrieve next results with scroll if original search use it
91-
if (options.scrollId) {
92-
if (this.fetchedDocument >= this.total) {
93-
cb(null, null);
94-
return;
95-
}
96-
97-
// from and size parameters are not valid for a scroll action
98-
if (typeof options.from !== 'undefined') {
99-
delete options.from;
100-
}
101-
102-
if (options.size) {
103-
delete options.size;
104-
}
63+
options = Object.assign({}, this.searchArgs.options);
64+
65+
options.previous = this;
66+
67+
// retrieve next results with scroll if original search use it
68+
if (options.scrollId) {
69+
if (this.fetchedDocument >= this.total) {
70+
cb(null, null);
71+
return;
72+
}
10573

106-
this.dataCollection.scroll(
107-
options.scrollId,
108-
options,
109-
this.searchArgs.filters || {},
110-
function(error, newSearchResults) {
111-
handleNextSearchResults(error, self, newSearchResults, cb);
112-
}
113-
);
74+
// from and size parameters are not valid for a scroll action
75+
if (typeof options.from !== 'undefined') {
76+
delete options.from;
77+
}
11478

115-
return;
79+
if (options.size) {
80+
delete options.size;
11681
}
117-
// retrieve next results with from/size if original search use it
118-
else if (options.from !== undefined && options.size !== undefined) {
119-
filters = Object.assign({}, this.searchArgs.filters);
12082

121-
// check if we need to do next request to fetch all matching documents
122-
options.from += options.size;
83+
this.dataCollection.scroll(options.scrollId, options, this.searchArgs.filters || {}, cb);
12384

124-
if (options.from >= this.total) {
125-
cb(null, null);
85+
return;
86+
}
12687

127-
return;
128-
}
88+
// retrieve next results with from/size if original search use it
89+
if (options.from !== undefined && options.size !== undefined) {
90+
filters = Object.assign({}, this.searchArgs.filters);
91+
92+
// check if we need to do next request to fetch all matching documents
93+
options.from += options.size;
12994

130-
this.dataCollection.search(
131-
filters,
132-
options,
133-
function(error, newSearchResults) {
134-
handleNextSearchResults(error, self, newSearchResults, cb);
135-
}
136-
);
95+
if (options.from >= this.total) {
96+
cb(null, null);
13797

13898
return;
13999
}
140-
}
141100

142-
if (this._next instanceof KuzzleSearchResult) {
143-
cb(null, this._next);
101+
this.dataCollection.search(filters, options, cb);
144102

145103
return;
146104
}
147105

148106
cb(new Error('Unable to retrieve next results from search: missing scrollId or from/size params'));
149107
};
150108

151-
/**
152-
* @param {Error} error
153-
* @param {KuzzleSearchResult} currentSearchResults
154-
* @param {KuzzleSearchResult} newSearchResults
155-
* @param {Function} cb
156-
*/
157-
function handleNextSearchResults (error, currentSearchResults, newSearchResults, cb) {
158-
if (error) {
159-
cb(error);
160-
return;
161-
}
162-
163-
newSearchResults.fetchedDocument += currentSearchResults.fetchedDocument;
164-
165-
newSearchResults._previous = currentSearchResults;
166-
currentSearchResults._next = newSearchResults;
167-
168-
169-
cb(null, newSearchResults);
170-
}
171-
172109
module.exports = KuzzleSearchResult;

test/Collection/methods.test.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,29 @@ describe('Collection methods', function () {
424424
should(emitted).be.true();
425425
});
426426

427-
it('should be able to handle the updateIfExist option', function () {
427+
it('should be able to handle the ifExist=replace option', function () {
428428
var collection = kuzzle.collection(expectedQuery.collection);
429429
expectedQuery.action = 'createOrReplace';
430430

431-
collection.createDocument(result.result._source, {updateIfExist: true});
431+
collection.createDocument(result.result._source, {ifExist: 'replace'});
432432
should(emitted).be.true();
433433
});
434+
435+
it('should be able to handle the ifExist=error option', function () {
436+
var collection = kuzzle.collection(expectedQuery.collection);
437+
expectedQuery.action = 'create';
438+
439+
collection.createDocument(result.result._source, {ifExist: 'error'});
440+
should(emitted).be.true();
441+
});
442+
443+
it('should throw an error if the ifExist option is invalid', function () {
444+
var collection = kuzzle.collection(expectedQuery.collection);
445+
446+
should(function () {
447+
collection.createDocument(result.result._source, {ifExist: 'foobar'});
448+
}).throw();
449+
});
434450
});
435451

436452
describe('#deleteDocument', function () {

test/SearchResult/constructor.test.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ describe('Document constructor', function () {
3535
should(searchResult.documents[0]).be.deepEqual(document);
3636
should(searchResult.searchArgs).be.deepEqual(searchArgs);
3737
should(searchResult.fetchedDocument).be.deepEqual(1);
38-
should(searchResult._previous).be.exactly(null);
39-
should(searchResult._next).be.exactly(null);
4038
});
4139

4240
it('should expose documented properties with the right permissions', function () {
@@ -47,14 +45,11 @@ describe('Document constructor', function () {
4745
should(searchResult).have.propertyWithDescriptor('documents', { enumerable: true, writable: false, configurable: false });
4846
should(searchResult).have.propertyWithDescriptor('searchArgs', { enumerable: true, writable: false, configurable: false });
4947
should(searchResult).have.propertyWithDescriptor('fetchedDocument', { enumerable: true, writable: true, configurable: false });
50-
should(searchResult).have.propertyWithDescriptor('_previous', { enumerable: false, writable: true, configurable: false });
51-
should(searchResult).have.propertyWithDescriptor('_next', { enumerable: false, writable: true, configurable: false });
5248
});
5349

5450
it('should promisify the right functions', function () {
5551
var searchResult = new KuzzleSearchResult(dataCollection, 2, [document], aggregations, searchArgs);
5652

5753
should.exist(searchResult.nextPromise);
58-
should.exist(searchResult.previousPromise);
5954
});
6055
});

test/SearchResult/methods.test.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,6 @@ describe('KuzzleSearchResult methods', function () {
2727
});
2828

2929
describe('#next', function () {
30-
it('should return the next SearchResult if it has already fetched', function (done) {
31-
var
32-
firstSearchResult = new KuzzleSearchResult(dataCollection, 2, [firstDocument], {}, searchArgs),
33-
secondSearchResult = new KuzzleSearchResult(dataCollection, 2, [secondDocument], {}, searchArgs, firstSearchResult);
34-
35-
firstSearchResult.next(function(error, next) {
36-
should(error).be.exactly(null);
37-
should(next).be.exactly(secondSearchResult);
38-
done();
39-
});
40-
41-
});
42-
4330
it('should be able to do a scroll request', function (done) {
4431
var
4532
mockScrollResult = new KuzzleSearchResult(
@@ -63,7 +50,6 @@ describe('KuzzleSearchResult methods', function () {
6350
should(result).be.an.instanceOf(KuzzleSearchResult);
6451
should(result.documents).be.an.Array();
6552
should(result.documents.length).be.exactly(1);
66-
should(result._previous).be.exactly(firstSearchResult);
6753
done();
6854
});
6955
});
@@ -162,18 +148,4 @@ describe('KuzzleSearchResult methods', function () {
162148
});
163149
});
164150
});
165-
166-
describe('#previous', function () {
167-
it('should call the callback with the previous SearchResult', function (done) {
168-
var
169-
firstSearchResult = new KuzzleSearchResult(dataCollection, 2, [firstDocument], {}, searchArgs),
170-
secondSearchResult = new KuzzleSearchResult(dataCollection, 2, [secondDocument], {}, searchArgs, firstSearchResult);
171-
172-
secondSearchResult.previous(function(error, previous) {
173-
should(error).be.exactly(null);
174-
should(previous).be.exactly(firstSearchResult);
175-
done();
176-
});
177-
});
178-
});
179151
});

test/kuzzle/constructor.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('Kuzzle constructor', function () {
5656
var kuzzle = new Kuzzle('nowhere');
5757

5858
should(kuzzle).have.propertyWithDescriptor('autoQueue', { enumerable: true, writable: true, configurable: false });
59-
should(kuzzle).have.propertyWithDescriptor('autoReconnect', { enumerable: true, writable: false, configurable: false });
59+
should(kuzzle).have.propertyWithDescriptor('autoReconnect', { enumerable: true, writable: true, configurable: false });
6060
should(kuzzle).have.propertyWithDescriptor('autoReplay', { enumerable: true, writable: true, configurable: false });
6161
should(kuzzle).have.propertyWithDescriptor('autoResubscribe', { enumerable: true, writable: true, configurable: false });
6262
should(kuzzle).have.propertyWithDescriptor('defaultIndex', { enumerable: true, writable: true, configurable: false });
@@ -67,11 +67,11 @@ describe('Kuzzle constructor', function () {
6767
should(kuzzle).have.propertyWithDescriptor('headers', { enumerable: true, writable: true, configurable: false });
6868
should(kuzzle).have.propertyWithDescriptor('metadata', { enumerable: true, writable: true, configurable: false });
6969
should(kuzzle).have.propertyWithDescriptor('replayInterval', { enumerable: true, writable: true, configurable: false });
70-
should(kuzzle).have.propertyWithDescriptor('reconnectionDelay', { enumerable: true, writable: false, configurable: false });
70+
should(kuzzle).have.propertyWithDescriptor('reconnectionDelay', { enumerable: true, writable: true, configurable: false });
7171
should(kuzzle).have.propertyWithDescriptor('jwtToken', { enumerable: true, writable: true, configurable: false });
7272
should(kuzzle).have.propertyWithDescriptor('offlineQueueLoader', { enumerable: true, writable: true, configurable: false });
7373
should(kuzzle).have.propertyWithDescriptor('port', { enumerable: true, writable: true, configurable: false });
74-
should(kuzzle).have.propertyWithDescriptor('sslConnection', { enumerable: true, writable: false, configurable: false });
74+
should(kuzzle).have.propertyWithDescriptor('sslConnection', { enumerable: true, writable: true, configurable: false });
7575
});
7676

7777
it('should have properties with the documented default values', function () {

0 commit comments

Comments
 (0)