Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 doc/7/controllers/document/upsert/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Additional query options

| Options | Type<br/>(default) | Description |
| ----------------- | -------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| `defaults` | <pre>object</pre><br/>(`{}`) | Fields to add to the document if it gets created |
| `default` | <pre>object</pre><br/>(`{}`) | Fields to add to the document if it gets created |
| `refresh` | <pre>string</pre><br/>(`""`) | If set to `wait_for`, waits for the change to be reflected for `search` (up to 1s) |
| `retryOnConflict` | <pre>int</pre><br/>(`10`) | The number of times the database layer should retry in case of version conflict |
| `silent` | <pre>boolean</pre><br/>(`false`) | If `true`, then Kuzzle will not generate notifications <SinceBadge version="7.5.3"/> |
Expand Down
18 changes: 13 additions & 5 deletions src/controllers/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export class DocumentController extends BaseController {
silent: options.silent,
source: options.source,
};

return this.query(request, options)
.then(response => response.result);
}
Expand Down Expand Up @@ -397,6 +397,7 @@ export class DocumentController extends BaseController {
body: { documents },
action: 'mCreate',
silent: options.silent,
strict: options.strict,
};

return this.query(request, options)
Expand Down Expand Up @@ -469,6 +470,7 @@ export class DocumentController extends BaseController {
body: { documents },
action: 'mCreateOrReplace',
silent: options.silent,
strict: options.strict,
};

return this.query(request, options)
Expand Down Expand Up @@ -528,6 +530,7 @@ export class DocumentController extends BaseController {
body: { ids },
action: 'mDelete',
silent: options.silent,
strict: options.strict,
};

return this.query(request, options)
Expand Down Expand Up @@ -645,6 +648,7 @@ export class DocumentController extends BaseController {
body: { documents },
action: 'mReplace',
silent: options.silent,
strict: options.strict,
};

return this.query(request, options)
Expand Down Expand Up @@ -722,6 +726,7 @@ export class DocumentController extends BaseController {
body: { documents },
action: 'mUpdate',
silent: options.silent,
strict: options.strict,
};

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

