| name | svelte-db |
| description | Svelte 5 bindings for TanStack DB. useLiveQuery with Svelte 5 runes ($state, $derived, $effect). Dependency arrays use getter functions (() => value) for reactive props. Direct destructuring breaks reactivity — use dot notation or wrap with $derived. Conditional queries via returning undefined/null. Import from @tanstack/svelte-db (re-exports all of @tanstack/db).
|
| type | framework |
| library | db |
| framework | svelte |
| library_version | 0.6.17 |
| requires | ["db-core"] |
| sources | ["TanStack/db:docs/framework/svelte/overview.md","TanStack/db:packages/svelte-db/src/useLiveQuery.svelte.ts"] |
This skill builds on db-core. Read it first for collection setup, query builder, and mutation patterns.
TanStack DB — Svelte 5
Setup
<script lang="ts">
import { useLiveQuery, eq, not } from "@tanstack/svelte-db"
const todosQuery = useLiveQuery((q) =>
q
.from({ todo: todoCollection })
.where(({ todo }) => not(todo.completed))
.orderBy(({ todo }) => todo.created_at, "asc")
)
</script>
{#if todosQuery.isLoading}
<div>Loading...</div>
{:else}
<ul>
{#each todosQuery.data as todo (todo.id)}
<li>{todo.text}</li>
{/each}
</ul>
{/if}
@tanstack/svelte-db re-exports everything from @tanstack/db.
Hook
useLiveQuery
Returns a reactive object with getter properties:
const query = useLiveQuery((q) => q.from({ todo: todoCollection }))
let minPriority = $state(5)
const query = useLiveQuery(
(q) =>
q
.from({ todo: todoCollection })
.where(({ todo }) => gt(todo.priority, minPriority)),
[() => minPriority],
)
const query = useLiveQuery({
query: (q) => q.from({ todo: todoCollection }),
gcTime: 60000,
})
query = (preloadedCollection)