Skip to content

Commit 166d877

Browse files
authored
Merge pull request #642 from kuzzleio/7.7.1-proposal
# [7.7.1](https://github.com/kuzzleio/sdk-javascript/releases/tag/7.7.1) (2021-05-31) #### Enhancements - [ [#633](#633) ] Add bulk:updateByQuery and document:mUpsert ([Yoann-Abbes](https://github.com/Yoann-Abbes)) ---
2 parents 04bcd39 + 1f397eb commit 166d877

File tree

13 files changed

+935
-527
lines changed

13 files changed

+935
-527
lines changed

.github/workflows/pull_request.workflow.yml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,27 +97,27 @@ jobs:
9797
# sdk-version: 7
9898
# cypress-key: ${{ secrets.CYPRESS_RECORD_KEY }}
9999

100-
documentation-dead-links:
101-
name: Dead links
102-
runs-on: ubuntu-18.04
103-
steps:
104-
- uses: actions/checkout@v2
105-
- name: Cache node modules
106-
uses: actions/cache@v2
107-
env:
108-
cache-name: cache-node-modules
109-
with:
110-
path: ~/.npm
111-
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
112-
restore-keys: |
113-
${{ runner.os }}-build-${{ env.cache-name }}-
114-
${{ runner.os }}-build-
115-
${{ runner.os }}-
116-
- uses: actions/[email protected]
117-
with:
118-
node-version: "12"
119-
- uses: ./.github/actions/dead-links
120-
100+
# documentation-dead-links:
101+
# name: Dead links
102+
# runs-on: ubuntu-18.04
103+
# steps:
104+
# - uses: actions/checkout@v2
105+
# - name: Cache node modules
106+
# uses: actions/cache@v2
107+
# env:
108+
# cache-name: cache-node-modules
109+
# with:
110+
# path: ~/.npm
111+
# key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
112+
# restore-keys: |
113+
# ${{ runner.os }}-build-${{ env.cache-name }}-
114+
# ${{ runner.os }}-build-
115+
# ${{ runner.os }}-
116+
# - uses: actions/[email protected]
117+
# with:
118+
# node-version: "12"
119+
# - uses: ./.github/actions/dead-links
120+
121121
documentation-snippet-tests:
122122
name: Documentation Snippet Tests
123123
needs: [unit-tests]

.github/workflows/push_master.workflow.yml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,27 @@ jobs:
100100
# sdk-version: 7
101101
# cypress-key: ${{ secrets.CYPRESS_RECORD_KEY }}
102102

