本页还没有译文
本页 33 段还没译成Español,下面显示的是英文原文。queryOnce
还没翻译译自英文(tanstack.com)本页 33 段都还没译
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),
})