Open History to see the last commit
1---
2id: useLiveQuery
3title: useLiveQuery
4---
5
6## Call Signature
7
8```ts
9function useLiveQuery<TContext>(queryFn, deps?): UseLiveQueryReturn<TContext>;
10```
11
12Defined in: [useLiveQuery.ts:138](https://github.com/TanStack/db/blob/main/packages/vue-db/src/useLiveQuery.ts#L138)
13
14Create a live query using a query function
15
16### Type Parameters
17
18#### TContext
19
20`TContext` *extends* `Context`
21
22### Parameters
23
24#### queryFn
25
26(`q`) => `QueryBuilder`\<`TContext`\>
27
28Query function that defines what data to fetch
29
30#### deps?
31
32`unknown`[]
33
34Array of reactive dependencies that trigger query re-execution when changed
35
36### Returns
37
38[`UseLiveQueryReturn`](../interfaces/UseLiveQueryReturn.md)\<`TContext`\>
39
40Reactive object with query data, state, and status information
41
42### Examples
43
44```ts
45// Basic query with object syntax
46const { data, isLoading } = useLiveQuery((q) =>
47 q.from({ todos: todosCollection })
48 .where(({ todos }) => eq(todos.completed, false))
49 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
50)
51```
52
53```ts
54// With reactive dependencies
55const minPriority = ref(5)
56const { data, state } = useLiveQuery(
57 (q) => q.from({ todos: todosCollection })
58 .where(({ todos }) => gt(todos.priority, minPriority.value)),
59 [minPriority] // Re-run when minPriority changes
60)
61```
62
63```ts
64// Join pattern
65const { data } = useLiveQuery((q) =>
66 q.from({ issues: issueCollection })
67 .join({ persons: personCollection }, ({ issues, persons }) =>
68 eq(issues.userId, persons.id)
69 )
70 .select(({ issues, persons }) => ({
71 id: issues.id,
72 title: issues.title,
73 userName: persons.name
74 }))
75)
76```
77
78```ts
79// Handle loading and error states in template
80const { data, isLoading, isError, status } = useLiveQuery((q) =>
81 q.from({ todos: todoCollection })
82)
83
84// In template:
85// <div v-if="isLoading">Loading...</div>
86// <div v-else-if="isError">Error: {{ status }}</div>
87// <ul v-else>
88// <li v-for="todo in data" :key="todo.id">{{ todo.text }}</li>
89// </ul>
90```
91
92## Call Signature
93
94```ts
95function useLiveQuery<TContext>(queryFn, deps?): UseLiveQueryReturn<TContext>;
96```
97
98Defined in: [useLiveQuery.ts:144](https://github.com/TanStack/db/blob/main/packages/vue-db/src/useLiveQuery.ts#L144)
99
100Create a live query using a query function
101
102### Type Parameters
103
104#### TContext
105
106`TContext` *extends* `Context`
107
108### Parameters
109
110#### queryFn
111
112(`q`) => `QueryBuilder`\<`TContext`\> \| `null` \| `undefined`
113
114Query function that defines what data to fetch
115
116#### deps?
117
118`unknown`[]
119
120Array of reactive dependencies that trigger query re-execution when changed
121
122### Returns
123
124[`UseLiveQueryReturn`](../interfaces/UseLiveQueryReturn.md)\<`TContext`\>
125
126Reactive object with query data, state, and status information
127
128### Examples
129
130```ts
131// Basic query with object syntax
132const { data, isLoading } = useLiveQuery((q) =>
133 q.from({ todos: todosCollection })
134 .where(({ todos }) => eq(todos.completed, false))
135 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
136)
137```
138
139```ts
140// With reactive dependencies
141const minPriority = ref(5)
142const { data, state } = useLiveQuery(
143 (q) => q.from({ todos: todosCollection })
144 .where(({ todos }) => gt(todos.priority, minPriority.value)),
145 [minPriority] // Re-run when minPriority changes
146)
147```
148
149```ts
150// Join pattern
151const { data } = useLiveQuery((q) =>
152 q.from({ issues: issueCollection })
153 .join({ persons: personCollection }, ({ issues, persons }) =>
154 eq(issues.userId, persons.id)
155 )
156 .select(({ issues, persons }) => ({
157 id: issues.id,
158 title: issues.title,
159 userName: persons.name
160 }))
161)
162```
163
164```ts
165// Handle loading and error states in template
166const { data, isLoading, isError, status } = useLiveQuery((q) =>
167 q.from({ todos: todoCollection })
168)
169
170// In template:
171// <div v-if="isLoading">Loading...</div>
172// <div v-else-if="isError">Error: {{ status }}</div>
173// <ul v-else>
174// <li v-for="todo in data" :key="todo.id">{{ todo.text }}</li>
175// </ul>
176```
177
178## Call Signature
179
180```ts
181function useLiveQuery<TContext>(config, deps?): UseLiveQueryReturn<TContext>;
182```
183
184Defined in: [useLiveQuery.ts:184](https://github.com/TanStack/db/blob/main/packages/vue-db/src/useLiveQuery.ts#L184)
185
186Create a live query using configuration object
187
188### Type Parameters
189
190#### TContext
191
192`TContext` *extends* `Context`
193
194### Parameters
195
196#### config
197
198`LiveQueryCollectionConfig`\<`TContext`\>
199
200Configuration object with query and options
201
202#### deps?
203
204`unknown`[]
205
206Array of reactive dependencies that trigger query re-execution when changed
207
208### Returns
209
210[`UseLiveQueryReturn`](../interfaces/UseLiveQueryReturn.md)\<`TContext`\>
211
212Reactive object with query data, state, and status information
213
214### Examples
215
216```ts
217// Basic config object usage
218const { data, status } = useLiveQuery({
219 query: (q) => q.from({ todos: todosCollection }),
220 gcTime: 60000
221})
222```
223
224```ts
225// With reactive dependencies
226const filter = ref('active')
227const { data, isReady } = useLiveQuery({
228 query: (q) => q.from({ todos: todosCollection })
229 .where(({ todos }) => eq(todos.status, filter.value))
230}, [filter])
231```
232
233```ts
234// Handle all states uniformly
235const { data, isLoading, isReady, isError } = useLiveQuery({
236 query: (q) => q.from({ items: itemCollection })
237})
238
239// In template:
240// <div v-if="isLoading">Loading...</div>
241// <div v-else-if="isError">Something went wrong</div>
242// <div v-else-if="!isReady">Preparing...</div>
243// <div v-else>{{ data.length }} items loaded</div>
244```
245
246## Call Signature
247
248```ts
249function useLiveQuery<TResult, TKey, TUtils>(liveQueryCollection): UseLiveQueryReturnWithCollection<TResult, TKey, TUtils>;
250```
251
252Defined in: [useLiveQuery.ts:229](https://github.com/TanStack/db/blob/main/packages/vue-db/src/useLiveQuery.ts#L229)
253
254Subscribe to an existing query collection (can be reactive)
255
256### Type Parameters
257
258#### TResult
259
260`TResult` *extends* `object`
261
262#### TKey
263
264`TKey` *extends* `string` \| `number`
265
266#### TUtils
267
268`TUtils` *extends* `Record`\<`string`, `any`\>
269
270### Parameters
271
272#### liveQueryCollection
273
274`MaybeRefOrGetter`\<`Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `NonSingleResult`\>
275
276Pre-created query collection to subscribe to (can be a ref)
277
278### Returns
279
280[`UseLiveQueryReturnWithCollection`](../interfaces/UseLiveQueryReturnWithCollection.md)\<`TResult`, `TKey`, `TUtils`\>
281
282Reactive object with query data, state, and status information
283
284### Examples
285
286```ts
287// Using pre-created query collection
288const myLiveQuery = createLiveQueryCollection((q) =>
289 q.from({ todos: todosCollection }).where(({ todos }) => eq(todos.active, true))
290)
291const { data, collection } = useLiveQuery(myLiveQuery)
292```
293
294```ts
295// Reactive query collection reference
296const selectedQuery = ref(todosQuery)
297const { data, collection } = useLiveQuery(selectedQuery)
298
299// Switch queries reactively
300selectedQuery.value = archiveQuery
301```
302
303```ts
304// Access query collection methods directly
305const { data, collection, isReady } = useLiveQuery(existingQuery)
306
307// Use underlying collection for mutations
308const handleToggle = (id) => {
309 collection.value.update(id, draft => { draft.completed = !draft.completed })
310}
311```
312
313```ts
314// Handle states consistently
315const { data, isLoading, isError } = useLiveQuery(sharedQuery)
316
317// In template:
318// <div v-if="isLoading">Loading...</div>
319// <div v-else-if="isError">Error loading data</div>
320// <div v-else>
321// <Item v-for="item in data" :key="item.id" v-bind="item" />
322// </div>
323```
324
325## Call Signature
326
327```ts
328function useLiveQuery<TResult, TKey, TUtils>(liveQueryCollection): UseLiveQueryReturnWithSingleResultCollection<TResult, TKey, TUtils>;
329```
330
331Defined in: [useLiveQuery.ts:240](https://github.com/TanStack/db/blob/main/packages/vue-db/src/useLiveQuery.ts#L240)
332
333Create a live query using a query function
334
335### Type Parameters
336
337#### TResult
338
339`TResult` *extends* `object`
340
341#### TKey
342
343`TKey` *extends* `string` \| `number`
344
345#### TUtils
346
347`TUtils` *extends* `Record`\<`string`, `any`\>
348
349### Parameters
350
351#### liveQueryCollection
352
353`MaybeRefOrGetter`\<`Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `SingleResult`\>
354
355### Returns
356
357[`UseLiveQueryReturnWithSingleResultCollection`](../interfaces/UseLiveQueryReturnWithSingleResultCollection.md)\<`TResult`, `TKey`, `TUtils`\>
358
359Reactive object with query data, state, and status information
360
361### Examples
362
363```ts
364// Basic query with object syntax
365const { data, isLoading } = useLiveQuery((q) =>
366 q.from({ todos: todosCollection })
367 .where(({ todos }) => eq(todos.completed, false))
368 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
369)
370```
371
372```ts
373// With reactive dependencies
374const minPriority = ref(5)
375const { data, state } = useLiveQuery(
376 (q) => q.from({ todos: todosCollection })
377 .where(({ todos }) => gt(todos.priority, minPriority.value)),
378 [minPriority] // Re-run when minPriority changes
379)
380```
381
382```ts
383// Join pattern
384const { data } = useLiveQuery((q) =>
385 q.from({ issues: issueCollection })
386 .join({ persons: personCollection }, ({ issues, persons }) =>
387 eq(issues.userId, persons.id)
388 )
389 .select(({ issues, persons }) => ({
390 id: issues.id,
391 title: issues.title,
392 userName: persons.name
393 }))
394)
395```
396
397```ts
398// Handle loading and error states in template
399const { data, isLoading, isError, status } = useLiveQuery((q) =>
400 q.from({ todos: todoCollection })
401)
402
403// In template:
404// <div v-if="isLoading">Loading...</div>
405// <div v-else-if="isError">Error: {{ status }}</div>
406// <ul v-else>
407// <li v-for="todo in data" :key="todo.id">{{ todo.text }}</li>
408// </ul>
409```