Skip to content

Defined in: src/query/queryBuilder.ts:350

QueryBuilder is a helper class to build queries to the Arkiv DBChains. It can be used to fetch entities from the Arkiv DBChains. It follows the Builder pattern allowing chaining of methods.

By default the result includes only the entity key. Additional data is opt-in through withAttributes(), withMetadata() and withPayload().

The Arkiv client

new QueryBuilder(client): QueryBuilder

Defined in: src/query/queryBuilder.ts:95

ArkivClient

QueryBuilder

BaseQueryBuilder.constructor

count(): Promise<number>

Defined in: src/query/queryBuilder.ts:317

Counts the entities from the query.

Promise<number>

The number of entities

const builder = client.select()
const result = await builder.where(eq("name", "John")).count()
// result = 10

BaseQueryBuilder.count


createdBy(createdBy): this

Defined in: src/query/queryBuilder.ts:123

Sets the createdBy filter

`0x${string}`

The address of the creator

this

The query builder instance

const builder = client.select()
builder.createdBy("0x1234567890123456789012345678901234567890")

BaseQueryBuilder.createdBy


cursor(cursor): this

Defined in: src/query/queryBuilder.ts:222

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).

string

The cursor to set which tells to RPC Query server where to start or continue the query.

this

The query builder instance

const builder = client.select()
builder.cursor("0xABC123")

BaseQueryBuilder.cursor


fetch(): Promise<QueryResult<Entity>>

Defined in: src/query/queryBuilder.ts:288

Fetches the entities from the query. It will return a QueryResult instance which can be used to fetch the next and previous pages.

Promise<QueryResult<Entity>>

The QueryResult instance QueryResult

const builder = client.select()
const result = await builder.where(eq("name", "John")).fetch()
// result = { entities: [Entity, Entity, Entity], next: async () => QueryResult, previous: async () => QueryResult }

BaseQueryBuilder.fetch


limit(limit): this

Defined in: src/query/queryBuilder.ts:208

Sets the limit for the query

number

The number of entities to return

this

The query builder instance

const builder = client.select()
builder.limit(10)

BaseQueryBuilder.limit


orderBy(attributeName, attributeType, order?): this

Defined in: src/query/queryBuilder.ts:148

Sets the orderBy for the query. It can be called multiple times to order by multiple attributes. The order of the attributes is important. The first attribute is the primary order by attribute. You can use the helper functions asc() and desc() as input for this method.

string

The name of the attribute to order by

The type of the attribute to order by (string or number)

"string" | "number"

The order to set the order by (asc or desc)

"asc" | "desc"

this

The query builder instance

const builder = client.select()
builder.orderBy("name", "string", "desc")
builder.orderBy(asc("name", "string"))
builder.orderBy(desc("name", "string"))

BaseQueryBuilder.orderBy

orderBy(orderByAttribute): this

Defined in: src/query/queryBuilder.ts:165

Sets the orderBy for the query. This method takes the OrderByAttribute object as an argument and is mainly used to use the helper functions asc() and desc() to create the OrderByAttribute instances.

OrderByAttribute

The OrderByAttribute instance to set

this

The query builder instance

const builder = client.select()
builder.orderBy(asc("name", "string"))
builder.orderBy(desc("name", "string"))

BaseQueryBuilder.orderBy


ownedBy(ownedBy): this

Defined in: src/query/queryBuilder.ts:109

Sets the ownedBy filter

`0x${string}`

The address of the owner

this

The query builder instance

const builder = client.select()
builder.ownedBy("0x1234567890123456789012345678901234567890")

BaseQueryBuilder.ownedBy


validAtBlock(validAtBlock): this

Defined in: src/query/queryBuilder.ts:237

Sets the validAtBlock for the query which tells at which block height the state we are intested. If not set, the latest block is used.

bigint

The block number to set

this

The query builder instance

const builder = client.select()
builder.validAtBlock(10000)

BaseQueryBuilder.validAtBlock


where(predicates): this

Defined in: src/query/queryBuilder.ts:259

Sets the predicates for the query limiting the results. It can be a single predicate, multiple predicates passed as separate arguments, or an array of predicates - all combined with ‘and’. Predicates can be nested using ‘or’ and ‘and’ predicates.

Predicate[]

The predicates to set, either as a single array or as separate arguments

this

The query builder instance

const builder = client.select()
builder.where(eq("name", "John"))
builder.where(eq("name", "John"), eq("age", 30))
builder.where([eq("name", "John"), eq("age", 30)])
builder.where(eq("name", "John"), or(eq("age", 30), eq("age", 31)))
builder.where(eq("name", "John"), and(eq("age", 30), eq("age", 31)))
builder.where(eq("name", "John"), or(eq("age", 30), and(eq("age", 31), eq("age", 32))))
builder.where(eq("name", "John"), and(eq("age", 30), or(eq("age", 31), eq("age", 32))))

BaseQueryBuilder.where

where(…predicates): this

Defined in: src/query/queryBuilder.ts:260

Sets the predicates for the query limiting the results. It can be a single predicate, multiple predicates passed as separate arguments, or an array of predicates - all combined with ‘and’. Predicates can be nested using ‘or’ and ‘and’ predicates.

Predicate[]

The predicates to set, either as a single array or as separate arguments

this

The query builder instance

const builder = client.select()
builder.where(eq("name", "John"))
builder.where(eq("name", "John"), eq("age", 30))
builder.where([eq("name", "John"), eq("age", 30)])
builder.where(eq("name", "John"), or(eq("age", 30), eq("age", 31)))
builder.where(eq("name", "John"), and(eq("age", 30), eq("age", 31)))
builder.where(eq("name", "John"), or(eq("age", 30), and(eq("age", 31), eq("age", 32))))
builder.where(eq("name", "John"), and(eq("age", 30), or(eq("age", 31), eq("age", 32))))

BaseQueryBuilder.where


withAttributes(withAttributes): this

Defined in: src/query/queryBuilder.ts:364

Sets the withAttributes flag which will return the attributes for the entities if true

boolean = true

The boolean value to set

this

The QueryBuilder instance

const builder = client.buildQuery()
builder.withAttributes(true)

withMetadata(withMetadata): this

Defined in: src/query/queryBuilder.ts:378

Sets the withMetadata flag which will return the metadata (like owner, expiredAt, etc.) for the entities if true

boolean = true

The boolean value to set

this

The QueryBuilder instance

const builder = client.buildQuery()
builder.withMetadata(true)

withPayload(withPayload): this

Defined in: src/query/queryBuilder.ts:392

Sets the withPayload flag which will return the payload for the entities if true

boolean = true

The boolean value to set

this

The QueryBuilder instance

const builder = client.buildQuery()
builder.withPayload(true)