Skip to content

Commit da78305

Browse files
authored
Add Observer class (#671)
Add an `Observer` class to manipulate `RealtimeDocument`. Those documents have the same structure as the `Document` object from the Document Controller except that they are connected to the realtime engine and their content will mutate when a realtime notification about changes is received. ```js const observer = new Observer(sdk); // SearchResult containing realtime documents let realtimeResult = await observer.search('nyc-open-data', 'yellow-taxi', { query }, options); // Dispose ressources (realtime connection + memory) observer.stop('nyc-open-data', 'yellow-taxi'); ```
1 parent 948a042 commit da78305

File tree

20 files changed

+1360
-131
lines changed

20 files changed

+1360
-131
lines changed

doc/7/controllers/auth/search-api-keys/snippets/search-api-keys-es.test.yml

Lines changed: 0 additions & 20 deletions
This file was deleted.

doc/7/controllers/security/get-profile-mapping/snippets/get-profile-mapping.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@ try {
33

44
console.log(response);
55
/*
6-
{ mapping:
7-
{ policies:
8-
{ properties:
9-
{ restrictedTo:
10-
{ properties:
11-
{ collections:
12-
{ type: 'text',
13-
fields: { keyword: { type: 'keyword', ignore_above: 256 } } },
14-
index:
15-
{ type: 'text',
16-
fields: { keyword: { type: 'keyword', ignore_above: 256 } } } } },
17-
roleId: { type: 'keyword' } } } } }
6+
{
7+
"mapping": {
8+
"policies": {
9+
"properties": {
10+
"roleId": {
11+
"type": "keyword"
12+
},
13+
"restrictedTo": {
14+
"properties": {
15+
"collections": {
16+
"type": "text"
17+
}
18+
},
19+
"index": {
20+
"type": "text"
21+
}
22+
}
23+
}
24+
},
25+
}
26+
}
1827
*/
1928
} catch (e) {
2029
console.error(e);

doc/7/controllers/security/get-profile-mapping/snippets/get-profile-mapping.test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ name: security#getProfileMapping
22
description: get profile mapping
33
template: default
44
expected:
5-
- "mapping: { policies: { properties: \\[Object\\] } }"
5+
- "mapping: { policies: { properties: \\[Object\\]"

doc/7/controllers/security/search-api-keys/snippets/search-api-keys-es.test.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
code: true
3+
type: page
4+
title: get
5+
description: Observer get method
6+
---
7+
8+
# get
9+
10+
<SinceBadge version="auto-version" />
11+
12+
Gets a realtime document
13+
14+
::: info
15+
This method uses the [Document.get](/sdk/js/7/controllers/document/get) method under the hood to retrieve document.
16+
:::
17+
18+
## Arguments
19+
20+
```js
21+
get (index: string, collection: string, id: string, options: any): Promise<RealtimeDocument>
22+
```
23+
24+
| Argument | Type | Description |
25+
|----------|------|-------------|
26+
| `index` | <pre>string</pre> | Index name |
27+
| `collection` | <pre>string</pre> | Collection name |
28+
| `id` | <pre>string</pre> | Document ID |
29+
| `options` | <pre>any</pre> | Additional options |
30+
31+
## Usage
32+
33+
```js
34+
const observer = new Observer(sdk);
35+
36+
const doc = await observer.get('nyc-open-data', 'yellow-taxi', 'some-id');
37+
38+
console.log(doc);
39+
/*
40+
RealtimeDocument {
41+
_id: 'some-id',
42+
_source: {
43+
name: 'aschen',
44+
age: '29',
45+
_kuzzle_info: {
46+
author: '-1',
47+
createdAt: 1638432270522,
48+
updatedAt: null,
49+
updater: null
50+
}
51+
},
52+
deleted: false
53+
}
54+
*/
55+
```
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: Observer
5+
description: Observer class documentation
6+
---
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
code: false
3+
type: page
4+
title: Introduction
5+
description: Observer class
6+
order: 0
7+
---
8+
9+
# Observer
10+
11+
<SinceBadge version="auto-version" />
12+
13+
The `Observer` class allows to manipulate realtime documents.
14+
A `RealtimeDocument` is like a normal document from Kuzzle except that it is
15+
connected to the realtime engine and it's content will change with changes
16+
occuring on the database.
17+
18+
They can be retrieved using methods with the same syntax as in the Document
19+
Controller:
20+
21+
```js
22+
const docs = await observer.get('nyc-open-data', 'yellow-taxi', 'foobar');
23+
24+
const result = await observer.search('nyc-open-data', 'yellow-taxi', {
25+
query: { exists: 'licence' }
26+
});
27+
```
28+
29+
Realtime documents are resources that should be disposed via the [Observer.stop](/sdk/js/7/core-classes/observer/stop) method otherwise subscriptions will never be terminated, documents will be kept into memory, which might lead to a memory leak.
30+
31+
```js
32+
await observer.stop('nyc-open-data', 'yellow-taxi');
33+
```
34+
35+
A good frontend practice is to instantiate one observer for the actual page
36+
and/or component(s) displaying realtime documents and to dispose them when
37+
they are not displayed anymore.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
code: true
3+
type: page
4+
title: mGet
5+
description: Observer mGet method
6+
---
7+
8+
# mGet
9+
10+
<SinceBadge version="auto-version" />
11+
12+
13+
Gets multiple realtime documents.
14+
15+
::: info
16+
This method uses the [Document.mGet](/sdk/js/7/controllers/document/m-get) method under the hood to retrieve documents.
17+
:::
18+
19+
## Arguments
20+
21+
```js
22+
mGet (index: string, collection: string, ids: string[]): Promise<{ successes: RealtimeDocument[]; errors: string[]; }>
23+
```
24+
25+
| Argument | Type | Description |
26+
|----------|------|-------------|
27+
| `index` | <pre>string</pre> | Index name |
28+
| `collection` | <pre>string</pre> | Collection name |
29+
| `ids` | <pre>string[]</pre> | Document IDs |
30+
31+
## Usage
32+
33+
```js
34+
const observer = new Observer(sdk);
35+
36+
const docs = await observer.get('nyc-open-data', 'yellow-taxi', ['foo', 'bar']);
37+
38+
console.log(docs);
39+
/*
40+
[
41+
RealtimeDocument {
42+
_id: 'foo',
43+
_source: {
44+
name: 'aschen',
45+
age: '28',
46+
_kuzzle_info: {
47+
author: '-1',
48+
createdAt: 1638432270522,
49+
updatedAt: null,
50+
updater: null
51+
}
52+
},
53+
deleted: false
54+
},
55+
RealtimeDocument {
56+
_id: 'bar',
57+
_source: {
58+
name: 'dana',
59+
age: '30',
60+
_kuzzle_info: {
61+
author: '-1',
62+
createdAt: 1638432270522,
63+
updatedAt: null,
64+
updater: null
65+
}
66+
},
67+
deleted: false
68+
}
69+
]
70+
*/
71+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
code: true
3+
type: page
4+
title: observe
5+
description: Observer observe method
6+
---
7+
8+
# observe
9+
10+
<SinceBadge version="auto-version" />
11+
12+
Retrieve a realtime document from a document
13+
14+
## Arguments
15+
16+
```js
17+
observe (index: string, collection: string, document: Document): Promise<RealtimeDocument>
18+
```
19+
20+
| Argument | Type | Description |
21+
|----------|------|-------------|
22+
| `index` | <pre>string</pre> | Index name |
23+
| `collection` | <pre>string</pre> | Collection name |
24+
| `document` | <pre>Document</pre> | Document to observe |
25+
26+
## Usage
27+
28+
```js
29+
const observer = new Observer(sdk);
30+
31+
const doc = await sdk.document.get('nyc-open-data', 'yellow-taxi', 'some-id');
32+
33+
const realtimeDoc = await observer.observe('nyc-open-data', 'yellow-taxi', doc);
34+
35+
console.log(realtimeDoc);
36+
/*
37+
RealtimeDocument {
38+
_id: 'some-id',
39+
_source: {
40+
name: 'aschen',
41+
age: '29',
42+
_kuzzle_info: {
43+
author: '-1',
44+
createdAt: 1638432270522,
45+
updatedAt: null,
46+
updater: null
47+
}
48+
},
49+
deleted: false
50+
}
51+
*/
52+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
code: true
3+
type: page
4+
title: search
5+
description: Observer search method
6+
---
7+
8+
# search
9+
10+
<SinceBadge version="auto-version" />
11+
12+
Searches for documents and returns a SearchResult containing realtime
13+
documents.
14+
15+
::: info
16+
This method uses the [Document.search](/sdk/js/7/controllers/document/search) method under the hood to retrieve documents.
17+
:::
18+
19+
## Arguments
20+
21+
```js
22+
search (index: string, collection: string, searchBody: JSONObject, options: JSONObject): Promise<RealtimeDocumentSearchResult>
23+
```
24+
25+
| Argument | Type | Description |
26+
|----------|------|-------------|
27+
| `index` | <pre>string</pre> | Index name |
28+
| `collection` | <pre>string</pre> | Collection name |
29+
| `searchBody` | <pre>JSONObject</pre> | Search body |
30+
| `options` | <pre>JSONObject</pre> | Additional options |
31+
32+
## Usage
33+
34+
```js
35+
const observer = new Observer(sdk);
36+
37+
let result = await observer.search('nyc-open-data', 'yellow-taxi', {
38+
query: { exists: 'licence' }
39+
});
40+
41+
console.log(result);
42+
/*
43+
RealtimeDocumentSearchResult {
44+
aggregations: undefined,
45+
hits: [
46+
RealtimeDocument {
47+
_id: 'some-id',
48+
_source: [Object],
49+
deleted: false
50+
},
51+
RealtimeDocument {
52+
_id: 'XaMyen0BDorKTmOQPm2u',
53+
_source: [Object],
54+
deleted: false
55+
}
56+
],
57+
fetched: 2,
58+
total: 2,
59+
suggest: undefined
60+
}
61+
*/
62+
```

0 commit comments

Comments
 (0)