@arkiv-network/sdk 0.6.8-dev.1 → 0.6.8-dev.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -16
- package/dist/chains/index.d.cts +25 -25
- package/dist/chains/index.d.cts.map +1 -1
- package/dist/chains/index.d.ts +25 -25
- package/dist/consts-AUoL-v42.cjs +18 -0
- package/dist/consts-BV4PR-de.js +7 -0
- package/dist/consts-BV4PR-de.js.map +1 -0
- package/dist/{query-CO6f35AE.d.cts → entity-BSU5-vyY.d.cts} +68 -31
- package/dist/entity-BSU5-vyY.d.cts.map +1 -0
- package/dist/{query-BhdmAzYg.d.ts → entity-CHiZ2Qu1.d.ts} +68 -31
- package/dist/entity-CHiZ2Qu1.d.ts.map +1 -0
- package/dist/{queryBuilder-BvoQXa_u.d.ts → index-CzqHiUAo.d.ts} +202 -74
- package/dist/index-CzqHiUAo.d.ts.map +1 -0
- package/dist/{queryBuilder-zTsKXctT.d.cts → index-gj33r3rd.d.cts} +202 -74
- package/dist/index-gj33r3rd.d.cts.map +1 -0
- package/dist/index.cjs +63 -28
- package/dist/index.d.cts +90 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +91 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +58 -21
- package/dist/index.js.map +1 -1
- package/dist/{payload-Da9gJMG5.d.ts → payload-B2kFJyah.d.ts} +1 -1
- package/dist/{payload-Da9gJMG5.d.ts.map → payload-B2kFJyah.d.ts.map} +1 -1
- package/dist/payload-Claf2jUO.js +13 -0
- package/dist/payload-Claf2jUO.js.map +1 -0
- package/dist/{payload-vwU3Rc6P.cjs → payload-DpA5PA-w.cjs} +0 -17
- package/dist/query/index.cjs +6 -2
- package/dist/query/index.d.cts +3 -3
- package/dist/query/index.d.ts +3 -3
- package/dist/query/index.js +3 -2
- package/dist/query/index.js.map +1 -1
- package/dist/{queryBuilder-DM-YO1Y7.cjs → queryBuilder-CTPwve9u.cjs} +229 -70
- package/dist/{queryBuilder-DonAg1aq.js → queryBuilder-D-ACo-g_.js} +201 -72
- package/dist/queryBuilder-D-ACo-g_.js.map +1 -0
- package/dist/types/index.d.cts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/utils/index.cjs +4 -10
- package/dist/utils/index.d.cts +0 -1
- package/dist/utils/index.d.cts.map +1 -1
- package/dist/utils/index.d.ts +1 -2
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +2 -3
- package/dist/utils/index.js.map +1 -1
- package/package.json +11 -15
- package/src/actions/public/buildQuery.ts +4 -0
- package/src/actions/wallet/createEntity.ts +12 -2
- package/src/actions/wallet/extendEntity.ts +6 -1
- package/src/actions/wallet/updateEntity.ts +12 -2
- package/src/clients/createPublicClient.test.ts +2 -0
- package/src/clients/decorators/arkivPublic.ts +67 -1
- package/src/clients/decorators/arkivWallet.ts +18 -0
- package/src/errors.ts +18 -0
- package/src/index.ts +0 -3
- package/src/query/engine.ts +22 -13
- package/src/query/index.ts +1 -0
- package/src/query/queryBuilder.ts +189 -94
- package/src/query/queryResult.ts +13 -7
- package/src/query/selectQueryBuilder.test.ts +210 -0
- package/src/query/selection.test.ts +120 -0
- package/src/query/selection.ts +116 -0
- package/src/types/attributes.ts +12 -0
- package/src/utils/arkivTransactions.test.ts +62 -0
- package/src/utils/arkivTransactions.ts +36 -27
- package/src/utils/index.ts +0 -1
- package/src/utils/validation.test.ts +60 -0
- package/src/utils/validation.ts +32 -0
- package/dist/accounts/index.cjs +0 -9
- package/dist/accounts/index.d.cts +0 -1
- package/dist/accounts/index.d.ts +0 -1
- package/dist/accounts/index.js +0 -1
- package/dist/payload-DKBYYjeA.js +0 -18
- package/dist/payload-DKBYYjeA.js.map +0 -1
- package/dist/query-BhdmAzYg.d.ts.map +0 -1
- package/dist/query-CO6f35AE.d.cts.map +0 -1
- package/dist/queryBuilder-BvoQXa_u.d.ts.map +0 -1
- package/dist/queryBuilder-DonAg1aq.js.map +0 -1
- package/dist/queryBuilder-zTsKXctT.d.cts.map +0 -1
- package/src/accounts/index.ts +0 -1
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import type { Hex } from "viem"
|
|
2
2
|
import type { ArkivClient } from "../clients/baseClient"
|
|
3
|
-
import type {
|
|
3
|
+
import type { Entity } from "../types/entity"
|
|
4
|
+
import type { RpcEntity, RpcIncludeData, RpcOrderByAttribute } from "../types/rpcSchema"
|
|
4
5
|
import { entityFromRpcResult } from "../utils/entities"
|
|
5
6
|
import { processQuery } from "./engine"
|
|
6
7
|
import type { Predicate } from "./predicate"
|
|
7
8
|
import { QueryResult } from "./queryResult"
|
|
9
|
+
import { type SelectArg, selectionToIncludeData } from "./selection"
|
|
8
10
|
|
|
9
11
|
export type OrderByAttribute = {
|
|
10
12
|
name: string
|
|
@@ -45,24 +47,37 @@ export function desc(attributeName: string, attributeType: "string" | "number"):
|
|
|
45
47
|
order: "desc",
|
|
46
48
|
}
|
|
47
49
|
}
|
|
50
|
+
|
|
48
51
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* @param client - The Arkiv client
|
|
52
|
-
* @returns The QueryBuilder instance {@link QueryBuilder}
|
|
52
|
+
* Data-selection parameters forwarded to the query engine on each fetch.
|
|
53
|
+
* Subclasses of {@link BaseQueryBuilder} decide which parts of an entity are fetched.
|
|
53
54
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
55
|
+
type SelectionParams = {
|
|
56
|
+
withAttributes?: boolean | undefined
|
|
57
|
+
withMetadata?: boolean | undefined
|
|
58
|
+
withPayload?: boolean | undefined
|
|
59
|
+
includeData?: RpcIncludeData | undefined
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* BaseQueryBuilder holds the query-building logic shared by every query builder
|
|
64
|
+
* (filtering, ordering, pagination, execution). It follows the Builder pattern,
|
|
65
|
+
* allowing methods to be chained. Subclasses decide how data selection is expressed
|
|
66
|
+
* by implementing the protected `selectionParams` method.
|
|
67
|
+
*
|
|
68
|
+
* Use {@link SelectQueryBuilder} via `client.select()` to build and execute queries.
|
|
69
|
+
*
|
|
70
|
+
* @typeParam TEntity - The shape of each entity produced by {@link BaseQueryBuilder.fetch}.
|
|
71
|
+
*/
|
|
72
|
+
export abstract class BaseQueryBuilder<TEntity> {
|
|
73
|
+
protected _client: ArkivClient
|
|
74
|
+
protected _ownedBy: Hex | undefined
|
|
75
|
+
protected _createdBy: Hex | undefined
|
|
76
|
+
protected _orderBy: RpcOrderByAttribute[] | undefined
|
|
77
|
+
protected _validAtBlock: bigint | undefined
|
|
78
|
+
protected _limit: number | undefined
|
|
79
|
+
protected _cursor: string | undefined
|
|
80
|
+
protected _predicates: Predicate[]
|
|
66
81
|
|
|
67
82
|
constructor(client: ArkivClient) {
|
|
68
83
|
this._client = client
|
|
@@ -72,13 +87,13 @@ export class QueryBuilder {
|
|
|
72
87
|
/**
|
|
73
88
|
* Sets the ownedBy filter
|
|
74
89
|
* @param ownedBy - The address of the owner
|
|
75
|
-
* @returns The
|
|
90
|
+
* @returns The query builder instance
|
|
76
91
|
*
|
|
77
92
|
* @example
|
|
78
|
-
* const builder =
|
|
93
|
+
* const builder = client.select()
|
|
79
94
|
* builder.ownedBy("0x1234567890123456789012345678901234567890")
|
|
80
95
|
*/
|
|
81
|
-
ownedBy(ownedBy: Hex) {
|
|
96
|
+
ownedBy(ownedBy: Hex): this {
|
|
82
97
|
this._ownedBy = ownedBy
|
|
83
98
|
return this
|
|
84
99
|
}
|
|
@@ -86,13 +101,13 @@ export class QueryBuilder {
|
|
|
86
101
|
/**
|
|
87
102
|
* Sets the createdBy filter
|
|
88
103
|
* @param createdBy - The address of the creator
|
|
89
|
-
* @returns The
|
|
104
|
+
* @returns The query builder instance
|
|
90
105
|
*
|
|
91
106
|
* @example
|
|
92
|
-
* const builder =
|
|
107
|
+
* const builder = client.select()
|
|
93
108
|
* builder.createdBy("0x1234567890123456789012345678901234567890")
|
|
94
109
|
*/
|
|
95
|
-
createdBy(createdBy: Hex) {
|
|
110
|
+
createdBy(createdBy: Hex): this {
|
|
96
111
|
this._createdBy = createdBy
|
|
97
112
|
return this
|
|
98
113
|
}
|
|
@@ -105,10 +120,10 @@ export class QueryBuilder {
|
|
|
105
120
|
* @param attributeName - The name of the attribute to order by
|
|
106
121
|
* @param attributeType - The type of the attribute to order by (string or number)
|
|
107
122
|
* @param order - The order to set the order by (asc or desc)
|
|
108
|
-
* @returns The
|
|
123
|
+
* @returns The query builder instance
|
|
109
124
|
*
|
|
110
125
|
* @example
|
|
111
|
-
* const builder = client.
|
|
126
|
+
* const builder = client.select()
|
|
112
127
|
* builder.orderBy("name", "string", "desc")
|
|
113
128
|
* builder.orderBy(asc("name", "string"))
|
|
114
129
|
* builder.orderBy(desc("name", "string"))
|
|
@@ -119,10 +134,10 @@ export class QueryBuilder {
|
|
|
119
134
|
* This method takes the OrderByAttribute object as an argument and is mainly
|
|
120
135
|
* used to use the helper functions asc() and desc() to create the OrderByAttribute instances.
|
|
121
136
|
* @param orderByAttribute - The OrderByAttribute instance to set
|
|
122
|
-
* @returns The
|
|
137
|
+
* @returns The query builder instance
|
|
123
138
|
*
|
|
124
139
|
* @example
|
|
125
|
-
* const builder =
|
|
140
|
+
* const builder = client.select()
|
|
126
141
|
* builder.orderBy(asc("name", "string"))
|
|
127
142
|
* builder.orderBy(desc("name", "string"))
|
|
128
143
|
*/
|
|
@@ -131,7 +146,7 @@ export class QueryBuilder {
|
|
|
131
146
|
attributeNameOrOrderByAttribute: string | OrderByAttribute,
|
|
132
147
|
attributeType?: "string" | "number",
|
|
133
148
|
order: "asc" | "desc" = "asc",
|
|
134
|
-
) {
|
|
149
|
+
): this {
|
|
135
150
|
if (!this._orderBy) {
|
|
136
151
|
this._orderBy = []
|
|
137
152
|
}
|
|
@@ -160,58 +175,16 @@ export class QueryBuilder {
|
|
|
160
175
|
return this
|
|
161
176
|
}
|
|
162
177
|
|
|
163
|
-
/**
|
|
164
|
-
* Sets the withAttributes flag which will return the attributes for the entities if true
|
|
165
|
-
* @param withAttributes - The boolean value to set
|
|
166
|
-
* @returns The QueryBuilder instance
|
|
167
|
-
*
|
|
168
|
-
* @example
|
|
169
|
-
* const builder = new QueryBuilder(client)
|
|
170
|
-
* builder.withAttributes(true)
|
|
171
|
-
*/
|
|
172
|
-
withAttributes(withAttributes: boolean = true) {
|
|
173
|
-
this._withAttributes = withAttributes
|
|
174
|
-
return this
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Sets the withMetadata flag which will return the metadata (like owner, expiredAt, etc.) for the entities if true
|
|
179
|
-
* @param withMetadata - The boolean value to set
|
|
180
|
-
* @returns The QueryBuilder instance
|
|
181
|
-
*
|
|
182
|
-
* @example
|
|
183
|
-
* const builder = new QueryBuilder(client)
|
|
184
|
-
* builder.withMetadata(true)
|
|
185
|
-
*/
|
|
186
|
-
withMetadata(withMetadata: boolean = true) {
|
|
187
|
-
this._withMetadata = withMetadata
|
|
188
|
-
return this
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Sets the withPayload flag which will return the payload for the entities if true
|
|
193
|
-
* @param withPayload - The boolean value to set
|
|
194
|
-
* @returns The QueryBuilder instance
|
|
195
|
-
*
|
|
196
|
-
* @example
|
|
197
|
-
* const builder = new QueryBuilder(client)
|
|
198
|
-
* builder.withPayload(true)
|
|
199
|
-
*/
|
|
200
|
-
withPayload(withPayload: boolean = true) {
|
|
201
|
-
this._withPayload = withPayload
|
|
202
|
-
return this
|
|
203
|
-
}
|
|
204
|
-
|
|
205
178
|
/**
|
|
206
179
|
* Sets the limit for the query
|
|
207
180
|
* @param limit - The number of entities to return
|
|
208
|
-
* @returns The
|
|
181
|
+
* @returns The query builder instance
|
|
209
182
|
*
|
|
210
183
|
* @example
|
|
211
|
-
* const builder =
|
|
184
|
+
* const builder = client.select()
|
|
212
185
|
* builder.limit(10)
|
|
213
186
|
*/
|
|
214
|
-
limit(limit: number) {
|
|
187
|
+
limit(limit: number): this {
|
|
215
188
|
this._limit = limit
|
|
216
189
|
return this
|
|
217
190
|
}
|
|
@@ -219,13 +192,13 @@ export class QueryBuilder {
|
|
|
219
192
|
/**
|
|
220
193
|
* Sets the cursor for the query - it is advances setting which rather shouldn't be used manually but it is provided from query result if limit is used (pagination).
|
|
221
194
|
* @param cursor - The cursor to set which tells to RPC Query server where to start or continue the query.
|
|
222
|
-
* @returns The
|
|
195
|
+
* @returns The query builder instance
|
|
223
196
|
*
|
|
224
197
|
* @example
|
|
225
|
-
* const builder =
|
|
226
|
-
* builder.
|
|
198
|
+
* const builder = client.select()
|
|
199
|
+
* builder.cursor("0xABC123")
|
|
227
200
|
*/
|
|
228
|
-
cursor(cursor: string) {
|
|
201
|
+
cursor(cursor: string): this {
|
|
229
202
|
this._cursor = cursor
|
|
230
203
|
return this
|
|
231
204
|
}
|
|
@@ -234,13 +207,13 @@ export class QueryBuilder {
|
|
|
234
207
|
* Sets the validAtBlock for the query which tells at which block height the state we are intested.
|
|
235
208
|
* If not set, the latest block is used.
|
|
236
209
|
* @param validAtBlock - The block number to set
|
|
237
|
-
* @returns The
|
|
210
|
+
* @returns The query builder instance
|
|
238
211
|
*
|
|
239
212
|
* @example
|
|
240
|
-
* const builder =
|
|
213
|
+
* const builder = client.select()
|
|
241
214
|
* builder.validAtBlock(10000)
|
|
242
215
|
*/
|
|
243
|
-
validAtBlock(validAtBlock: bigint) {
|
|
216
|
+
validAtBlock(validAtBlock: bigint): this {
|
|
244
217
|
this._validAtBlock = validAtBlock
|
|
245
218
|
return this
|
|
246
219
|
}
|
|
@@ -249,10 +222,10 @@ export class QueryBuilder {
|
|
|
249
222
|
* Sets the predicates for the query limiting the results. It can be a single predicate or an array of predicates combined with 'and'.
|
|
250
223
|
* Predicates can be nested using 'or' and 'and' predicates.
|
|
251
224
|
* @param predicates - The predicates to set
|
|
252
|
-
* @returns The
|
|
225
|
+
* @returns The query builder instance
|
|
253
226
|
*
|
|
254
227
|
* @example
|
|
255
|
-
* const builder =
|
|
228
|
+
* const builder = client.select()
|
|
256
229
|
* builder.where(eq("name", "John"))
|
|
257
230
|
* builder.where([eq("name", "John"), eq("age", 30)])
|
|
258
231
|
* builder.where([eq("name", "John"), or([eq("age", 30), eq("age", 31)])])
|
|
@@ -261,7 +234,7 @@ export class QueryBuilder {
|
|
|
261
234
|
* builder.where([eq("name", "John"), and([eq("age", 30), or([eq("age", 31), eq("age", 32)])])])
|
|
262
235
|
* builder.where([eq("name", "John"), and([eq("age", 30), or([eq("age", 31), and([eq("age", 32), eq("age", 33)])])])])
|
|
263
236
|
*/
|
|
264
|
-
where(predicates: Predicate[] | Predicate) {
|
|
237
|
+
where(predicates: Predicate[] | Predicate): this {
|
|
265
238
|
if (Array.isArray(predicates)) {
|
|
266
239
|
this._predicates.push(...predicates)
|
|
267
240
|
} else {
|
|
@@ -271,16 +244,28 @@ export class QueryBuilder {
|
|
|
271
244
|
}
|
|
272
245
|
|
|
273
246
|
/**
|
|
274
|
-
*
|
|
247
|
+
* Returns the data-selection parameters forwarded to the query engine.
|
|
248
|
+
* Subclasses decide which parts of an entity are fetched.
|
|
249
|
+
*/
|
|
250
|
+
protected abstract selectionParams(): SelectionParams
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Builds a single result entity from a raw RPC entity. Subclasses decide the produced shape
|
|
254
|
+
* (a full {@link Entity}, or a projected object inferred from the selection).
|
|
255
|
+
*/
|
|
256
|
+
protected abstract projectEntity(rpcEntity: RpcEntity): TEntity | Promise<TEntity>
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Fetches the entities from the query.
|
|
275
260
|
* It will return a QueryResult instance which can be used to fetch the next and previous pages.
|
|
276
261
|
* @returns The QueryResult instance {@link QueryResult}
|
|
277
262
|
*
|
|
278
263
|
* @example
|
|
279
|
-
* const builder =
|
|
264
|
+
* const builder = client.select()
|
|
280
265
|
* const result = await builder.where(eq("name", "John")).fetch()
|
|
281
266
|
* // result = { entities: [Entity, Entity, Entity], next: async () => QueryResult, previous: async () => QueryResult }
|
|
282
267
|
*/
|
|
283
|
-
async fetch() {
|
|
268
|
+
async fetch(): Promise<QueryResult<TEntity>> {
|
|
284
269
|
const queryResult = await processQuery(this._client, {
|
|
285
270
|
predicates: this._predicates,
|
|
286
271
|
limit: this._limit,
|
|
@@ -289,19 +274,15 @@ export class QueryBuilder {
|
|
|
289
274
|
createdBy: this._createdBy,
|
|
290
275
|
orderBy: this._orderBy,
|
|
291
276
|
validAtBlock: this._validAtBlock,
|
|
292
|
-
|
|
293
|
-
withMetadata: this._withMetadata,
|
|
294
|
-
withPayload: this._withPayload,
|
|
277
|
+
...this.selectionParams(),
|
|
295
278
|
})
|
|
296
279
|
|
|
297
|
-
const entities = await Promise.all(
|
|
298
|
-
queryResult.data.map((entity) => entityFromRpcResult(entity)),
|
|
299
|
-
)
|
|
280
|
+
const entities = await Promise.all(queryResult.data.map((entity) => this.projectEntity(entity)))
|
|
300
281
|
|
|
301
282
|
this.cursor(queryResult.cursor)
|
|
302
283
|
this.validAtBlock(BigInt(queryResult.blockNumber ?? 0))
|
|
303
284
|
|
|
304
|
-
return new QueryResult(entities, this, this._cursor, this._limit, this._validAtBlock)
|
|
285
|
+
return new QueryResult<TEntity>(entities, this, this._cursor, this._limit, this._validAtBlock)
|
|
305
286
|
}
|
|
306
287
|
|
|
307
288
|
/**
|
|
@@ -309,7 +290,7 @@ export class QueryBuilder {
|
|
|
309
290
|
* @returns The number of entities
|
|
310
291
|
*
|
|
311
292
|
* @example
|
|
312
|
-
* const builder =
|
|
293
|
+
* const builder = client.select()
|
|
313
294
|
* const result = await builder.where(eq("name", "John")).count()
|
|
314
295
|
* // result = 10
|
|
315
296
|
*/
|
|
@@ -330,3 +311,117 @@ export class QueryBuilder {
|
|
|
330
311
|
return queryResult.data.length ?? 0
|
|
331
312
|
}
|
|
332
313
|
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* QueryBuilder is a helper class to build queries to the Arkiv DBChains.
|
|
317
|
+
* It can be used to fetch entities from the Arkiv DBChains. It follows the Builder pattern allowing chaining of methods.
|
|
318
|
+
*
|
|
319
|
+
* By default the result includes only the entity `key`. Additional data is opt-in through
|
|
320
|
+
* `withAttributes()`, `withMetadata()` and `withPayload()`.
|
|
321
|
+
*
|
|
322
|
+
* @deprecated Use {@link SelectQueryBuilder} via `client.select()` instead. Declaring the
|
|
323
|
+
* selection up front avoids the common mistake of forgetting to opt in to data and getting back
|
|
324
|
+
* entities with only their `key` populated. This class remains for backwards compatibility and
|
|
325
|
+
* will be removed in a future release.
|
|
326
|
+
*
|
|
327
|
+
* @param client - The Arkiv client
|
|
328
|
+
* @returns The QueryBuilder instance {@link QueryBuilder}
|
|
329
|
+
*/
|
|
330
|
+
export class QueryBuilder extends BaseQueryBuilder<Entity> {
|
|
331
|
+
private _withAttributes: boolean | undefined
|
|
332
|
+
private _withMetadata: boolean | undefined
|
|
333
|
+
private _withPayload: boolean | undefined
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Sets the withAttributes flag which will return the attributes for the entities if true
|
|
337
|
+
* @param withAttributes - The boolean value to set
|
|
338
|
+
* @returns The QueryBuilder instance
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* const builder = client.buildQuery()
|
|
342
|
+
* builder.withAttributes(true)
|
|
343
|
+
*/
|
|
344
|
+
withAttributes(withAttributes: boolean = true): this {
|
|
345
|
+
this._withAttributes = withAttributes
|
|
346
|
+
return this
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Sets the withMetadata flag which will return the metadata (like owner, expiredAt, etc.) for the entities if true
|
|
351
|
+
* @param withMetadata - The boolean value to set
|
|
352
|
+
* @returns The QueryBuilder instance
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* const builder = client.buildQuery()
|
|
356
|
+
* builder.withMetadata(true)
|
|
357
|
+
*/
|
|
358
|
+
withMetadata(withMetadata: boolean = true): this {
|
|
359
|
+
this._withMetadata = withMetadata
|
|
360
|
+
return this
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Sets the withPayload flag which will return the payload for the entities if true
|
|
365
|
+
* @param withPayload - The boolean value to set
|
|
366
|
+
* @returns The QueryBuilder instance
|
|
367
|
+
*
|
|
368
|
+
* @example
|
|
369
|
+
* const builder = client.buildQuery()
|
|
370
|
+
* builder.withPayload(true)
|
|
371
|
+
*/
|
|
372
|
+
withPayload(withPayload: boolean = true): this {
|
|
373
|
+
this._withPayload = withPayload
|
|
374
|
+
return this
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
protected selectionParams(): SelectionParams {
|
|
378
|
+
return {
|
|
379
|
+
withAttributes: this._withAttributes,
|
|
380
|
+
withMetadata: this._withMetadata,
|
|
381
|
+
withPayload: this._withPayload,
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
protected projectEntity(rpcEntity: RpcEntity): Promise<Entity> {
|
|
386
|
+
return entityFromRpcResult(rpcEntity)
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* SelectQueryBuilder is the recommended query builder. It requires the selection to be declared
|
|
392
|
+
* up front, so results always contain exactly the data you asked for — and the type of each
|
|
393
|
+
* returned entity is narrowed to exactly the selected fields.
|
|
394
|
+
*
|
|
395
|
+
* The selection is fixed at construction time and is flat — each field maps to an entity field.
|
|
396
|
+
* Create one via `client.select(...)` rather than constructing it directly, so the result type is
|
|
397
|
+
* inferred from the selection.
|
|
398
|
+
*
|
|
399
|
+
* @typeParam TEntity - The projected entity shape, inferred from the selection by `client.select()`.
|
|
400
|
+
*
|
|
401
|
+
* @example
|
|
402
|
+
* // everything
|
|
403
|
+
* await client.select().where(eq("name", "John")).fetch()
|
|
404
|
+
* await client.select("*").where(eq("name", "John")).fetch()
|
|
405
|
+
* // specific fields
|
|
406
|
+
* await client.select({ owner: true, attributes: true }).fetch()
|
|
407
|
+
* // a single field — result is typed (flat) { owner: Hex }
|
|
408
|
+
* await client.select({ owner: true }).fetch()
|
|
409
|
+
*/
|
|
410
|
+
export class SelectQueryBuilder<TEntity> extends BaseQueryBuilder<TEntity> {
|
|
411
|
+
private _includeData: RpcIncludeData
|
|
412
|
+
|
|
413
|
+
constructor(client: ArkivClient, selection?: SelectArg) {
|
|
414
|
+
super(client)
|
|
415
|
+
this._includeData = selectionToIncludeData(selection)
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
protected selectionParams(): SelectionParams {
|
|
419
|
+
return { includeData: this._includeData }
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
protected async projectEntity(rpcEntity: RpcEntity): Promise<TEntity> {
|
|
423
|
+
// The runtime value is a full Entity (with methods); the static type is narrowed to the
|
|
424
|
+
// selected fields via `client.select()`'s inference.
|
|
425
|
+
return (await entityFromRpcResult(rpcEntity)) as unknown as TEntity
|
|
426
|
+
}
|
|
427
|
+
}
|
package/src/query/queryResult.ts
CHANGED
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
import { NoCursorOrLimitError, NoMoreResultsError } from "../errors"
|
|
2
2
|
import type { Entity } from "../types/entity"
|
|
3
3
|
import { getLogger } from "../utils/logger"
|
|
4
|
-
import type {
|
|
4
|
+
import type { BaseQueryBuilder } from "./queryBuilder"
|
|
5
5
|
|
|
6
6
|
const logger = getLogger("query:result")
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* The result of a query. Holds the fetched entities and supports cursor-based pagination.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam TEntity - The shape of each entity, inferred from the query builder
|
|
12
|
+
* (a full {@link Entity}, or a projected object inferred from a `select()` selection).
|
|
13
|
+
*/
|
|
14
|
+
export class QueryResult<TEntity = Entity> {
|
|
15
|
+
entities: TEntity[]
|
|
10
16
|
private _endOfIteration: boolean
|
|
11
17
|
private _cursor: string | undefined
|
|
12
18
|
private _limit: number | undefined
|
|
13
19
|
private _validAtBlock: bigint | undefined
|
|
14
|
-
private _queryBuilder:
|
|
20
|
+
private _queryBuilder: BaseQueryBuilder<TEntity>
|
|
15
21
|
|
|
16
22
|
// Public getters for internal state
|
|
17
|
-
get queryBuilder():
|
|
23
|
+
get queryBuilder(): BaseQueryBuilder<TEntity> {
|
|
18
24
|
return this._queryBuilder
|
|
19
25
|
}
|
|
20
26
|
|
|
@@ -23,8 +29,8 @@ export class QueryResult {
|
|
|
23
29
|
}
|
|
24
30
|
|
|
25
31
|
constructor(
|
|
26
|
-
entities:
|
|
27
|
-
queryBuilder:
|
|
32
|
+
entities: TEntity[],
|
|
33
|
+
queryBuilder: BaseQueryBuilder<TEntity>,
|
|
28
34
|
cursor: string | undefined,
|
|
29
35
|
limit: number | undefined,
|
|
30
36
|
validAtBlock: bigint | undefined,
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, jest, test } from "bun:test"
|
|
2
|
+
import type { ArkivClient } from "../clients/baseClient"
|
|
3
|
+
import type { Entity } from "../types/entity"
|
|
4
|
+
import * as entitiesUtils from "../utils/entities"
|
|
5
|
+
import * as engine from "./engine"
|
|
6
|
+
import { eq, gte } from "./predicate"
|
|
7
|
+
import { SelectQueryBuilder } from "./queryBuilder"
|
|
8
|
+
import { selectionToIncludeData } from "./selection"
|
|
9
|
+
|
|
10
|
+
describe("SelectQueryBuilder", () => {
|
|
11
|
+
let mockClient: ArkivClient
|
|
12
|
+
let mockProcessQuery: any
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
mockClient = {
|
|
16
|
+
request: jest.fn(),
|
|
17
|
+
} as unknown as ArkivClient
|
|
18
|
+
|
|
19
|
+
mockProcessQuery = jest.spyOn(engine, "processQuery")
|
|
20
|
+
jest.spyOn(entitiesUtils, "entityFromRpcResult")
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
jest.restoreAllMocks()
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test("select() with no argument forwards include-data for everything", async () => {
|
|
28
|
+
mockProcessQuery.mockResolvedValue({ data: [] })
|
|
29
|
+
|
|
30
|
+
await new SelectQueryBuilder(mockClient).where(eq("name", "test")).fetch()
|
|
31
|
+
|
|
32
|
+
expect(mockProcessQuery).toHaveBeenCalledWith(
|
|
33
|
+
mockClient,
|
|
34
|
+
expect.objectContaining({
|
|
35
|
+
predicates: [eq("name", "test")],
|
|
36
|
+
includeData: selectionToIncludeData(),
|
|
37
|
+
}),
|
|
38
|
+
)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('select("*") forwards include-data for everything', async () => {
|
|
42
|
+
mockProcessQuery.mockResolvedValue({ data: [] })
|
|
43
|
+
|
|
44
|
+
await new SelectQueryBuilder(mockClient, "*").fetch()
|
|
45
|
+
|
|
46
|
+
expect(mockProcessQuery).toHaveBeenCalledWith(
|
|
47
|
+
mockClient,
|
|
48
|
+
expect.objectContaining({
|
|
49
|
+
includeData: selectionToIncludeData("*"),
|
|
50
|
+
}),
|
|
51
|
+
)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test("select({ owner: true, attributes: true }) forwards the resolved include-data", async () => {
|
|
55
|
+
mockProcessQuery.mockResolvedValue({ data: [] })
|
|
56
|
+
|
|
57
|
+
await new SelectQueryBuilder(mockClient, { owner: true, attributes: true }).fetch()
|
|
58
|
+
|
|
59
|
+
expect(mockProcessQuery).toHaveBeenCalledWith(
|
|
60
|
+
mockClient,
|
|
61
|
+
expect.objectContaining({
|
|
62
|
+
includeData: selectionToIncludeData({ owner: true, attributes: true }),
|
|
63
|
+
}),
|
|
64
|
+
)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test("select({ owner: true }) forwards narrowed include-data", async () => {
|
|
68
|
+
mockProcessQuery.mockResolvedValue({ data: [] })
|
|
69
|
+
|
|
70
|
+
await new SelectQueryBuilder(mockClient, { owner: true }).fetch()
|
|
71
|
+
|
|
72
|
+
const call = mockProcessQuery.mock.calls[0][1]
|
|
73
|
+
expect(call.includeData).toEqual(selectionToIncludeData({ owner: true }))
|
|
74
|
+
expect(call.includeData.owner).toBe(true)
|
|
75
|
+
expect(call.includeData.creator).toBe(false)
|
|
76
|
+
// key is opt-in: not selected here, so it is excluded
|
|
77
|
+
expect(call.includeData.key).toBe(false)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test("does not forward the with* boolean flags", async () => {
|
|
81
|
+
mockProcessQuery.mockResolvedValue({ data: [] })
|
|
82
|
+
|
|
83
|
+
await new SelectQueryBuilder(mockClient, { attributes: true }).fetch()
|
|
84
|
+
|
|
85
|
+
const call = mockProcessQuery.mock.calls[0][1]
|
|
86
|
+
expect(call.withAttributes).toBeUndefined()
|
|
87
|
+
expect(call.withMetadata).toBeUndefined()
|
|
88
|
+
expect(call.withPayload).toBeUndefined()
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test("shares the filtering/ordering/pagination chain methods", async () => {
|
|
92
|
+
mockProcessQuery.mockResolvedValue({ data: [] })
|
|
93
|
+
|
|
94
|
+
const owner = "0x1234567890123456789012345678901234567890" as const
|
|
95
|
+
await new SelectQueryBuilder(mockClient, { attributes: true })
|
|
96
|
+
.where(eq("name", "test"))
|
|
97
|
+
.where(gte("age", 18))
|
|
98
|
+
.ownedBy(owner)
|
|
99
|
+
.createdBy(owner)
|
|
100
|
+
.orderBy("name", "string", "desc")
|
|
101
|
+
.limit(10)
|
|
102
|
+
.cursor("0xABC123")
|
|
103
|
+
.validAtBlock(123n)
|
|
104
|
+
.fetch()
|
|
105
|
+
|
|
106
|
+
expect(mockProcessQuery).toHaveBeenCalledWith(mockClient, {
|
|
107
|
+
predicates: [eq("name", "test"), gte("age", 18)],
|
|
108
|
+
limit: 10,
|
|
109
|
+
cursor: "0xABC123",
|
|
110
|
+
ownedBy: owner,
|
|
111
|
+
createdBy: owner,
|
|
112
|
+
orderBy: [{ name: "name", type: "string", desc: true }],
|
|
113
|
+
validAtBlock: 123n,
|
|
114
|
+
includeData: selectionToIncludeData({ attributes: true }),
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test("count() ignores the selection and requests no data", async () => {
|
|
119
|
+
mockProcessQuery.mockResolvedValue({
|
|
120
|
+
data: [
|
|
121
|
+
{ key: "0xabc", value: "data1" },
|
|
122
|
+
{ key: "0xdef", value: "data2" },
|
|
123
|
+
],
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
const count = await new SelectQueryBuilder(mockClient, "*")
|
|
127
|
+
.where(eq("status", "active"))
|
|
128
|
+
.count()
|
|
129
|
+
|
|
130
|
+
expect(count).toBe(2)
|
|
131
|
+
expect(mockProcessQuery).toHaveBeenCalledWith(mockClient, {
|
|
132
|
+
predicates: [eq("status", "active")],
|
|
133
|
+
limit: undefined,
|
|
134
|
+
cursor: undefined,
|
|
135
|
+
ownedBy: undefined,
|
|
136
|
+
createdBy: undefined,
|
|
137
|
+
orderBy: undefined,
|
|
138
|
+
validAtBlock: undefined,
|
|
139
|
+
withAttributes: false,
|
|
140
|
+
withMetadata: false,
|
|
141
|
+
withPayload: false,
|
|
142
|
+
})
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
describe("result entities", () => {
|
|
146
|
+
test("fetch returns flat Entity instances with the selected data and methods", async () => {
|
|
147
|
+
mockProcessQuery.mockResolvedValue({
|
|
148
|
+
data: [
|
|
149
|
+
{
|
|
150
|
+
key: "0xabc",
|
|
151
|
+
owner: "0xowner",
|
|
152
|
+
value: "0x68656c6c6f", // "hello"
|
|
153
|
+
stringAttributes: [{ key: "category", value: "docs" }],
|
|
154
|
+
numericAttributes: [{ key: "score", value: "0x2a" }],
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
cursor: "",
|
|
158
|
+
blockNumber: "0x1",
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
const result = await new SelectQueryBuilder(mockClient, {
|
|
162
|
+
key: true,
|
|
163
|
+
owner: true,
|
|
164
|
+
attributes: true,
|
|
165
|
+
payload: true,
|
|
166
|
+
}).fetch()
|
|
167
|
+
|
|
168
|
+
expect(result.entities).toHaveLength(1)
|
|
169
|
+
const entity = result.entities[0] as unknown as Entity
|
|
170
|
+
// flat, backwards-compatible field access
|
|
171
|
+
expect(entity.key).toBe("0xabc")
|
|
172
|
+
expect(entity.owner).toBe("0xowner")
|
|
173
|
+
expect(entity.attributes).toEqual([
|
|
174
|
+
{ key: "category", value: "docs" },
|
|
175
|
+
{ key: "score", value: 42 },
|
|
176
|
+
])
|
|
177
|
+
// entity methods are preserved on the result
|
|
178
|
+
expect(typeof entity.toText).toBe("function")
|
|
179
|
+
expect(entity.toText()).toBe("hello")
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
test("toJson decodes a JSON payload", async () => {
|
|
183
|
+
mockProcessQuery.mockResolvedValue({
|
|
184
|
+
data: [{ key: "0xabc", value: "0x7b2261223a317d" }], // {"a":1}
|
|
185
|
+
cursor: "",
|
|
186
|
+
blockNumber: "0x1",
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
const result = await new SelectQueryBuilder(mockClient, { payload: true }).fetch()
|
|
190
|
+
const entity = result.entities[0] as unknown as Entity
|
|
191
|
+
expect(entity.toJson()).toEqual({ a: 1 })
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test("fields the RPC omits (because they were not selected) are undefined at runtime", async () => {
|
|
195
|
+
// The RPC honours includeData and returns only the key.
|
|
196
|
+
mockProcessQuery.mockResolvedValue({
|
|
197
|
+
data: [{ key: "0xabc" }],
|
|
198
|
+
cursor: "",
|
|
199
|
+
blockNumber: "0x1",
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
const result = await new SelectQueryBuilder(mockClient, { key: true }).fetch()
|
|
203
|
+
const entity = result.entities[0] as unknown as Entity
|
|
204
|
+
expect(entity.key).toBe("0xabc")
|
|
205
|
+
expect(entity.owner).toBeUndefined()
|
|
206
|
+
expect(entity.payload).toBeUndefined()
|
|
207
|
+
expect(entity.attributes).toEqual([])
|
|
208
|
+
})
|
|
209
|
+
})
|
|
210
|
+
})
|