Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kuzzle-sdk",
"version": "3.3.0",
"version": "4.0.0",
"description": "Official Javascript SDK for Kuzzle",
"author": "The Kuzzle Team <support@kuzzle.io>",
"repository": {
Expand Down
16 changes: 11 additions & 5 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ Collection.prototype.create = function (options, cb) {
* Takes an optional argument object with the following properties:
* - metadata (object, default: null):
* Additional information passed to notifications to other users
* - updateIfExist (boolean, default: false):
* If the same document already exists: throw an error if sets to false.
* Update the existing document otherwise
* - ifExist (string, allowed values: "error" (default), "replace"):
* If the same document already exists:
* - resolves with an error if set to "error".
* - replaces the existing document if set to "replace"
*
* @param {string} [id] - (optional) document identifier
* @param {object} document - either an instance of a Document object, or a document
Expand Down Expand Up @@ -170,8 +171,13 @@ Collection.prototype.createDocument = function (id, document, options, cb) {
data.body = document;
}

if (options) {
action = options.updateIfExist ? 'createOrReplace' : 'create';
if (options && options.ifExist) {
if (options.ifExist === 'replace') {
action = 'createOrReplace';
}
else if (options.ifExist !== 'error') {
throw new Error('Invalid value for the "ifExist" option: ' + options.ifExist);
}
}

if (id) {
Expand Down
20 changes: 18 additions & 2 deletions test/Collection/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,29 @@ describe('Collection methods', function () {
should(emitted).be.true();
});

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

collection.createDocument(result.result._source, {updateIfExist: true});
collection.createDocument(result.result._source, {ifExist: 'replace'});
should(emitted).be.true();
});

it('should be able to handle the ifExist=error option', function () {
var collection = kuzzle.collection(expectedQuery.collection);
expectedQuery.action = 'create';

collection.createDocument(result.result._source, {ifExist: 'error'});
should(emitted).be.true();
});

it('should throw an error if the ifExist option is invalid', function () {
var collection = kuzzle.collection(expectedQuery.collection);

should(function () {
collection.createDocument(result.result._source, {ifExist: 'foobar'});
}).throw();
});
});

describe('#deleteDocument', function () {
Expand Down