- Core API Reference
- Collection
- createCollection
- liveQueryCollectionOptions
- createLiveQueryCollection
- createOptimisticAction
- createTransaction
- Electric DB Collection
- electricCollectionOptions
- Query DB Collection
- queryCollectionOptions
- RxDB DB Collection
- rxdbCollectionOptions
- PowerSync Collection
- powerSyncCollectionOptions
- createIndexedDB
- indexedDBCollectionOptions
- React Hooks
- useLiveQuery
queryOnce
Call Signature#
function queryOnce<TQueryFn, TQuery>(queryFn): Promise<InferResultType<ExtractContext<TQuery>>>;Defined in: packages/db/src/query/query-once.ts:59
Executes a one-shot query and returns the results as an array.
This function creates a live query collection, preloads it, extracts the results, and automatically cleans up the collection. It's ideal for:
- AI/LLM context building
- Data export
- Background processing
- Testing
Type Parameters#
TQueryFn#
TQueryFn extends (q) => QueryBuilder<any>
TQuery#
TQuery extends QueryBuilder<any> = ReturnType<TQueryFn>
Parameters#
queryFn#
TQueryFn & RootQueryFn<TQuery>
A function that receives the query builder and returns a query
Returns#
Promise<InferResultType<ExtractContext<TQuery>>>
A promise that resolves to an array of query results
Example#
// Basic query
const users = await queryOnce((q) =>
q.from({ user: usersCollection })
)
// With filtering and projection
const activeUserNames = await queryOnce((q) =>
q.from({ user: usersCollection })
.where(({ user }) => eq(user.active, true))
.select(({ user }) => ({ name: user.name }))
)Call Signature#
function queryOnce<TQuery>(config): Promise<InferResultType<ExtractContext<TQuery>>>;Defined in: packages/db/src/query/query-once.ts:83
Executes a one-shot query using a configuration object.
Type Parameters#
TQuery#
TQuery extends QueryBuilder<any>
Parameters#
config#
QueryOnceConfig<ExtractContext<TQuery>> & object
Configuration object with the query function
Returns#
Promise<InferResultType<ExtractContext<TQuery>>>
A promise that resolves to an array of query results
Example#
const recentOrders = await queryOnce({
query: (q) =>
q.from({ order: ordersCollection })
.orderBy(({ order }) => desc(order.createdAt))
.limit(100),
})