103-
documentation-dead-links:
104-
name: Dead links
105-
runs-on: ubuntu-18.04
106-
steps:
107-
- uses: actions/checkout@v2
108-
- name: Cache node modules
109-
uses: actions/cache@v2
110-
env:
111-
cache-name: cache-node-modules
112-
with:
113-
path: ~/.npm
114-
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
115-
restore-keys: |
116-
${{ runner.os }}-build-${{ env.cache-name }}-
117-
${{ runner.os }}-build-
118-
${{ runner.os }}-
119-
- uses: actions/[email protected]
120-
with:
121-
node-version: "12"
122-
- uses: ./.github/actions/dead-links
123-
103+
# documentation-dead-links:
104+
# name: Dead links
105+
# runs-on: ubuntu-18.04
106+
# steps:
107+
# - uses: actions/checkout@v2
108+
# - name: Cache node modules
109+
# uses: actions/cache@v2
110+
# env:
111+
# cache-name: cache-node-modules
112+
# with:
113+
# path: ~/.npm
114+
# key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
115+
# restore-keys: |
116+
# ${{ runner.os }}-build-${{ env.cache-name }}-
117+
# ${{ runner.os }}-build-
118+
# ${{ runner.os }}-
119+
# - uses: actions/[email protected]
120+
# with:
121+
# node-version: "12"
122+
# - uses: ./.github/actions/dead-links
123+
124124
documentation-snippet-tests:
125125
name: Documentation Snippet Tests
126126
needs: [unit-tests]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
code: true
3+
type: page
4+
title: updateByQuery
5+
description: Updates documents matching query
6+
---
7+
8+
# updateByQuery
9+
10+
<SinceBadge version="Kuzzle 2.11.0"/>
11+
<SinceBadge version="7.7.1"/>
12+
13+
Updates documents matching the provided search query.
14+
15+
Kuzzle uses the [ElasticSearch Query DSL](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl.html) syntax.
16+
17+
An empty or null query will match all documents in the collection.
18+
19+
<br/>
20+
21+
```js
22+
updateByQuery(index, collection, query, changes, [options])
23+
```
24+
25+
| Argument | Type | Description |
26+
|---------------|-------------------|-------------------------------------------|
27+
| `index` | <pre>string</pre> | Index name |
28+
| `collection` | <pre>string</pre> | Collection name |
29+
| `query` | <pre>object</pre> | Query to match |
30+
| `changes` | <pre>object</pre> | Partial changes to apply to the documents |
31+
| `options` | <pre>object</pre> | Optional parameters |
32+
33+
---
34+
35+
### options
36+
37+
Additional query options.
38+
39+
| Options | Type<br/>(default) | Description |
40+
|-----------|----------------------------------|------------------------------------------------------------------------------------------------------------------------------------|
41+
| `refresh` | <pre>string</pre><br/>(`""`) | If set to `wait_for`, waits for the change to be reflected for `search` (up to 1s) |
42+
| [`timeout`](/sdk/7/core-classes/kuzzle/query#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 |
43+
44+
## Resolves
45+
46+
Returns the number of updated documents.
47+
48+
## Usage
49+
50+
<<< ./snippets/update-by-query.js
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
try {
2+
const result = await kuzzle.bulk.updateByQuery(
3+
'nyc-open-data',
4+
'yellow-taxi',
5+
{
6+
match: {
7+
capacity: 4
8+
}
9+
},
10+
{ capacity: 42 });
11+
console.log(result);
12+
/**
13+
* 2
14+
*/
15+
} catch (error) {
16+
console.log(error.message);
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: bulk#updateByQuery
2+
description: Update documents matching query
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+
for i in 1 2 ; do
9+
curl --fail -H "Content-type: application/json" -d '{"capacity": 4}' kuzzle:7512/nyc-open-data/yellow-taxi/document_$i/_create
10+
done
11+
for i in 1 2 3 4 5; do
12+
curl --fail -H "Content-type: application/json" -d '{"capacity": 7}' kuzzle:7512/nyc-open-data/yellow-taxi/_create
13+
done
14+
curl -XPOST kuzzle:7512/nyc-open-data/yellow-taxi/_refresh
15+
after:
16+
- curl -XDELETE kuzzle:7512/nyc-open-data
17+
template: default
18+
expected: 2
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
code: true
3+
type: page
4+
title: mUpsert
5+
description: Update documents
6+
---
7+
8+
# mUpsert
9+
10+
<SinceBadge version="Kuzzle 2.11.0"/>
11+
<SinceBadge version="7.7.1"/>
12+
13+
Applies partial changes to multiple documents. If a document doesn't already exist, a new document is created.
14+
15+
You can set the `retryOnConflict` optional argument (with a retry count), to tell Kuzzle to retry the failing updates the specified amount of times before rejecting the request with an error.
16+
17+
<br/>
18+
19+
```js
20+
mUpsert(index, collection, documents, [options]);
21+
```
22+
23+
| Argument | Type | Description |
24+
|--------------|---------------------|------------------------------|
25+
| `index` | <pre>string</pre> | Index name |
26+
| `collection` | <pre>string</pre> | Collection name |
27+
| `documents` | <pre>object[]</pre> | Array of documents to update |
28+
| `options` | <pre>object</pre> | Query options |
29+
30+
31+
### documents
32+
33+
`documents` is an array of object which each object representing a document. Fields `_id` and `changes` is always mandatory while `default` is optional.
34+
Example:
35+
36+
```js
37+
[
38+
{
39+
"_id": "<documentId>",
40+
"changes": {
41+
// document partial changes
42+
},
43+
"default": {
44+
// optional: document fields to add to the "update" part if the document
45+
// is created
46+
}
47+
},
48+
{
49+
"_id": "<anotherDocumentId>",
50+
"changes": {
51+
// document partial changes
52+
},
53+
}
54+
]
55+
```
56+
57+
### Options
58+
59+
Additional query options
60+
61+
| Options | Type<br/>(default) | Description |
62+
|-------------------|----------------------------------|------------------------------------------------------------------------------------------|
63+
| `queuable` | <pre>boolean</pre><br/>(`true`) | If true, queues the request during downtime, until connected to Kuzzle again |
64+
| `refresh` | <pre>string</pre><br/>(`""`) | If set to `wait_for`, waits for the change to be reflected for `search` (up to 1s) |
65+
| `retryOnConflict` | <pre>int</pre><br/>(`0`) | The number of times the database layer should retry in case of version conflict |
66+
| `silent` | <pre>boolean</pre><br/>(`false`) | If `true`, then Kuzzle will not generate notifications <SinceBadge version="7.5.3"/> |
67+
68+
## Resolves
69+
70+
Returns an object containing 2 arrays: `successes` and `errors`
71+
72+
Each updated document is an object of the `successes` array with the following properties:
73+
74+
| Name | Type | Description |
75+
|------------|-------------------|--------------------------------------------------------|
76+
| `_id` | <pre>string</pre> | Document ID |
77+
| `status` | <pre>number</pre> | HTTP error status |
78+
| `created` | <pre>boolean</pre>| `true` if the document has been created |
79+
| `_version` | <pre>number</pre> | Version of the document in the persistent data storage |
80+
| `_source` | <pre>object</pre> | Document content |
81+
82+
Each errored document is an object of the `errors` array with the following properties:
83+
84+
| Name | Type | Description |
85+
|------------|-------------------|-------------------------------|
86+
| `document` | <pre>object</pre> | Document that cause the error |
87+
| `status` | <pre>number</pre> | HTTP error status |
88+
| `reason` | <pre>string</pre> | Human readable reason |
89+
90+
## Usage
91+
92+
<<< ./snippets/m-upsert.js
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const doc1 = { capacity: 4 };
2+
const doc2 = { capacity: 7 };
3+
4+
try {
5+
await kuzzle.document.create('nyc-open-data', 'yellow-taxi', doc1, 'some-id');
6+
7+
const documents = [
8+
{
9+
_id: 'some-id',
10+
changes: { category: 'sedan' }
11+
},
12+
{
13+
_id: 'some-other-id',
14+
changes: { category: 'limousine' },
15+
default: { capacity: 8 }
16+
}
17+
];
18+
19+
const response = await kuzzle.document.mUpsert(
20+
'nyc-open-data',
21+
'yellow-taxi',
22+
documents
23+
);
24+
25+
console.log(response);
26+
/*
27+
{ successes:
28+
[ { _id: 'some-id',
29+
_source: { _kuzzle_info: [Object], category: 'sedan' },
30+
_version: 2,
31+
created: false,
32+
status: 200 },
33+
{ _id: 'some-other-id',
34+
_source: { _kuzzle_info: [Object], category: 'limousine', capacity: 8 },
35+
_version: 1,
36+
created: true,
37+
status: 200 } ],
38+
errors: [] }
39+
*/
40+
console.log('Success');
41+
} catch (error) {
42+
console.error(error.message);
43+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: document#mUpsert
2+
description: Update documents
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+
after:
9+
template: default
10+
expected: Success

0 commit comments

Comments
 (0)