Skip to content

Commit 1fb22d4

Browse files
authored
Fix upsert options.default (#660)
The options.default option of document.upsert method was wrong: instead of default, the defaults property was sent to Kuzzle
1 parent 174ae31 commit 1fb22d4

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

doc/7/controllers/document/upsert/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Additional query options
3030

3131
| Options | Type<br/>(default) | Description |
3232
| ----------------- | -------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
33-
| `defaults` | <pre>object</pre><br/>(`{}`) | Fields to add to the document if it gets created |
33+
| `default` | <pre>object</pre><br/>(`{}`) | Fields to add to the document if it gets created |
3434
| `refresh` | <pre>string</pre><br/>(`""`) | If set to `wait_for`, waits for the change to be reflected for `search` (up to 1s) |
3535
| `retryOnConflict` | <pre>int</pre><br/>(`10`) | The number of times the database layer should retry in case of version conflict |
3636
| `silent` | <pre>boolean</pre><br/>(`false`) | If `true`, then Kuzzle will not generate notifications <SinceBadge version="7.5.3"/> |

src/controllers/Document.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export class DocumentController extends BaseController {
250250
silent: options.silent,
251251
source: options.source,
252252
};
253-
253+
254254
return this.query(request, options)
255255
.then(response => response.result);
256256
}
@@ -397,6 +397,7 @@ export class DocumentController extends BaseController {
397397
body: { documents },
398398
action: 'mCreate',
399399
silent: options.silent,
400+
strict: options.strict,
400401
};
401402

402403
return this.query(request, options)
@@ -469,6 +470,7 @@ export class DocumentController extends BaseController {
469470
body: { documents },
470471
action: 'mCreateOrReplace',
471472
silent: options.silent,
473+
strict: options.strict,
472474
};
473475

474476
return this.query(request, options)
@@ -528,6 +530,7 @@ export class DocumentController extends BaseController {
528530
body: { ids },
529531
action: 'mDelete',
530532
silent: options.silent,
533+
strict: options.strict,
531534
};
532535

533536
return this.query(request, options)
@@ -645,6 +648,7 @@ export class DocumentController extends BaseController {
645648
body: { documents },
646649
action: 'mReplace',
647650
silent: options.silent,
651+
strict: options.strict,
648652
};
649653

650654
return this.query(request, options)
@@ -722,6 +726,7 @@ export class DocumentController extends BaseController {
722726
body: { documents },
723727
action: 'mUpdate',
724728
silent: options.silent,
729+
strict: options.strict,
725730
};
726731

