Skip to main content

Interface: Query<TableInfo>

server.Query

The Query interface allows functions to read values out of the database.

If you only need to load an object by ID, use db.get(id) instead.

Executing a query consists of calling

  1. (Optional) order to define the order
  2. (Optional) filter to refine the results
  3. A consumer method to obtain the results

Queries are lazily evaluated. No work is done until iteration begins, so constructing and extending a query is free. The query is executed incrementally as the results are iterated over, so early terminating also reduces the cost of the query.

It is more efficient to use filter expression rather than executing JavaScript to filter.

Ordering
order("asc")Define the order of query results.
Filtering
filter(...)Filter the query results to only the values that match some condition.
ConsumingExecute a query and return results in different ways.
[Symbol.asyncIterator]()The query's results can be iterated over using a for await..of loop.
collect()Return all of the results as an array.
take(n: number)Return the first n results as an array.
first()Return the first result.
unique()Return the only result, and throw if there are no results or more than one result.

To learn more about how to write queries, see Querying the Database.

Type parameters

NameType
TableInfoextends GenericTableInfo

Hierarchy

Methods

[asyncIterator]

[asyncIterator](): AsyncIterator<DocumentByInfo<TableInfo>, any, undefined>

Returns

AsyncIterator<DocumentByInfo<TableInfo>, any, undefined>

Inherited from

OrderedQuery.[asyncIterator]


order

order(order): OrderedQuery<TableInfo>

Define the order of the query output.

Use "asc" for an ascending order and "desc" for a descending order. If not specified, the order defaults to ascending.

Parameters

NameTypeDescription
order"desc" | "asc"The order to return results in.

Returns

OrderedQuery<TableInfo>


filter

filter(predicate): OrderedQuery<TableInfo>

Filter the query output, returning only the values for which predicate evaluates to true.

Parameters

NameTypeDescription
predicate(q: FilterBuilder<TableInfo>) => Expression<boolean>An Expression constructed with the supplied FilterBuilder that specifies which documents to keep.

Returns

OrderedQuery<TableInfo>

Inherited from

OrderedQuery.filter


collect

collect(): Promise<DocumentByInfo<TableInfo>[]>

Execute the query and return all of the results as an array.

Note: when processing a query with a lot of results, it's often better to use the Query as an AsyncIterable instead.

Returns

Promise<DocumentByInfo<TableInfo>[]>

  • An array of all of the query's results.

Inherited from

OrderedQuery.collect


take

take(n): Promise<DocumentByInfo<TableInfo>[]>

Execute the query and return the first n results.

Parameters

NameTypeDescription
nnumberThe number of items to take.

Returns

Promise<DocumentByInfo<TableInfo>[]>

  • An array of the first n results of the query (or less if the query doesn't have n results).

Inherited from

OrderedQuery.take


first

first(): Promise<null | DocumentByInfo<TableInfo>>

Execute the query and return the first result if there is one.

Returns

Promise<null | DocumentByInfo<TableInfo>>

  • The first value of the query or null if the query returned no results.

Inherited from

OrderedQuery.first


unique

unique(): Promise<DocumentByInfo<TableInfo>>

Execute the query and return the singular result.

Throws

Will throw an error if the query returns no results or more than one result.

Returns

Promise<DocumentByInfo<TableInfo>>

  • The single result returned from the query.

Inherited from

OrderedQuery.unique