Skip to content

Commit 4772efe

Browse files
author
Adrien Maret
authored
Add KDocument<T> strong param type for Kuzzle Document (#686)
The Document controller methods can be used with an explicit type that represents the content of the manipulated document. Document content must be defined by extending the `KDocumentContent` interface. ```js interface DeviceContent extends KDocumentContent { model: string; reference: string; battery: number; } const device = await sdk.document.get<DeviceContent>('iot', 'devices', 'abeeway-H72K2'); // this is still possible const device = await sdk.document.get('iot', 'devices', 'abeeway-H72K2'); ``` The returned type is `KDocument<DeviceContent>` and it contains a `_source` property of type `DeviceContent`. This is not a breaking change since the `KDocument` type is compatible with the former `Document` type. fix #670
1 parent f314f6f commit 4772efe

File tree

14 files changed

+240
-125
lines changed

14 files changed

+240
-125
lines changed

doc/7/essentials/batch-processing/index.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ It overloads the original DocumentController but methods will be executed in bat
2929

3030
The BatchController is usable without modifying the original code, just by replacing the original calls to `document.*` to `batch.*`
3131

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+
3242
**Example:**
3343

3444
```js

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+
:::

src/controllers/Document.ts

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { BaseController } from './Base';
22
import { DocumentSearchResult } from '../core/searchResult/Document';
33
import {
44
JSONObject,
5-
Document,
65
mCreateResponse,
76
ArgsDefault,
87
mCreateRequest,
@@ -14,7 +13,9 @@ import {
1413
mReplaceResponse,
1514
mUpdateRequest,
1615
mUpdateResponse,
17-
DocumentHit,
16+
KDocumentContentGeneric,
17+
KDocument,
18+
KHit,
1819
} from '../types';
1920
import { SearchResult } from '../core/searchResult/SearchResultBase';
2021

@@ -73,13 +74,13 @@ export class DocumentController extends BaseController {
7374
*
7475
* @returns The created document
7576
*/
76-
create (
77+
create<TKDocumentContent extends KDocumentContentGeneric> (
7778
index: string,
7879
collection: string,
79-
content: JSONObject,
80+
content: Partial<TKDocumentContent>,
8081
_id: string = null,
8182
options: ArgsDocumentControllerCreate = {}
82-
): Promise<Document> {
83+
): Promise<KDocument<TKDocumentContent>> {
8384
const request = {
8485
index,
8586
collection,
@@ -110,13 +111,13 @@ export class DocumentController extends BaseController {
110111
*
111112
* @returns The created or replaced document
112113
*/
113-
createOrReplace (
114+
createOrReplace<TKDocumentContent extends KDocumentContentGeneric> (
114115
index: string,
115116
collection: string,
116117
_id: string,
117-
content: JSONObject,
118+
content: Partial<TKDocumentContent>,
118119
options: ArgsDocumentControllerCreateOrReplace = {}
119-
): Promise<Document> {
120+
): Promise<KDocument<TKDocumentContent>> {
120121
const request = {
121122
index,
122123
collection,
@@ -181,12 +182,12 @@ export class DocumentController extends BaseController {
181182
*
182183
* @returns The deleted documents IDs
183184
*/
184-
deleteByQuery(
185+
deleteByQuery (
185186
index: string,
186187
collection: string,
187188
query: JSONObject = {},
188189
options: ArgsDocumentControllerDeleteByQuery = {}
189-
): Promise<Array<string>> {
190+
): Promise<string[]> {
190191
const request = {
191192
index,
192193
collection,
@@ -217,13 +218,13 @@ export class DocumentController extends BaseController {
217218
*
218219
* @returns The updated document
219220
*/
220-
deleteFields(
221+
deleteFields<TKDocumentContent extends KDocumentContentGeneric> (
221222
index: string,
222223
collection: string,
223224
_id: string,
224225
fields: string[],
225226
options: ArgsDocumentControllerDeleteFields = {}
226-
): Promise<Document> {
227+
): Promise<KDocument<TKDocumentContent>> {
227228
const request = {
228229
index,
229230
collection,
@@ -287,12 +288,12 @@ export class DocumentController extends BaseController {
287288
*
288289
* @returns The document
289290
*/
290-
get (
291+
get<TKDocumentContent extends KDocumentContentGeneric> (
291292
index: string,
292293
collection: string,
293294
_id: string,
294295
options: ArgsDocumentControllerGet = {}
295-
): Promise<Document> {
296+
): Promise<KDocument<TKDocumentContent>> {
296297
const request = {
297298
index,
298299
collection,
@@ -321,10 +322,10 @@ export class DocumentController extends BaseController {
321322
*
322323
* @returns An object containing 2 arrays: "successes" and "errors"
323324
*/
324-
mCreate (
325+
mCreate<TKDocumentContent extends KDocumentContentGeneric> (
325326
index: string,
326327
collection: string,
327-
documents: mCreateRequest,
328+
documents: mCreateRequest<TKDocumentContent>,
328329
options: ArgsDocumentControllerMCreate = {}
329330
): Promise<mCreateResponse> {
330331
const request = {
@@ -357,10 +358,10 @@ export class DocumentController extends BaseController {
357358
*
358359
* @returns An object containing 2 arrays: "successes" and "errors"
359360
*/
360-
mCreateOrReplace (
361+
mCreateOrReplace<TKDocumentContent extends KDocumentContentGeneric> (
361362
index: string,
362363
collection: string,
363-
documents: mCreateOrReplaceRequest,
364+
documents: mCreateOrReplaceRequest<TKDocumentContent>,
364365
options: ArgsDocumentControllerMCreateOrReplace = {}
365366
): Promise<mCreateOrReplaceResponse> {
366367
const request = {
@@ -427,20 +428,20 @@ export class DocumentController extends BaseController {
427428
*
428429
* @returns An object containing 2 arrays: "successes" and "errors"
429430
*/
430-
mGet (
431+
mGet<TKDocumentContent extends KDocumentContentGeneric> (
431432
index: string,
432433
collection: string,
433-
ids: Array<string>,
434+
ids: string[],
434435
options: ArgsDocumentControllerMGet = {}
435436
): Promise<{
436437
/**
437438
* Array of successfully retrieved documents
438439
*/
439-
successes: Array<Document>;
440+
successes: KDocument<TKDocumentContent>[];
440441
/**
441442
* Array of the IDs of not found documents.
442443
*/
443-
errors: Array<string>;
444+
errors: string[];
444445
}> {
445446
const request = {
446447
index,
@@ -470,10 +471,10 @@ export class DocumentController extends BaseController {
470471
*
471472
* @returns An object containing 2 arrays: "successes" and "errors"
472473
*/
473-
mReplace (
474+
mReplace<TKDocumentContent extends KDocumentContentGeneric> (
474475
index: string,
475476
collection: string,
476-
documents: mReplaceRequest,
477+
documents: mReplaceRequest<TKDocumentContent>,
477478
options: ArgsDocumentControllerMReplace = {}
478479
): Promise<mReplaceResponse> {
479480
const request = {
@@ -510,10 +511,10 @@ export class DocumentController extends BaseController {
510511
*
511512
* @returns An object containing 2 arrays: "successes" and "errors"
512513
*/
513-
mUpdate (
514+
mUpdate<TKDocumentContent extends KDocumentContentGeneric> (
514515
index: string,
515516
collection: string,
516-
documents: mUpdateRequest,
517+
documents: mUpdateRequest<TKDocumentContent>,
517518
options: ArgsDocumentControllerMUpdate = {}
518519
): Promise<mUpdateResponse> {
519520
const request = {
@@ -547,10 +548,10 @@ export class DocumentController extends BaseController {
547548
*
548549
* @returns An object containing 2 arrays: "successes" and "errors"
549550
*/
550-
mUpsert (
551+
mUpsert<TKDocumentContent extends KDocumentContentGeneric> (
551552
index: string,
552553
collection: string,
553-
documents: mUpdateRequest,
554+
documents: mUpdateRequest<TKDocumentContent>,
554555
options: ArgsDocumentControllerMUpsert = {}
555556
): Promise<mUpdateResponse> {
556557
const request = {
@@ -582,13 +583,13 @@ export class DocumentController extends BaseController {
582583
*
583584
* @returns The replaced document
584585
*/
585-
replace (
586+
replace<TKDocumentContent extends KDocumentContentGeneric> (
586587
index: string,
587588
collection: string,
588589
_id: string,
589-
content: JSONObject,
590+
content: Partial<TKDocumentContent>,
590591
options: ArgsDocumentControllerReplace = {}
591-
): Promise<Document> {
592+
): Promise<KDocument<TKDocumentContent>> {
592593
const request = {
593594
index,
594595
collection,
@@ -620,12 +621,12 @@ export class DocumentController extends BaseController {
620621
*
621622
* @returns A SearchResult
622623
*/
623-
search (
624+
search<TKDocumentContent extends KDocumentContentGeneric> (
624625
index: string,
625626
collection: string,
626627
searchBody: JSONObject = {},
627628
options: ArgsDocumentControllerSearch = {}
628-
): Promise<SearchResult<DocumentHit>> {
629+
): Promise<SearchResult<KHit<TKDocumentContent>>> {
629630
return this._search(index, collection, searchBody, options)
630631
.then(({ response, request, opts }) => (
631632
new DocumentSearchResult(this.kuzzle, request, opts, response.result)
@@ -680,13 +681,13 @@ export class DocumentController extends BaseController {
680681
*
681682
* @returns The replaced document
682683
*/
683-
update (
684+
update<TKDocumentContent extends KDocumentContentGeneric> (
684685
index: string,
685686
collection: string,
686687
_id: string,
687-
content: JSONObject,
688+
content: Partial<TKDocumentContent>,
688689
options: ArgsDocumentControllerUpdate = {}
689-
): Promise<Document> {
690+
): Promise<KDocument<TKDocumentContent>> {
690691
const request = {
691692
index,
692693
collection,
@@ -720,7 +721,7 @@ export class DocumentController extends BaseController {
720721
*
721722
* @returns An object containing 2 arrays: "successes" and "errors"
722723
*/
723-
updateByQuery(
724+
updateByQuery<TKDocumentContent extends KDocumentContentGeneric> (
724725
index: string,
725726
collection: string,
726727
query: JSONObject,
@@ -730,15 +731,15 @@ export class DocumentController extends BaseController {
730731
/**
731732
* Array of successfully updated documents
732733
*/
733-
successes: Array<Document>;
734+
successes: KDocument<TKDocumentContent>[];
734735
/**
735736
* Array of failed creation
736737
*/
737738
errors: Array<{
738739
/**
739740
* Document that cause the error
740741
*/
741-
document: Document;
742+
document: KDocument<TKDocumentContent>;
742743
/**
743744
* HTTP error status
744745
*/
@@ -781,13 +782,13 @@ export class DocumentController extends BaseController {
781782
*
782783
* @returns Information about the updated document
783784
*/
784-
upsert (
785+
upsert<TKDocumentContent extends KDocumentContentGeneric> (
785786
index: string,
786787
collection: string,
787788
_id: string,
788-
changes: JSONObject,
789+
changes: Partial<TKDocumentContent>,
789790
options: ArgsDocumentControllerUpsert = {}
790-
): Promise<Document> {
791+
): Promise<KDocument<TKDocumentContent>> {
791792
const request = {
792793
index,
793794
collection,
@@ -816,10 +817,10 @@ export class DocumentController extends BaseController {
816817
*
817818
* @returns True if the document is valid
818819
*/
819-
validate (
820+
validate<TKDocumentContent extends KDocumentContentGeneric> (
820821
index: string,
821822
collection: string,
822-
content: JSONObject,
823+
content: TKDocumentContent,
823824
options: ArgsDocumentControllerValidate = {}
824825
): Promise<boolean> {
825826
return this.query({

0 commit comments

Comments
 (0)