Skip to content

Commit a967b88

Browse files
author
Shiranuit
authored
Merge pull request #636 from kuzzleio/587-document-deleteFields
Add support for the document:deleteFields API action
2 parents 2e1a76d + 0600843 commit a967b88

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
code: true
3+
type: page
4+
title: deleteFields
5+
description: Deletes fields of an existing document.
6+
---
7+
8+
# delete
9+
10+
Deletes fields of an existing document.
11+
12+
The optional parameter `refresh` can be used with the value `wait_for` in order to wait for the document to be indexed (and to no longer be available in search).
13+
14+
<br/>
15+
16+
```js
17+
deleteFields (index, collection, id, fields, [options]);
18+
```
19+
20+
| Argument | Type | Description |
21+
| ------------ | ------------------- | ------------------------------------------------------------------ |
22+
| `index` | <pre>string</pre> | Index name |
23+
| `collection` | <pre>string</pre> | Collection name |
24+
| `id` | <pre>string</pre> | Document ID |
25+
| `fields` | <pre>string[]</pre> | [Path](https://lodash.com/docs/4.17.15#toPath) of fields to delete |
26+
| `options` | <pre>object</pre> | Query options |
27+
28+
### Options
29+
30+
Additional query options
31+
32+
| Options | Type<br/>(default) | Description |
33+
| ---------- | -------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
34+
| `queuable` | <pre>boolean</pre><br/>(`true`) | If true, queues the request during downtime, until connected to Kuzzle again |
35+
| `refresh` | <pre>string</pre><br/>(`""`) | If set to `wait_for`, waits for the change to be reflected for `search` (up to 1s) |
36+
| `silent` | <pre>boolean</pre><br/>(`false`) | If `true`, then Kuzzle will not generate notifications <SinceBadge version="7.5.3"/> |
37+
| `source` | <pre>boolean</pre><br/>(`false`) | If `true`, then the response will contain the updated document |
38+
| `timeout` | <pre>number</pre> | Time (in ms) during which a request will still be waited to be resolved. Set it `-1` if you want to wait indefinitely |
39+
40+
## Resolves
41+
42+
Resolves to updated document.
43+
44+
## Usage
45+
46+
<<< ./snippets/deleteFields.js
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
try {
2+
const response = await kuzzle.document.deleteFields('nyc-open-data', 'yellow-taxi', 'some-id', ['bar'], {source: true});
3+
4+
console.log(response._source);
5+
} catch (error) {
6+
console.error(error.message);
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: document#delete
2+
description: Deletes a document from kuzzle
3+
hooks:
4+
before: |
5+
curl -XDELETE kuzzle:7512/nyc-open-data
6+
curl -XPOST kuzzle:7512/nyc-open-data/_create
7+
curl -XPUT kuzzle:7512/nyc-open-data/yellow-taxi
8+
curl --fail -H "Content-type: application/json" -XPUT -d '{"foo": "bar", "bar": "baz"}' kuzzle:7512/nyc-open-data/yellow-taxi/some-id
9+
after:
10+
template: default
11+
expected: {"foo": "bar"}

src/controllers/Document.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,50 @@ export class DocumentController extends BaseController {
211211
.then(response => response.result.ids);
212212
}
213213

214+
/**
215+
* Deletes fields of an existing document.
216+
*
217+
* @see https://docs.kuzzle.io/core/2/api/controllers/document/delete-fields/
218+
*
219+
* @param index Index name
220+
* @param collection Collection name
221+
* @param _id Document ID
222+
* @param options Additional options
223+
* - `queuable` If true, queues the request during downtime, until connected to Kuzzle again
224+
* - `refresh` If set to `wait_for`, Kuzzle will not respond until the API key is indexed
225+
* - `silent` If true, then Kuzzle will not generate notifications
226+
* - `source` If true, the response will contain the updated document
227+
* - `timeout` Request Timeout in ms, after the delay if not resolved the promise will be rejected
228+
*
229+
* @returns The updated document
230+
*/
231+
deleteFields(
232+
index: string,
233+
collection: string,
234+
_id: string,
235+
fields: string[],
236+
options: {
237+
queuable?: boolean,
238+
refresh?: 'wait_for',
239+
silent?: boolean,
240+
source?: boolean,
241+
timeout?: number,
242+
} = {}
243+
): Promise<Document> {
244+
const request = {
245+
index,
246+
collection,
247+
_id,
248+
body: { fields },
249+
action: 'deleteFields',
250+
silent: options.silent,
251+
source: options.source,
252+
};
253+
254+
return this.query(request, options)
255+
.then(response => response.result);
256+
}
257+
214258
/**
215259
* Checks if the given document exists.
216260
*

test/controllers/document.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,36 @@ describe('Document Controller', () => {
118118
});
119119
});
120120

121+
122+
describe('deleteFields', () => {
123+
it('should call document/deleteFields query and return a Promise which resolves the updated document', () => {
124+
kuzzle.query.resolves({result: {_id: 'document-id', _source: {foo: 'bar'}}});
125+
options.silent = true;
126+
127+
const optionsCopy = Object.assign({}, options);
128+
optionsCopy.source = true;
129+
130+
return kuzzle.document.deleteFields('index', 'collection', 'document-id', ['bar'], optionsCopy)
131+
.then(res => {
132+
should(kuzzle.query)
133+
.be.calledOnce()
134+
.be.calledWithMatch({
135+
controller: 'document',
136+
action: 'deleteFields',
137+
index: 'index',
138+
collection: 'collection',
139+
_id: 'document-id',
140+
body: {fields: ['bar']},
141+
silent: true,
142+
source: true,
143+
}, optionsCopy);
144+
145+
should(res._id).be.equal('document-id');
146+
should(res._source.foo).be.equal('bar');
147+
});
148+
});
149+
});
150+
121151
describe('deleteByQuery', () => {
122152
it('should call document/deleteByQuery query and return a Promise which resolves the list of deleted document ids', () => {
123153
kuzzle.query.resolves({result: {ids: ['foo', 'bar', 'baz']}});

0 commit comments

Comments
 (0)