Skip to content

Commit

Permalink
Add document:upsert (#56)
Browse files Browse the repository at this point in the history
Add document:upsert API action
  • Loading branch information
Yoann-Abbes authored Jan 5, 2021
1 parent 032d292 commit 0fa1e18
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 2 deletions.
60 changes: 60 additions & 0 deletions doc/2/controllers/document/upsert/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
code: true
type: page
title: upsert
description: Applies partial changes to a document. If the document doesn't already exist, a new document is created.
---

# upsert

<SinceBadge version="Kuzzle 2.8.0"/>
<SinceBadge version="auto-version" />

Applies partial changes to a document. If the document doesn't already exist, a new document is created.


```dart
Future<Map<String, dynamic>> upsert(
String index,
String collection,
String id,
Map<String, dynamic> changes, {
bool waitForRefresh = false,
int retryOnConflict,
bool source,
})
```

| Argument | Type | Description |
| ------------ | ----------------- | ----------------------------------------- |
| `index` | <pre>String</pre> | Index name |
| `collection` | <pre>String</pre> | Collection name |
| `id` | <pre>String</pre> | Document ID |
| `changes` | <pre>Map<String, dynamic></pre> | Partial content of the document to update |

### Options

Additional query options

| Options | Type<br/>(default) | Description |
| ----------------- | ------------------------------- | ---------------------------------------------------------------------------------- |
| `defaults` | <pre>Map<String, dynamic></pre><br/>(`{}`) | (optional) fields to add to the document if it gets created |
| `waitForRefresh` | <pre>bool</pre><br/>(`""`) | If set to `true`, 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 |
| `source` | <pre>bool</pre><br/>(`false`)| If true, returns the updated document inside the response


## Returns

A `Map<String, dynamic>` with the following properties:

| Property | Type | Description |
|------------- |--------------------------------------------- |--------------------------------- |
| `_source` | <pre>Map<String, dynamic></pre> | Updated document (if `source` option set to true) |
| `_id` | <pre>String</pre> | ID of the udated document |
| `_version` | <pre>int</pre> | Version of the document in the persistent data storage |
| `created` | <pre>bool</pre> | If `true`, a new document was created, otherwise the document existed and was updated |

## Usage

<<< ./snippets/upsert.dart
6 changes: 6 additions & 0 deletions doc/2/controllers/document/upsert/snippets/upsert.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
final result = await kuzzle
.document
.upsert('nyc-open-data', 'yellow-taxi', 'some-id', {
'changes': { 'category': 'suv' },
},
);
10 changes: 10 additions & 0 deletions doc/2/controllers/document/upsert/snippets/upsert.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: document#upsert
description: Applies a partial update to an existing document.
hooks:
before: |
curl -XDELETE kuzzle:7512/nyc-open-data
curl -XPOST kuzzle:7512/nyc-open-data/_create
curl -XPUT kuzzle:7512/nyc-open-data/yellow-taxi
after:
template: print-result
expected: "{_id: some-id, _version: 1, created: true}"
46 changes: 45 additions & 1 deletion lib/src/controllers/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class DocumentController extends KuzzleController {
///
/// Optional
///
/// **[refresh]**: if set to wait_for, Kuzzle will not respond
/// **[waitForRefresh]**: if set to wait_for, Kuzzle will not respond
/// until the update is indexed \\\
/// **[retryOnConflict]**: conflicts may occur if the same document gets updated multiple times within a short timespan, in a database cluster. 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. \\\
/// **[source]**: if set to true Kuzzle will return the updated document body in the response \\\
Expand Down Expand Up @@ -421,6 +421,50 @@ class DocumentController extends KuzzleController {
return response.result as Map<String, dynamic>;
}

/// ####Applies a partial update to an existing document.
/// If the document doesn't already exist, a new document is created.
///
/// **[index]**: index name \\\
/// **[collection]**: collection name \\\
/// **[id]**: unique identifier of the document to update \\\
/// **[changes]**: Partial changes to apply to the document
///
/// Optional
///
/// **[defaults]**: Fields to add to the document if it gets created
/// **[waitForRefresh]**: if set to wait_for, Kuzzle will not respond
/// until the update is indexed \\\
/// **[retryOnConflict]**: conflicts may occur if the same document gets updated multiple times within a short timespan, in a database cluster. 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. \\\
/// **[source]**: if set to true Kuzzle will return the updated document body in the response \\\
Future<Map<String, dynamic>> upsert(
String index,
String collection,
String id,
Map<String, dynamic> changes, {
Map<String, dynamic> defaults,
bool waitForRefresh = false,
int retryOnConflict,
bool source,
}) async {
final response = await kuzzle.query(KuzzleRequest(
controller: name,
action: 'upsert',
index: index,
collection: collection,
uid: id,
body: {
'changes': changes,
'defaults': defaults
},
waitForRefresh: waitForRefresh,
source: source,
retryOnConflict: retryOnConflict,
));

return response.result as Map<String, dynamic>;
}

/// Validates data against existing validation rules.
///
/// Documents are always valid if no validation rules are defined
Expand Down
1 change: 0 additions & 1 deletion lib/src/kuzzle/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ class KuzzleRequest {
if (source != null) {
map['source'] = source;
}

if (includeKuzzleMeta != null) {
map['includeKuzzleMeta'] = includeKuzzleMeta;
}
Expand Down

0 comments on commit 0fa1e18

Please sign in to comment.