From 8cafde112c8fbb69a68c063546ad82a729739f24 Mon Sep 17 00:00:00 2001 From: Scott Twiname Date: Thu, 4 Apr 2024 13:49:56 +1300 Subject: [PATCH] Update store documentation and include v4 specific changes (#505) * Update store documentation and include v4 specific changes * Update docs --------- Co-authored-by: James Bayly --- docs/build/mapping/store.md | 132 ++++++++++++++++++++++++------------ 1 file changed, 89 insertions(+), 43 deletions(-) diff --git a/docs/build/mapping/store.md b/docs/build/mapping/store.md index 7d7d0f914f2..a5a6fd07bb5 100644 --- a/docs/build/mapping/store.md +++ b/docs/build/mapping/store.md @@ -9,30 +9,38 @@ Note that there are additional methods autogenerated with your entities that als Following is a summary of the `Store` interface: ```ts +export type GetOptions = { + offset?: number; + limit?: number; + /* Order fields are only available in SDK versions >= 4.0.0 */ + orderBy?: keyof T; + orderDirection?: "ASC" | "DESC"; +}; + export interface Store { get(entity: string, id: string): Promise; - getByField( - entity: string, - field: string, - value: any, - options?: { limit?: number; offset?: number }, - ): Promise; - getByFields( entity: string, filter: [ field: keyof T, operator: "=" | "!=" | "in" | "!in", - value: T[keyof T] | Array, + value: T[keyof T] | Array ][], - options?: { offset?: number; limit?: number }, + options?: GetOptions ): Promise; - getOneByField( + getByField( entity: string, field: string, value: any, + options?: GetOptions + ): Promise; + + getOneByField( + entity: string, + field: string, + value: any ): Promise; set(entity: string, id: string, data: Entity): Promise; @@ -42,13 +50,6 @@ export interface Store { bulkUpdate(entity: string, data: Entity[], fields?: string[]): Promise; remove(entity: string, id: string): Promise; - - count( - entity: string, - field?: string, - value?: any, - options?: { distinct?: boolean; col?: string }, - ): Promise; } ``` @@ -63,39 +64,48 @@ const id = block.block.header.hash.toString(); await store.get(`TransactionEntity`, id); ``` -## Get Records by a Single Field - -`getByField(entity: string, field: string, value: any, options?: { limit?: number; offset?: number }): Promise;` - -This returns matching records for the specific entity that matches an equality comparison. By default it will return the first 100 results. -The number of results can be changed via the `query-limit` flag for the node or via the options field. If you need more than the number of results provided you can also specify an `offset` and page your results. +## Get Records by Fields ```ts -// Get all records with ChainID == 50 -await store.getByField(`TransactionEntity`, "ChainID", 50); -``` - -Please note, the third parameter also accepts array, you can consider this similar like `bulkGet` with OR search. -To get a list of records with `ChainID` equal to 50, 100 or 150: - -```ts -// Get all records with ChainID == 50 OR ChainID == 100 OR ChainID == 150 -await store.getByField("TransactionEntity", "ChainID", [50, 100, 150]); -``` - -## Get Records by Field +export type GetOptions = { + offset?: number; + limit?: number; + /* Order fields are only available in SDK versions >= 4.0.0 */ + orderBy?: keyof T; + orderDirection?: "ASC" | "DESC"; +}; -```ts -getByFields( +export interface Store { + getByFields( entity: string, - filter: [field: keyof T, operator: '=' | '!=' | 'in' | '!in', value: T[keyof T] | Array][], - options?: {offset?: number; limit?: number} + filter: [ + field: keyof T, + operator: "=" | "!=" | "in" | "!in", + value: T[keyof T] | Array + ][], + options?: GetOptions ): Promise; +} ``` This returns all matching records for the specific entity that matches the given filter(s). Each entry in the filter is an AND operation. By default it will return the first 100 results. + The number of results can be changed via the `query-limit` flag for the node or via the options field. If you need more than the number of results provided, we recommend you specify an `offset` and paginate through your results. +The store has a cache layer in order to increase performance, because of this the returned results will always return data that is in the cache followed by data that is in the database. This is required to ensure pagination works. + +Only fields with an index can be filtered on and an error will be thrown if the fields are not indexed. To add an index the projects graphql schema will need to be updated to include [@index](/build/graphql.html#indexing) decorators. + +### Ordering + +::: info Note +Ordering is only available in SDK versions >= 4.0.0 +::: + +By default ordering is done by `id` in ascending order. + +### Examples + Using the store directly: ```ts @@ -120,14 +130,50 @@ It's also possible to match multiple values to a field (in this case an OR opera ```ts // Get all records with ChainID == 50 OR ChainID == 51 -await TransactionEntity.getByFields([["ChainID", "=", [50, 51]]]); +await TransactionEntity.getByFields([["ChainID", "in", [50, 51]]]); +``` + +## Get Records by a Single Field + +```ts +export type GetOptions = { + offset?: number; + limit?: number; + /* Order fields are only available in SDK versions >= 4.0.0 */ + orderBy?: keyof T; + orderDirection?: "ASC" | "DESC"; +}; + +export interface Store { + getByField( + entity: string, + field: string, + value: any, + options?: GetOptions + ): Promise; +} +``` + +This is a convenient wrapper for [getByFields](#get-records-by-field) but only accepts a single filter and uses the `=` or `in` operator. + +```ts +// Get all records with ChainID == 50 +await store.getByField(`TransactionEntity`, "ChainID", 50); +``` + +Please note, the third parameter also accepts array, you can consider this similar like `bulkGet` with OR search. +To get a list of records with `ChainID` equal to 50, 100 or 150: + +```ts +// Get all records with ChainID == 50 OR ChainID == 100 OR ChainID == 150 +await store.getByField("TransactionEntity", "ChainID", [50, 100, 150]); ``` ## Get First Record by Field -`getOneByField(entity: string, field: string, value: any): Promise;` +`getOneByField(entity: string, field: string, value: any): Promise;` -This returns the first matching record for the specific entity that matches a given search. +This returns the first matching record for the specific entity that matches a given search. This is a convenient wrapper for [getByFields](#get-records-by-field) that allows filtering by a field and returns a single result. ```ts const ChainIDValue = 50;