Skip to content

Commit cfd8b52

Browse files
author
Adrien Maret
authored
Merge pull request #687 from kuzzleio/7.9.0-proposal
# [7.9.0](https://github.com/kuzzleio/sdk-javascript/releases/tag/7.9.0) (2022-03-16) #### New features - [ [#679](#679) ] Add BatchController for performant document writing ([Aschen](https://github.com/Aschen)) #### Enhancements - [ [#686](#686) ] Add KDocument<T> strong param type for Kuzzle Document ([Aschen](https://github.com/Aschen)) ---
2 parents 6226063 + bc3ad54 commit cfd8b52

File tree

31 files changed

+1861
-177
lines changed

31 files changed

+1861
-177
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
code: true
3+
type: page
4+
title: constructor
5+
description: BatchController constructor method
6+
---
7+
8+
# constructor
9+
10+
<SinceBadge version="7.9.0" />
11+
12+
Instantiate a new BatchController.
13+
14+
Each instance will start a timer to periodically send batch requests.
15+
16+
## Arguments
17+
18+
```js
19+
const batch = new BatchController(sdk, options);
20+
```
21+
22+
<br/>
23+
24+
| Argument | Type | Description |
25+
| --------- | ----------------- | ------------------ |
26+
| `sdk` | <pre>Kuzzle</pre> | SDK instance |
27+
| `options` | <pre>object</pre> | Additional options |
28+
29+
## options
30+
31+
* `interval`: Timer interval in ms (10). Actions will be executed every {interval} ms
32+
* `maxWriteBufferSize`: Max write buffer size (200). (Should match config "limits.documentsWriteCount")
33+
* `maxReadBufferSize`: Max read buffer size (10000). (Should match config "limits.documentsReadCount")
34+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
code: true
3+
type: page
4+
title: dispose
5+
description: BatchController dispose method
6+
---
7+
8+
# dispose
9+
10+
<SinceBadge version="7.9.0" />
11+
12+
Dispose the instance.
13+
14+
This method has to be called to destroy the underlaying timer sending batch requests.
15+
16+
## Arguments
17+
18+
```js
19+
dispose (): Promise<void>
20+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
code: true
3+
type: branch
4+
title: BatchController
5+
description: BatchController class documentation
6+
---
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
code: false
3+
type: page
4+
title: Introduction
5+
description: BatchController class
6+
order: 0
7+
---
8+
9+
# BatchController
10+
11+
<SinceBadge version="7.9.0" />
12+
13+
This class is an overload of the document controller and can be switched "as-is" in your code.
14+
15+
It allows to group API actions into batches to significantly increase performances.
16+
17+
The following methods will be executed by batch using
18+
m* actions:
19+
- create => mCreate
20+
- replace => mReplace
21+
- createOrReplace => mCreateOrReplace
22+
- update => mUpdate
23+
- get => mGet
24+
- exists => mGet
25+
- delete => mDelete
26+
27+
::: warning
28+
Standard API errors will not be available.
29+
Except for the `services.storage.not_found` error.
30+
:::
31+
32+
By default, the BatchController sends a batch of documents every 10ms. This can be configured when instantiating the BatchController through the `options.interval` constructor parameter.
33+
34+
::: info
35+
Depending on your workload, you may want to increase the timer interval to execute bigger batches.
36+
A bigger interval will also mean more time between two batches and potentially degraded performances.
37+
The default value of 10ms offers a good balance between batch size and maximum delay between two batches and should be suitable for most situations.
38+
:::
39+
40+
**Example:**
41+
42+
```js
43+
import { BatchController, Kuzzle, Http } from 'kuzzle';
44+
45+
const sdk = new Kuzzle(new Http('localhost'));
46+
47+
const batch = new BatchController(sdk);
48+
49+
// Same as sdk.document.exists but executed in a batch
50+
const exists = await batch.exists('city', 'galle', 'dana');
51+
52+
if (exists) {
53+
// Same as sdk.document.update but executed in a batch
54+
await batch.update('city', 'galle', 'dana', { power: 'off' });
55+
}
56+
else {
57+
// Same as sdk.document.create but executed in a batch
58+
await batch.create('city', 'galle', { power: 'off' }, 'dana');
59+
}
60+
61+
// Original sdk.document.search method
62+
const results = await batch.search('city', 'galle', {});
63+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
code: false
3+
type: page
4+
title: Batch Processing
5+
description: Process batches of data with the SDK
6+
order: 600
7+
---
8+
9+
# Batch Processing
10+
11+
Most of the methods of the Document controller have a batch alternative:
12+
- create => mCreate
13+
- replace => mReplace
14+
- createOrReplace => mCreateOrReplace
15+
- update => mUpdate
16+
- get => mGet
17+
- exists => mGet
18+
- delete => mDelete
19+
20+
Those methods can be used to process batches of documents at once and increase performances.
21+
22+
## BatchController
23+
24+
Although the m* methods offer very good performances when handling documents, they will need a refactor of the code and architecture.
25+
26+
Instead the [BatchController](/sdk/js/7/core-classes/batch-controller/introduction) provides a consistent way to deal with documents per batch.
27+
28+
It overloads the original DocumentController but methods will be executed in batch at a fixed interval.
29+
30+
The BatchController is usable without modifying the original code, just by replacing the original calls to `document.*` to `batch.*`
31+
32+
::: info
33+
The BatchController can be used with [strong typing](/sdk/js/7/essentials/strong-typing) like the Document controller.
34+
35+
```js
36+
const doc = await batch.get<DeviceContent>('nyc-open-data', 'yellow-taxi', 'aschen');
37+
```
38+
39+
:::
40+
41+
42+
**Example:**
43+
44+
```js
45+
import { BatchController, Kuzzle, Http } from 'kuzzle';
46+
47+
const sdk = new Kuzzle(new Http('localhost'));
48+
49+
const batch = new BatchController(sdk);
50+
51+
// Same as sdk.document.exists but executed in a batch
52+
const exists = await batch.exists('city', 'galle', 'dana');
53+
54+
if (exists) {
55+
// Same as sdk.document.update but executed in a batch
56+
await batch.update('city', 'galle', 'dana', { power: 'off' });
57+
}
58+
else {
59+
// Same as sdk.document.create but executed in a batch
60+
await batch.create('city', 'galle', { power: 'off' }, 'dana');
61+
}
62+
63+
// Original sdk.document.search method
64+
const results = await batch.search('city', 'galle', {});
65+
```
66+
67+
::: warning
68+
Standard API errors will not be available.
69+
Except for the `services.storage.not_found` error.
70+
:::
71+
72+
By default, the BatchController send a batch of document every 10ms. This can be configured when instantiating the BatchController through the `options.interval` [constructor](/sdk/js/7/core-classes/batch-controller/constructor) parameter.
73+
74+
::: info
75+
Depending on your load, you may want to increase the timer interval to execute bigger batch.
76+
A bigger interval will also mean more time between two batch and potentialy degraded performances.
77+
The default value of 10ms offer a good balance between batch size and maximum delay between two batch and should be suitable for most situations.
78+
:::
79+
80+

