Open History to see the last commit
1---
2title: TanStack DB Vue Adapter
3id: adapter
4---
5
6## Installation
7
8```sh
9npm install @tanstack/vue-db
10```
11
12## Vue Composables
13
14See the [Vue Functions Reference](./reference/index.md) to see the full list of composables available in the Vue 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` composable creates a live query that automatically updates your component when data changes. It returns reactive computed refs:
23
24```vue
25<script setup>
26import { useLiveQuery } from '@tanstack/vue-db'
27import { eq } from '@tanstack/db'
28
29const { data, isLoading } = 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<template>
37 <div v-if="isLoading">Loading...</div>
38 <ul v-else>
39 <li v-for="todo in data" :key="todo.id">{{ todo.text }}</li>
40 </ul>
41</template>
42```
43
44**Note:** All return values (`data`, `isLoading`, `status`, etc.) are computed refs, so access them with `.value` in `<script>` but directly in `<template>`.
45
46### Dependency Arrays
47
48The `useLiveQuery` composable accepts an optional dependency array as its last parameter. When any reactive value in the array changes, the query is recreated and re-executed.
49
50#### When to Use Dependency Arrays
51
52Use dependency arrays when your query depends on external reactive values (refs, props, or reactive objects):
53
54```vue
55<script setup>
56import { ref } from 'vue'
57import { useLiveQuery } from '@tanstack/vue-db'
58import { gt } from '@tanstack/db'
59
60const minPriority = ref(5)
61
62const { data } = useLiveQuery(
63 (q) => q.from({ todos: todosCollection })
64 .where(({ todos }) => gt(todos.priority, minPriority.value)),
65 [minPriority] // Pass the ref directly, it will be unwrapped automatically
66)
67</script>
68
69<template>
70 <div>{{ data.length }} high-priority todos</div>
71</template>
72```
73
74**Important:** Pass refs directly in the dependency array, not as functions. Vue will automatically track them.
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 composable shows loading state again
83
84#### Best Practices
85
86**Include all external refs used in the query:**
87
88```vue
89<script setup>
90import { ref } from 'vue'
91import { useLiveQuery } from '@tanstack/vue-db'
92import { eq, and } from '@tanstack/db'
93
94const userId = ref(1)
95const status = ref('active')
96
97// Good - all refs in deps array
98const { data } = useLiveQuery(
99 (q) => q.from({ todos: todosCollection })
100 .where(({ todos }) => and(
101 eq(todos.userId, userId.value),
102 eq(todos.status, status.value)
103 )),
104 [userId, status] // Pass refs directly
105)
106
107// Bad - missing dependencies
108const { data: badData } = useLiveQuery(
109 (q) => q.from({ todos: todosCollection })
110 .where(({ todos }) => eq(todos.userId, userId.value)),
111 [] // Missing userId!
112)
113</script>
114
115<template>
116 <div>{{ data.length }} todos</div>
117</template>
118```
119
120**Using with props:**
121
122```vue
123<script setup>
124import { toRef } from 'vue'
125import { useLiveQuery } from '@tanstack/vue-db'
126import { eq } from '@tanstack/db'
127
128const props = defineProps<{ userId: number }>()
129
130// Option 1: Convert prop to ref
131const userIdRef = toRef(props, 'userId')
132const { data } = useLiveQuery(
133 (q) => q.from({ todos: todosCollection })
134 .where(({ todos }) => eq(todos.userId, userIdRef.value)),
135 [userIdRef]
136)
137
138// Option 2: Use a getter function for the prop
139const { data: data2 } = useLiveQuery(
140 (q) => q.from({ todos: todosCollection })
141 .where(({ todos }) => eq(todos.userId, props.userId)),
142 [() => props.userId] // Getter function for non-ref values
143)
144</script>
145
146<template>
147 <div>{{ data.length }} todos</div>
148</template>
149```
150
151**Empty array for static queries:**
152
153```vue
154<script setup>
155import { useLiveQuery } from '@tanstack/vue-db'
156
157// No external dependencies - query never changes
158const { data } = useLiveQuery(
159 (q) => q.from({ todos: todosCollection }),
160 []
161)
162</script>
163
164<template>
165 <div>{{ data.length }} todos</div>
166</template>
167```
168
169**Omit the array for queries with no external dependencies:**
170
171```vue
172<script setup>
173import { useLiveQuery } from '@tanstack/vue-db'
174
175// Same as above - no deps needed
176const { data } = useLiveQuery(
177 (q) => q.from({ todos: todosCollection })
178)
179</script>
180
181<template>
182 <div>{{ data.length }} todos</div>
183</template>
184```
185
186### Using Pre-created Collections
187
188You can also pass an existing collection to `useLiveQuery`. This is useful for sharing queries across components:
189
190```vue
191<script setup>
192import { ref } from 'vue'
193import { createLiveQueryCollection } from '@tanstack/db'
194import { useLiveQuery } from '@tanstack/vue-db'
195import { eq } from '@tanstack/db'
196
197// Create collection outside component or in a composable
198const todosQuery = createLiveQueryCollection({
199 query: (q) => q.from({ todos: todosCollection })
200 .where(({ todos }) => eq(todos.active, true)),
201 startSync: true
202})
203
204// Use the pre-created collection
205const { data, collection } = useLiveQuery(todosQuery)
206
207// Or use a reactive ref to switch between collections
208const currentQuery = ref(todosQuery)
209const { data: reactiveData } = useLiveQuery(currentQuery)
210</script>
211
212<template>
213 <div>{{ data.length }} todos</div>
214</template>
215```