Skip to content

Commit

Permalink
Update store interface changes (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname authored Nov 4, 2024
1 parent ed050e8 commit 6543de3
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions docs/indexer/build/mapping/store.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Following is a summary of the `Store` interface:

```ts
export type GetOptions<T> = {
limit: number;
offset?: number;
limit?: number;
/* Order fields are only available in SDK versions >= 4.0.0 */
orderBy?: keyof T;
orderDirection?: "ASC" | "DESC";
Expand All @@ -27,14 +27,14 @@ export interface Store {
operator: "=" | "!=" | "in" | "!in",
value: T[keyof T] | Array<T[keyof T]>,
][],
options?: GetOptions<T>,
options: GetOptions<T>,
): Promise<T[]>;

getByField(
entity: string,
field: string,
value: any,
options?: GetOptions<T>,
options: GetOptions<T>,
): Promise<Entity[]>;

getOneByField(
Expand Down Expand Up @@ -68,8 +68,8 @@ await store.get(`TransactionEntity`, id);

```ts
export type GetOptions<T> = {
limit: number;
offset?: number;
limit?: number;
/* Order fields are only available in SDK versions >= 4.0.0 */
orderBy?: keyof T;
orderDirection?: "ASC" | "DESC";
Expand All @@ -83,7 +83,7 @@ export interface Store {
operator: "=" | "!=" | "in" | "!in",
value: T[keyof T] | Array<T[keyof T]>,
][],
options?: GetOptions<T>,
options: GetOptions<T>,
): Promise<T[]>;
}
```
Expand All @@ -109,36 +109,36 @@ By default ordering is done by `id` in ascending order.
Using the store directly:

```ts
// Get all records with ChainID == 50 AND AccountID == '0xSomeAddress'
// Get the first 100 records with ChainID == 50 AND AccountID == '0xSomeAddress'
await store.getByFields(`TransactionEntity`, [
["ChainID", "=", 50],
["AccountID", "=", "0xSomeAddress"],
]);
], { limit: 100 });
```

Using an entity, this will provide better type safety:

```ts
// Get all records with ChainID == 50 AND AccountID == '0xSomeAddress'
// Get the first 100 records with ChainID == 50 AND AccountID == '0xSomeAddress'
await TransactionEntity.getByFields([
["ChainID", "=", 50],
["AccountID", "=", "0xSomeAddress"],
]);
], { limit: 100 });
```

It's also possible to match multiple values to a field (in this case an OR operation is applied):

```ts
// Get all records with ChainID == 50 OR ChainID == 51
await TransactionEntity.getByFields([["ChainID", "in", [50, 51]]]);
// Get the first 100 records with ChainID == 50 OR ChainID == 51
await TransactionEntity.getByFields([["ChainID", "in", [50, 51]]], { limit: 100 });
```

## Get Records by a Single Field

```ts
export type GetOptions<T> = {
limit: number;
offset?: number;
limit?: number;
/* Order fields are only available in SDK versions >= 4.0.0 */
orderBy?: keyof T;
orderDirection?: "ASC" | "DESC";
Expand All @@ -149,24 +149,24 @@ export interface Store {
entity: string,
field: string,
value: any,
options?: GetOptions,
options: GetOptions,
): Promise<Entity[]>;
}
```

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);
// Get the first 100 records with ChainID == 50
await store.getByField(`TransactionEntity`, "ChainID", 50, { limit: 100 });
```

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 the first 100 records with ChainID == 50 OR ChainID == 100 OR ChainID == 150
await store.getByField("TransactionEntity", "ChainID", [50, 100, 150], { limit: 100 });
```

## Get First Record by Field
Expand Down

0 comments on commit 6543de3

Please sign in to comment.