@tanstack/svelte-db 0.1.73 → 0.1.74
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -3
- package/skills/svelte-db/SKILL.md +171 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/svelte-db",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.74",
|
|
4
4
|
"description": "Svelte integration for @tanstack/db",
|
|
5
5
|
"author": "Kyle Mathews",
|
|
6
6
|
"license": "MIT",
|
|
@@ -30,10 +30,11 @@
|
|
|
30
30
|
"files": [
|
|
31
31
|
"dist",
|
|
32
32
|
"!dist/**/*.test.*",
|
|
33
|
-
"!dist/**/*.spec.*"
|
|
33
|
+
"!dist/**/*.spec.*",
|
|
34
|
+
"skills"
|
|
34
35
|
],
|
|
35
36
|
"dependencies": {
|
|
36
|
-
"@tanstack/db": "0.5.
|
|
37
|
+
"@tanstack/db": "0.5.31"
|
|
37
38
|
},
|
|
38
39
|
"peerDependencies": {
|
|
39
40
|
"svelte": "^5.0.0"
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: svelte-db
|
|
3
|
+
description: >
|
|
4
|
+
Svelte 5 bindings for TanStack DB. useLiveQuery with Svelte 5 runes
|
|
5
|
+
($state, $derived, $effect). Dependency arrays use getter functions
|
|
6
|
+
(() => value) for reactive props. Direct destructuring breaks reactivity —
|
|
7
|
+
use dot notation or wrap with $derived. Conditional queries via returning
|
|
8
|
+
undefined/null. Import from @tanstack/svelte-db (re-exports all of
|
|
9
|
+
@tanstack/db).
|
|
10
|
+
type: framework
|
|
11
|
+
library: db
|
|
12
|
+
framework: svelte
|
|
13
|
+
library_version: '0.5.30'
|
|
14
|
+
requires:
|
|
15
|
+
- db-core
|
|
16
|
+
sources:
|
|
17
|
+
- 'TanStack/db:docs/framework/svelte/overview.md'
|
|
18
|
+
- 'TanStack/db:packages/svelte-db/src/useLiveQuery.svelte.ts'
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
This skill builds on db-core. Read it first for collection setup, query builder, and mutation patterns.
|
|
22
|
+
|
|
23
|
+
# TanStack DB — Svelte 5
|
|
24
|
+
|
|
25
|
+
## Setup
|
|
26
|
+
|
|
27
|
+
```svelte
|
|
28
|
+
<script lang="ts">
|
|
29
|
+
import { useLiveQuery, eq, not } from "@tanstack/svelte-db"
|
|
30
|
+
|
|
31
|
+
const todosQuery = useLiveQuery((q) =>
|
|
32
|
+
q
|
|
33
|
+
.from({ todo: todoCollection })
|
|
34
|
+
.where(({ todo }) => not(todo.completed))
|
|
35
|
+
.orderBy(({ todo }) => todo.created_at, "asc")
|
|
36
|
+
)
|
|
37
|
+
</script>
|
|
38
|
+
|
|
39
|
+
{#if todosQuery.isLoading}
|
|
40
|
+
<div>Loading...</div>
|
|
41
|
+
{:else}
|
|
42
|
+
<ul>
|
|
43
|
+
{#each todosQuery.data as todo (todo.id)}
|
|
44
|
+
<li>{todo.text}</li>
|
|
45
|
+
{/each}
|
|
46
|
+
</ul>
|
|
47
|
+
{/if}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`@tanstack/svelte-db` re-exports everything from `@tanstack/db`.
|
|
51
|
+
|
|
52
|
+
## Hook
|
|
53
|
+
|
|
54
|
+
### useLiveQuery
|
|
55
|
+
|
|
56
|
+
Returns a reactive object with getter properties:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
// Query function — access via dot notation
|
|
60
|
+
const query = useLiveQuery((q) => q.from({ todo: todoCollection }))
|
|
61
|
+
// query.data, query.isLoading, query.status, etc.
|
|
62
|
+
|
|
63
|
+
// With reactive deps — use GETTER FUNCTIONS
|
|
64
|
+
let minPriority = $state(5)
|
|
65
|
+
const query = useLiveQuery(
|
|
66
|
+
(q) =>
|
|
67
|
+
q
|
|
68
|
+
.from({ todo: todoCollection })
|
|
69
|
+
.where(({ todo }) => gt(todo.priority, minPriority)),
|
|
70
|
+
[() => minPriority],
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
// Config object
|
|
74
|
+
const query = useLiveQuery({
|
|
75
|
+
query: (q) => q.from({ todo: todoCollection }),
|
|
76
|
+
gcTime: 60000,
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
// Pre-created collection
|
|
80
|
+
const query = useLiveQuery(preloadedCollection)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Svelte-Specific Patterns
|
|
84
|
+
|
|
85
|
+
### Destructuring with $derived
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
// WRONG — breaks reactivity
|
|
89
|
+
const { data, isLoading } = useLiveQuery(...)
|
|
90
|
+
|
|
91
|
+
// CORRECT — use dot notation
|
|
92
|
+
const query = useLiveQuery(...)
|
|
93
|
+
// Access: query.data, query.isLoading
|
|
94
|
+
|
|
95
|
+
// CORRECT — wrap with $derived if you need destructuring
|
|
96
|
+
const query = useLiveQuery(...)
|
|
97
|
+
const { data, isLoading } = $derived(query)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Props in dependency arrays
|
|
101
|
+
|
|
102
|
+
```svelte
|
|
103
|
+
<script lang="ts">
|
|
104
|
+
let { userId }: { userId: number } = $props()
|
|
105
|
+
|
|
106
|
+
const todosQuery = useLiveQuery(
|
|
107
|
+
(q) => q.from({ todo: todoCollection })
|
|
108
|
+
.where(({ todo }) => eq(todo.userId, userId)),
|
|
109
|
+
[() => userId]
|
|
110
|
+
)
|
|
111
|
+
</script>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Common Mistakes
|
|
115
|
+
|
|
116
|
+
### CRITICAL Passing values instead of getter functions in deps
|
|
117
|
+
|
|
118
|
+
Wrong:
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
let filter = $state('active')
|
|
122
|
+
const query = useLiveQuery(
|
|
123
|
+
(q) =>
|
|
124
|
+
q
|
|
125
|
+
.from({ todo: todoCollection })
|
|
126
|
+
.where(({ todo }) => eq(todo.status, filter)),
|
|
127
|
+
[filter],
|
|
128
|
+
)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Correct:
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
let filter = $state('active')
|
|
135
|
+
const query = useLiveQuery(
|
|
136
|
+
(q) =>
|
|
137
|
+
q
|
|
138
|
+
.from({ todo: todoCollection })
|
|
139
|
+
.where(({ todo }) => eq(todo.status, filter)),
|
|
140
|
+
[() => filter],
|
|
141
|
+
)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
In Svelte 5, deps must be getter functions. Passing values directly captures them at creation time — changes won't trigger re-execution.
|
|
145
|
+
|
|
146
|
+
Source: docs/framework/svelte/overview.md
|
|
147
|
+
|
|
148
|
+
### HIGH Direct destructuring breaks reactivity
|
|
149
|
+
|
|
150
|
+
Wrong:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
const { data, isLoading } = useLiveQuery((q) =>
|
|
154
|
+
q.from({ todo: todoCollection }),
|
|
155
|
+
)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Correct:
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
const query = useLiveQuery((q) => q.from({ todo: todoCollection }))
|
|
162
|
+
// Use query.data, query.isLoading
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Direct destructuring captures values at creation time. Use dot notation or wrap with `$derived`.
|
|
166
|
+
|
|
167
|
+
Source: packages/svelte-db/src/useLiveQuery.svelte.ts
|
|
168
|
+
|
|
169
|
+
See also: db-core/live-queries/SKILL.md — for query builder API.
|
|
170
|
+
|
|
171
|
+
See also: db-core/mutations-optimistic/SKILL.md — for mutation patterns.
|