doc/7/essentials/realtime-application/index.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ const doc = await observer.get('nyc-open-data', 'yellow-taxi', 'aschen');
5656
*/
5757
```
5858

59+
::: info
60+
The Observer controller can be used with [strong typing](/sdk/js/7/essentials/strong-typing) like the Document controller.
61+
62+
```js
63+
const doc = await observer.get<DeviceContent>('nyc-open-data', 'yellow-taxi', 'aschen');
64+
```
65+
66+
:::
67+
5968
### Retrieve realtime documents
6069

6170
Realtime documents can be retrieved by using one of those methods:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
code: false
3+
type: page
4+
title: Strong Typing
5+
description: Use strong typing for more safety
6+
order: 700
7+
---
8+
9+
# Strong Typing
10+
11+
The SDK exposes numerous types to help Typescript developer to maintain a safer codebase and prevents obvious errors.
12+
13+
## Kuzzle Document (KDocument)
14+
15+
The Document controller methods can be used with an explicit type that represents the content of the manipulated document.
16+
17+
Document content must be defined by extending the `KDocumentContent` interface.
18+
19+
```js
20+
interface DeviceContent extends KDocumentContent {
21+
model: string;
22+
reference: string;
23+
battery: number;
24+
}
25+
26+
const device = await sdk.document.get<DeviceContent>('iot', 'devices', 'abeeway-H72K2');
27+
```
28+
29+
The returned type is `KDocument<DeviceContent>` and it contains a `_source` property of type `DeviceContent`.
30+
31+
::: info
32+
By default, a generic document content with only a strongly defined `_kuzzle_info` property is returned.
33+
:::

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export * from './src/core/searchResult/Specifications';
2121
export * from './src/core/searchResult/User';
2222
export * from './src/core/Observer';
2323
export * from './src/core/RealtimeDocument';
24+
export * from './src/core/batchWriter/BatchController';
2425

2526
export * from './src/types';
2627

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "7.8.4",
3+
"version": "7.9.0",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

0 commit comments

Comments
 (0)