727732
return this.query(request, options)
@@ -730,7 +735,7 @@ export class DocumentController extends BaseController {
730735

731736
/**
732737
* Applies partial updates to multiple documents.
733-
*
738+
*
734739
* If a document doesn't already exist, a new document is created.
735740
* @see https://docs.kuzzle.io/sdk/js/7/controllers/document/m-upsert/
736741
*
@@ -742,6 +747,7 @@ export class DocumentController extends BaseController {
742747
* - `refresh` If set to `wait_for`, Kuzzle will not respond until the API key is indexed
743748
* - `silent` If true, then Kuzzle will not generate notifications
744749
* - `retryOnConflict` Number of times the database layer should retry in case of version conflict
750+
* - `strict` If true, an error will occur if a document was not updated
745751
*
746752
* @returns An object containing 2 arrays: "successes" and "errors"
747753
*/
@@ -767,6 +773,7 @@ export class DocumentController extends BaseController {
767773
refresh?: 'wait_for',
768774
silent?: boolean,
769775
retryOnConflict?: number,
776+
strict?: boolean
770777
} = {}
771778
): Promise<{
772779
/**
@@ -797,6 +804,7 @@ export class DocumentController extends BaseController {
797804
body: { documents },
798805
action: 'mUpsert',
799806
silent: options.silent,
807+
strict: options.strict,
800808
};
801809

802810
return this.query(request, options)
@@ -1038,7 +1046,7 @@ export class DocumentController extends BaseController {
10381046
* @param _id Unique document identifier
10391047
* @param changes Partial content of the document to update
10401048
* @param [options]
1041-
* - `defaults` Fields to add to the document if it gets created
1049+
* - `default` Fields to add to the document if it gets created
10421050
* - `refresh` If set to `wait_for`, Kuzzle will not respond until the API key is indexed
10431051
* - `silent` If true, then Kuzzle will not generate notifications
10441052
* - `retryOnConflict` Number of times the database layer should retry in case of version conflict
@@ -1052,7 +1060,7 @@ export class DocumentController extends BaseController {
10521060
_id: string,
10531061
changes: JSONObject,
10541062
options: {
1055-
defaults?: JSONObject;
1063+
default?: JSONObject;
10561064
refresh?: string,
10571065
silent?: boolean,
10581066
retryOnConflict?: boolean,
@@ -1064,7 +1072,7 @@ export class DocumentController extends BaseController {
10641072
index,
10651073
collection,
10661074
_id,
1067-
body: { changes, defaults: options.defaults },
1075+
body: { changes, default: options.default },
10681076
action: 'upsert',
10691077
source: options.source,
10701078
silent: options.silent,

test/controllers/document.test.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ const { DocumentController } = require('../../src/controllers/Document');
55
const { DocumentSearchResult } = require('../../src/core/searchResult/Document');
66

77
describe('Document Controller', () => {
8-
const options = {opt: 'in'};
8+
let options;
99
let kuzzle;
1010

1111
beforeEach(() => {
1212
kuzzle = {
1313
query: sinon.stub().resolves()
1414
};
1515
kuzzle.document = new DocumentController(kuzzle);
16+
options = { opt: 'in' };
1617
});
1718

1819
describe('count', () => {
@@ -118,7 +119,7 @@ describe('Document Controller', () => {
118119
});
119120
});
120121

121-
122+
122123
describe('deleteFields', () => {
123124
it('should call document/deleteFields query and return a Promise which resolves the updated document', () => {
124125
kuzzle.query.resolves({result: {_id: 'document-id', _source: {foo: 'bar'}}});
@@ -219,6 +220,7 @@ describe('Document Controller', () => {
219220
};
220221
kuzzle.query.resolves({result});
221222
options.silent = true;
223+
options.strict = true;
222224

223225
return kuzzle.document.mCreate('index', 'collection', [{_id: 'document-id', body: {foo: 'bar'}}], options)
224226
.then(res => {
@@ -230,6 +232,7 @@ describe('Document Controller', () => {
230232
index: 'index',
231233
collection: 'collection',
232234
silent: true,
235+
strict: true,
233236
body: {documents: [{_id: 'document-id', body: {foo: 'bar'}}]}
234237
}, options);
235238

@@ -250,6 +253,7 @@ describe('Document Controller', () => {
250253
};
251254
kuzzle.query.resolves({result});
252255
options.silent = true;
256+
options.strict = true;
253257

254258
return kuzzle.document.mCreateOrReplace('index', 'collection', [{_id: 'document-id', body: {foo: 'bar'}}], options)
255259
.then(res => {
@@ -261,6 +265,7 @@ describe('Document Controller', () => {
261265
index: 'index',
262266
collection: 'collection',
263267
silent: true,
268+
strict: true,
264269
body: {documents: [{_id: 'document-id', body: {foo: 'bar'}}]}
265270
}, options);
266271

@@ -269,11 +274,44 @@ describe('Document Controller', () => {
269274
});
270275
});
271276

277+
describe('upsert', () => {
278+
it('should call document/upsert query and return a Promise which resolves Kuzzle result', () => {
279+
const result = {
280+
_id: 'document-1629897817507',
281+
_version: 1,
282+
created: true
283+
};
284+
kuzzle.query.resolves({result});
285+
options.silent = true;
286+
options.default = { def: 'default' };
287+
options.source = true;
288+
289+
return kuzzle.document.upsert('index', 'collection', 'some-id', { changes: 'changes' }, options)
290+
.then(res => {
291+
should(kuzzle.query)
292+
.be.calledOnce()
293+
.be.calledWith({
294+
controller: 'document',
295+
action: 'upsert',
296+
index: 'index',
297+
collection: 'collection',
298+
_id: 'some-id',
299+
silent: true,
300+
source: true,
301+
body: { changes: { changes: 'changes' }, default: { def: 'default' } },
302+
}, options);
303+
304+
should(res).be.equal(result);
305+
});
306+
});
307+
});
308+
272309
describe('mDelete', () => {
273310
it('should call document/mDelete query and return a Promise which resolves the list of deleted documents ids', () => {
274311
const result = ['document1', 'document2'];
275312
kuzzle.query.resolves({result});
276313
options.silent = true;
314+
options.strict = true;
277315

278316
return kuzzle.document.mDelete('index', 'collection', ['document1', 'document2'], options)
279317
.then(res => {
@@ -285,6 +323,7 @@ describe('Document Controller', () => {
285323
index: 'index',
286324
collection: 'collection',
287325
silent: true,
326+
strict: true,
288327
body: {ids: ['document1', 'document2']}
289328
}, options);
290329

@@ -336,6 +375,7 @@ describe('Document Controller', () => {
336375
};
337376
kuzzle.query.resolves({result});
338377
options.silent = true;
378+
options.strict = true;
339379

340380
return kuzzle.document.mReplace('index', 'collection', [{_id: 'document-id', body: {foo: 'bar'}}], options)
341381
.then(res => {
@@ -347,6 +387,7 @@ describe('Document Controller', () => {
347387
index: 'index',
348388
collection: 'collection',
349389
silent: true,
390+
strict: true,
350391
body: {documents: [{_id: 'document-id', body: {foo: 'bar'}}]}
351392
}, options);
352393

@@ -367,6 +408,7 @@ describe('Document Controller', () => {
367408
};
368409
kuzzle.query.resolves({result});
369410
options.silent = true;
411+
options.strict = true;
370412

371413
return kuzzle.document.mUpdate('index', 'collection', [{_id: 'document-id', body: {foo: 'bar'}}], options)
372414
.then(res => {
@@ -378,6 +420,7 @@ describe('Document Controller', () => {
378420
index: 'index',
379421
collection: 'collection',
380422
silent: true,
423+
strict: true,
381424
body: {documents: [{_id: 'document-id', body: {foo: 'bar'}}]}
382425
}, options);
383426

0 commit comments

Comments
 (0)