Open History to see the last commit
1---
2id: queryOnce
3title: queryOnce
4---
5
6# Function: queryOnce()
7
8## Call Signature
9
10```ts
11function queryOnce<TContext>(queryFn): Promise<InferResultType<TContext>>;
12```
13
14Defined in: [packages/db/src/query/query-once.ts:47](https://github.com/TanStack/db/blob/main/packages/db/src/query/query-once.ts#L47)
15
16Executes a one-shot query and returns the results as an array.
17
18This function creates a live query collection, preloads it, extracts the results,
19and automatically cleans up the collection. It's ideal for:
20- AI/LLM context building
21- Data export
22- Background processing
23- Testing
24
25### Type Parameters
26
27#### TContext
28
29`TContext` *extends* [`Context`](../interfaces/Context.md)
30
31### Parameters
32
33#### queryFn
34
35(`q`) => [`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`TContext`\>
36
37A function that receives the query builder and returns a query
38
39### Returns
40
41`Promise`\<[`InferResultType`](../type-aliases/InferResultType.md)\<`TContext`\>\>
42
43A promise that resolves to an array of query results
44
45### Example
46
47```typescript
48// Basic query
49const users = await queryOnce((q) =>
50 q.from({ user: usersCollection })
51)
52
53// With filtering and projection
54const activeUserNames = await queryOnce((q) =>
55 q.from({ user: usersCollection })
56 .where(({ user }) => eq(user.active, true))
57 .select(({ user }) => ({ name: user.name }))
58)
59```
60
61## Call Signature
62
63```ts
64function queryOnce<TContext>(config): Promise<InferResultType<TContext>>;
65```
66
67Defined in: [packages/db/src/query/query-once.ts:68](https://github.com/TanStack/db/blob/main/packages/db/src/query/query-once.ts#L68)
68
69Executes a one-shot query using a configuration object.
70
71### Type Parameters
72
73#### TContext
74
75`TContext` *extends* [`Context`](../interfaces/Context.md)
76
77### Parameters
78
79#### config
80
81[`QueryOnceConfig`](../interfaces/QueryOnceConfig.md)\<`TContext`\>
82
83Configuration object with the query function
84
85### Returns
86
87`Promise`\<[`InferResultType`](../type-aliases/InferResultType.md)\<`TContext`\>\>
88
89A promise that resolves to an array of query results
90
91### Example
92
93```typescript
94const recentOrders = await queryOnce({
95 query: (q) =>
96 q.from({ order: ordersCollection })
97 .orderBy(({ order }) => desc(order.createdAt))
98 .limit(100),
99})
100```