/**
* Applies partial updates to multiple documents.
*
*
* If a document doesn't already exist, a new document is created.
* @see https://docs.kuzzle.io/sdk/js/7/controllers/document/m-upsert/
*
Expand All @@ -742,6 +747,7 @@ export class DocumentController extends BaseController {
* - `refresh` If set to `wait_for`, Kuzzle will not respond until the API key is indexed
* - `silent` If true, then Kuzzle will not generate notifications
* - `retryOnConflict` Number of times the database layer should retry in case of version conflict
* - `strict` If true, an error will occur if a document was not updated
*
* @returns An object containing 2 arrays: "successes" and "errors"
*/
Expand All @@ -767,6 +773,7 @@ export class DocumentController extends BaseController {
refresh?: 'wait_for',
silent?: boolean,
retryOnConflict?: number,
strict?: boolean
} = {}
): Promise<{
/**
Expand Down Expand Up @@ -797,6 +804,7 @@ export class DocumentController extends BaseController {
body: { documents },
action: 'mUpsert',
silent: options.silent,
strict: options.strict,
};

return this.query(request, options)
Expand Down Expand Up @@ -1038,7 +1046,7 @@ export class DocumentController extends BaseController {
* @param _id Unique document identifier
* @param changes Partial content of the document to update
* @param [options]
* - `defaults` Fields to add to the document if it gets created
* - `default` Fields to add to the document if it gets created
* - `refresh` If set to `wait_for`, Kuzzle will not respond until the API key is indexed
* - `silent` If true, then Kuzzle will not generate notifications
* - `retryOnConflict` Number of times the database layer should retry in case of version conflict
Expand All @@ -1052,7 +1060,7 @@ export class DocumentController extends BaseController {
_id: string,
changes: JSONObject,
options: {
defaults?: JSONObject;
default?: JSONObject;
refresh?: string,
silent?: boolean,
retryOnConflict?: boolean,
Expand All @@ -1064,7 +1072,7 @@ export class DocumentController extends BaseController {
index,
collection,
_id,
body: { changes, defaults: options.defaults },
body: { changes, default: options.default },
action: 'upsert',
source: options.source,
silent: options.silent,
Expand Down
47 changes: 45 additions & 2 deletions test/controllers/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ const { DocumentController } = require('../../src/controllers/Document');
const { DocumentSearchResult } = require('../../src/core/searchResult/Document');

describe('Document Controller', () => {
const options = {opt: 'in'};
let options;
let kuzzle;

beforeEach(() => {
kuzzle = {
query: sinon.stub().resolves()
};
kuzzle.document = new DocumentController(kuzzle);
options = { opt: 'in' };
});

describe('count', () => {
Expand Down Expand Up @@ -118,7 +119,7 @@ describe('Document Controller', () => {
});
});


describe('deleteFields', () => {
it('should call document/deleteFields query and return a Promise which resolves the updated document', () => {
kuzzle.query.resolves({result: {_id: 'document-id', _source: {foo: 'bar'}}});
Expand Down Expand Up @@ -219,6 +220,7 @@ describe('Document Controller', () => {
};
kuzzle.query.resolves({result});
options.silent = true;
options.strict = true;

return kuzzle.document.mCreate('index', 'collection', [{_id: 'document-id', body: {foo: 'bar'}}], options)
.then(res => {
Expand All @@ -230,6 +232,7 @@ describe('Document Controller', () => {
index: 'index',
collection: 'collection',
silent: true,
strict: true,
body: {documents: [{_id: 'document-id', body: {foo: 'bar'}}]}
}, options);

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

return kuzzle.document.mCreateOrReplace('index', 'collection', [{_id: 'document-id', body: {foo: 'bar'}}], options)
.then(res => {
Expand All @@ -261,6 +265,7 @@ describe('Document Controller', () => {
index: 'index',
collection: 'collection',
silent: true,
strict: true,
body: {documents: [{_id: 'document-id', body: {foo: 'bar'}}]}
}, options);

Expand All @@ -269,11 +274,44 @@ describe('Document Controller', () => {
});
});

describe('upsert', () => {
it('should call document/upsert query and return a Promise which resolves Kuzzle result', () => {
const result = {
_id: 'document-1629897817507',
_version: 1,
created: true
};
kuzzle.query.resolves({result});
options.silent = true;
options.default = { def: 'default' };
options.source = true;

return kuzzle.document.upsert('index', 'collection', 'some-id', { changes: 'changes' }, options)
.then(res => {
should(kuzzle.query)
.be.calledOnce()
.be.calledWith({
controller: 'document',
action: 'upsert',
index: 'index',
collection: 'collection',
_id: 'some-id',
silent: true,
source: true,
body: { changes: { changes: 'changes' }, default: { def: 'default' } },
}, options);

should(res).be.equal(result);
});
});
});

describe('mDelete', () => {
it('should call document/mDelete query and return a Promise which resolves the list of deleted documents ids', () => {
const result = ['document1', 'document2'];
kuzzle.query.resolves({result});
options.silent = true;
options.strict = true;

return kuzzle.document.mDelete('index', 'collection', ['document1', 'document2'], options)
.then(res => {
Expand All @@ -285,6 +323,7 @@ describe('Document Controller', () => {
index: 'index',
collection: 'collection',
silent: true,
strict: true,
body: {ids: ['document1', 'document2']}
}, options);

Expand Down Expand Up @@ -336,6 +375,7 @@ describe('Document Controller', () => {
};
kuzzle.query.resolves({result});
options.silent = true;
options.strict = true;

return kuzzle.document.mReplace('index', 'collection', [{_id: 'document-id', body: {foo: 'bar'}}], options)
.then(res => {
Expand All @@ -347,6 +387,7 @@ describe('Document Controller', () => {
index: 'index',
collection: 'collection',
silent: true,
strict: true,
body: {documents: [{_id: 'document-id', body: {foo: 'bar'}}]}
}, options);

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

return kuzzle.document.mUpdate('index', 'collection', [{_id: 'document-id', body: {foo: 'bar'}}], options)
.then(res => {
Expand All @@ -378,6 +420,7 @@ describe('Document Controller', () => {
index: 'index',
collection: 'collection',
silent: true,
strict: true,
body: {documents: [{_id: 'document-id', body: {foo: 'bar'}}]}
}, options);

Expand Down