Open History to see the last commit
1---
2title: TanStack DB Svelte Adapter
3id: adapter
4---
5
6## Installation
7
8```sh
9npm install @tanstack/svelte-db
10```
11
12## Svelte Utilities
13
14See the [Svelte Functions Reference](./reference/index.md) to see the full list of utilities available in the Svelte Adapter.
15
16For comprehensive documentation on writing queries (filtering, joins, aggregations, etc.), see the [Live Queries Guide](../../guides/live-queries).
17
18## Basic Usage
19
20### useLiveQuery
21
22The `useLiveQuery` utility creates a live query that automatically updates your component when data changes. It returns reactive values powered by Svelte 5 runes:
23
24```svelte
25<script>
26 import { useLiveQuery } from '@tanstack/svelte-db'
27 import { eq } from '@tanstack/db'
28
29 const query = useLiveQuery((q) =>
30 q.from({ todos: todosCollection })
31 .where(({ todos }) => eq(todos.completed, false))
32 .select(({ todos }) => ({ id: todos.id, text: todos.text }))
33 )
34</script>
35
36{#if query.isLoading}
37 <div>Loading...</div>
38{:else}
39 <ul>
40 {#each query.data as todo (todo.id)}
41 <li>{todo.text}</li>
42 {/each}
43 </ul>
44{/if}
45```
46
47**Note:** With Svelte 5, `useLiveQuery` returns reactive values through getters. Access `query.data` and `query.isLoading` directly (no `$` prefix needed).
48
49### Dependency Arrays
50
51The `useLiveQuery` utility accepts an optional dependency array as its last parameter. When any value in the array changes, the query is recreated and re-executed.
52
53#### When to Use Dependency Arrays
54
55Use dependency arrays when your query depends on external reactive values (props or state):
56
57```svelte
58<script>
59 import { useLiveQuery } from '@tanstack/svelte-db'
60 import { gt } from '@tanstack/db'
61
62 let { minPriority } = $props()
63
64 const query = useLiveQuery(
65 (q) => q.from({ todos: todosCollection })
66 .where(({ todos }) => gt(todos.priority, minPriority)),
67 [() => minPriority] // Re-run when minPriority changes
68 )
69</script>
70
71<div>{query.data.length} high-priority todos</div>
72```
73
74**Note:** When using props or reactive state in the query, wrap them in a function for the dependency array.
75
76#### What Happens When Dependencies Change
77
78When a dependency value changes:
791. The previous live query collection is cleaned up
802. A new query is created with the updated values
813. The component re-renders with the new data
824. The utility shows loading state again
83
84#### Best Practices
85
86**Include all external values used in the query:**
87
88```svelte
89<script>
90 import { useLiveQuery } from '@tanstack/svelte-db'
91 import { eq, and } from '@tanstack/db'
92
93 let userId = $state(1)
94 let status = $state('active')
95
96 // Good - all external values in deps
97 const query = useLiveQuery(
98 (q) => q.from({ todos: todosCollection })
99 .where(({ todos }) => and(
100 eq(todos.userId, userId),
101 eq(todos.status, status)
102 )),
103 [() => userId, () => status]
104 )
105
106 // Bad - missing dependencies
107 const badQuery = useLiveQuery(
108 (q) => q.from({ todos: todosCollection })
109 .where(({ todos }) => eq(todos.userId, userId)),
110 [] // Missing userId!
111 )
112</script>
113
114<div>{query.data.length} todos</div>
115```
116
117**Empty array for static queries:**
118
119```svelte
120<script>
121 import { useLiveQuery } from '@tanstack/svelte-db'
122
123 // No external dependencies - query never changes
124 const query = useLiveQuery(
125 (q) => q.from({ todos: todosCollection }),
126 []
127 )
128</script>
129
130<div>{query.data.length} todos</div>
131```
132
133**Omit the array for queries with no external dependencies:**
134
135```svelte
136<script>
137 import { useLiveQuery } from '@tanstack/svelte-db'
138
139 // Same as above - no deps needed
140 const query = useLiveQuery(
141 (q) => q.from({ todos: todosCollection })
142 )
143</script>
144
145<div>{query.data.length} todos</div>
146```
147
148### Accessing Multiple Properties
149
150You can access all status properties directly on the query result:
151
152```svelte
153<script>
154 import { useLiveQuery } from '@tanstack/svelte-db'
155 import { eq } from '@tanstack/db'
156
157 const query = useLiveQuery((q) =>
158 q.from({ todos: todosCollection })
159 .where(({ todos }) => eq(todos.active, true))
160 )
161</script>
162
163<div>
164 <div>Status: {query.status}</div>
165 <div>Loading: {query.isLoading}</div>
166 <div>Ready: {query.isReady}</div>
167 <div>Total: {query.data.length}</div>
168</div>
169```