Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

useLiveQuery.md

451 lines9.7 KB

1---
2id: useLiveQuery
3title: useLiveQuery
4---
5​
6## Call Signature
7​
8```ts
9function useLiveQuery<TContext>(queryFn): Accessor<InferResultType<TContext>> & object;
10```
11​
12Defined in: [useLiveQuery.ts:103](https://github.com/TanStack/db/blob/main/packages/solid-db/src/useLiveQuery.ts#L103)
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### Returns
31​
32Accessor that returns data with Suspense support, with state and status information as properties
33​
34### Examples
35​
36```ts
37// Basic query with object syntax
38const todosQuery = useLiveQuery((q) =>
39 q.from({ todos: todosCollection })
40 .where(({ todos }) => eq(todos.completed, false))
41 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
43```
44​
45```ts
46// With dependencies that trigger re-execution
47const todosQuery = useLiveQuery(
48 (q) => q.from({ todos: todosCollection })
49 .where(({ todos }) => gt(todos.priority, minPriority())),
51```
52​
53```ts
54// Join pattern
55const personIssues = useLiveQuery((q) =>
56 q.from({ issues: issueCollection })
57 .join({ persons: personCollection }, ({ issues, persons }) =>
58 eq(issues.userId, persons.id)
59 )
60 .select(({ issues, persons }) => ({
61 id: issues.id,
62 title: issues.title,
63 userName: persons.name
64 }))
66```
67​
68```ts
69// Handle loading and error states
70const todosQuery = useLiveQuery((q) =>
71 q.from({ todos: todoCollection })
73​
74return (
75 <Switch>
76 <Match when={todosQuery.isLoading}>
77 <div>Loading...</div>
78 </Match>
79 <Match when={todosQuery.isError}>
80 <div>Error: {todosQuery.status}</div>
81 </Match>
82 <Match when={todosQuery.isReady}>
83 <For each={todosQuery()}>
84 {(todo) => <li key={todo.id}>{todo.text}</li>}
85 </For>
86 </Match>
87 </Switch>
89```
90​
91```ts
92// Use Suspense boundaries
93const todosQuery = useLiveQuery((q) =>
94 q.from({ todos: todoCollection })
96​
97return (
98 <Suspense fallback={<div>Loading...</div>}>
99 <For each={todosQuery()}>
100 {(todo) => <li key={todo.id}>{todo.text}</li>}
101 </For>
102 </Suspense>
104```
105​
106## Call Signature
107​
108```ts
109function useLiveQuery<TContext>(queryFn): Accessor<InferResultType<TContext>> & object;
110```
111​
112Defined in: [useLiveQuery.ts:122](https://github.com/TanStack/db/blob/main/packages/solid-db/src/useLiveQuery.ts#L122)
113​
114Create a live query using a query function
115​
116### Type Parameters
117​
118#### TContext
119​
120`TContext` *extends* `Context`
121​
122### Parameters
123​
124#### queryFn
125​
126(`q`) => `QueryBuilder`\<`TContext`\> \| `null` \| `undefined`
127​
128Query function that defines what data to fetch
129​
130### Returns
131​
132Accessor that returns data with Suspense support, with state and status information as properties
133​
134### Examples
135​
136```ts
137// Basic query with object syntax
138const todosQuery = useLiveQuery((q) =>
139 q.from({ todos: todosCollection })
140 .where(({ todos }) => eq(todos.completed, false))
141 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
143```
144​
145```ts
146// With dependencies that trigger re-execution
147const todosQuery = useLiveQuery(
148 (q) => q.from({ todos: todosCollection })
149 .where(({ todos }) => gt(todos.priority, minPriority())),
151```
152​
153```ts
154// Join pattern
155const personIssues = useLiveQuery((q) =>
156 q.from({ issues: issueCollection })
157 .join({ persons: personCollection }, ({ issues, persons }) =>
158 eq(issues.userId, persons.id)
159 )
160 .select(({ issues, persons }) => ({
161 id: issues.id,
162 title: issues.title,
163 userName: persons.name
164 }))
166```
167​
168```ts
169// Handle loading and error states
170const todosQuery = useLiveQuery((q) =>
171 q.from({ todos: todoCollection })
173​
174return (
175 <Switch>
176 <Match when={todosQuery.isLoading}>
177 <div>Loading...</div>
178 </Match>
179 <Match when={todosQuery.isError}>
180 <div>Error: {todosQuery.status}</div>
181 </Match>
182 <Match when={todosQuery.isReady}>
183 <For each={todosQuery()}>
184 {(todo) => <li key={todo.id}>{todo.text}</li>}
185 </For>
186 </Match>
187 </Switch>
189```
190​
191```ts
192// Use Suspense boundaries
193const todosQuery = useLiveQuery((q) =>
194 q.from({ todos: todoCollection })
196​
197return (
198 <Suspense fallback={<div>Loading...</div>}>
199 <For each={todosQuery()}>
200 {(todo) => <li key={todo.id}>{todo.text}</li>}
201 </For>
202 </Suspense>
204```
205​
206## Call Signature
207​
208```ts
209function useLiveQuery<TContext>(config): Accessor<InferResultType<TContext>> & object;
210```
211​
212Defined in: [useLiveQuery.ts:183](https://github.com/TanStack/db/blob/main/packages/solid-db/src/useLiveQuery.ts#L183)
213​
214Create a live query using configuration object
215​
216### Type Parameters
217​
218#### TContext
219​
220`TContext` *extends* `Context`
221​
222### Parameters
223​
224#### config
225​
226`Accessor`\<`LiveQueryCollectionConfig`\<`TContext`, `RootQueryResult`\<`TContext`\>\>\>
227​
228Configuration object with query and options
229​
230### Returns
231​
232Accessor that returns data with Suspense support, with state and status information as properties
233​
234### Examples
235​
236```ts
237// Basic config object usage
238const todosQuery = useLiveQuery(() => ({
239 query: (q) => q.from({ todos: todosCollection }),
240 gcTime: 60000
241}))
242```
243​
244```ts
245// With query builder and options
246const queryBuilder = new Query()
247 .from({ persons: collection })
248 .where(({ persons }) => gt(persons.age, 30))
249 .select(({ persons }) => ({ id: persons.id, name: persons.name }))
250​
251const personsQuery = useLiveQuery(() => ({ query: queryBuilder }))
252```
253​
254```ts
255// Handle all states uniformly
256const itemsQuery = useLiveQuery(() => ({
257 query: (q) => q.from({ items: itemCollection })
258}))
259​
260return (
261 <Switch fallback={<div>{itemsQuery().length} items loaded</div>}>
262 <Match when={itemsQuery.isLoading}>
263 <div>Loading...</div>
264 </Match>
265 <Match when={itemsQuery.isError}>
266 <div>Something went wrong</div>
267 </Match>
268 <Match when={!itemsQuery.isReady}>
269 <div>Preparing...</div>
270 </Match>
271 </Switch>
273```
274​
275## Call Signature
276​
277```ts
278function useLiveQuery<TResult, TKey, TUtils>(liveQueryCollection): Accessor<TResult[]> & object;
279```
280​
281Defined in: [useLiveQuery.ts:237](https://github.com/TanStack/db/blob/main/packages/solid-db/src/useLiveQuery.ts#L237)
282​
283Subscribe to an existing live query collection
284​
285### Type Parameters
286​
287#### TResult
288​
289`TResult` *extends* `object`
290​
291#### TKey
292​
293`TKey` *extends* `string` \| `number`
294​
295#### TUtils
296​
297`TUtils` *extends* `Record`\<`string`, `any`\>
298​
299### Parameters
300​
301#### liveQueryCollection
302​
303`Accessor`\<`Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `NonSingleResult`\>
304​
305Pre-created live query collection to subscribe to
306​
307### Returns
308​
309Accessor that returns data with Suspense support, with state and status information as properties
310​
311### Examples
312​
313```ts
314// Using pre-created live query collection
315const myLiveQuery = createLiveQueryCollection((q) =>
316 q.from({ todos: todosCollection }).where(({ todos }) => eq(todos.active, true))
318const todosQuery = useLiveQuery(() => myLiveQuery)
319```
320​
321```ts
322// Access collection methods directly
323const existingQuery = useLiveQuery(() => existingCollection)
324​
325// Use collection for mutations
326const handleToggle = (id) => {
327 existingQuery.collection.update(id, draft => { draft.completed = !draft.completed })
329```
330​
331```ts
332// Handle states consistently
333const sharedQuery = useLiveQuery(() => sharedCollection)
334​
335return (
336 <Switch fallback={<div><For each={sharedQuery()}>{(item) => <Item key={item.id} {...item} />}</For></div>}>
337 <Match when={sharedQuery.isLoading}>
338 <div>Loading...</div>
339 </Match>
340 <Match when={sharedQuery.isError}>
341 <div>Error loading data</div>
342 </Match>
343 </Switch>
345```
346​
347## Call Signature
348​
349```ts
350function useLiveQuery<TResult, TKey, TUtils>(liveQueryCollection): Accessor<TResult | undefined> & object;
351```
352​
353Defined in: [useLiveQuery.ts:262](https://github.com/TanStack/db/blob/main/packages/solid-db/src/useLiveQuery.ts#L262)
354​
355Create a live query using a query function
356​
357### Type Parameters
358​
359#### TResult
360​
361`TResult` *extends* `object`
362​
363#### TKey
364​
365`TKey` *extends* `string` \| `number`
366​
367#### TUtils
368​
369`TUtils` *extends* `Record`\<`string`, `any`\>
370​
371### Parameters
372​
373#### liveQueryCollection
374​
375`Accessor`\<`Collection`\<`TResult`, `TKey`, `TUtils`, `StandardSchemaV1`\<`unknown`, `unknown`\>, `TResult`\> & `SingleResult`\>
376​
377### Returns
378​
379Accessor that returns data with Suspense support, with state and status information as properties
380​
381### Examples
382​
383```ts
384// Basic query with object syntax
385const todosQuery = useLiveQuery((q) =>
386 q.from({ todos: todosCollection })
387 .where(({ todos }) => eq(todos.completed, false))
388 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
390```
391​
392```ts
393// With dependencies that trigger re-execution
394const todosQuery = useLiveQuery(
395 (q) => q.from({ todos: todosCollection })
396 .where(({ todos }) => gt(todos.priority, minPriority())),
398```
399​
400```ts
401// Join pattern
402const personIssues = useLiveQuery((q) =>
403 q.from({ issues: issueCollection })
404 .join({ persons: personCollection }, ({ issues, persons }) =>
405 eq(issues.userId, persons.id)
406 )
407 .select(({ issues, persons }) => ({
408 id: issues.id,
409 title: issues.title,
410 userName: persons.name
411 }))
413```
414​
415```ts
416// Handle loading and error states
417const todosQuery = useLiveQuery((q) =>
418 q.from({ todos: todoCollection })
420​
421return (
422 <Switch>
423 <Match when={todosQuery.isLoading}>
424 <div>Loading...</div>
425 </Match>
426 <Match when={todosQuery.isError}>
427 <div>Error: {todosQuery.status}</div>
428 </Match>
429 <Match when={todosQuery.isReady}>
430 <For each={todosQuery()}>
431 {(todo) => <li key={todo.id}>{todo.text}</li>}
432 </For>
433 </Match>
434 </Switch>
436```
437​
438```ts
439// Use Suspense boundaries
440const todosQuery = useLiveQuery((q) =>
441 q.from({ todos: todoCollection })
443​
444return (
445 <Suspense fallback={<div>Loading...</div>}>
446 <For each={todosQuery()}>
447 {(todo) => <li key={todo.id}>{todo.text}</li>}
448 </For>
449 </Suspense>
451```