Skip to content

Commit d544133

Browse files
authored
Merge pull request #182 from kuzzleio/rename-updateifexist-option
Collection.createDocument: rename the updateIfExist option
2 parents f47098e + b4595a3 commit d544133

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
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: 11 additions & 5 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) {

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 () {

0 commit comments

Comments
 (0)