Skip to content

Commit b1e0bf7

Browse files
feat: add Svelte support (#91)
Co-authored-by: Kyle Mathews <[email protected]>
1 parent 3e9a36d commit b1e0bf7

File tree

20 files changed

+3671
-1695
lines changed

20 files changed

+3671
-1695
lines changed

.changeset/khaki-ties-shout.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
"@tanstack/svelte-db": patch
3+
---
4+
5+
Add Svelte support
6+
7+
Usage example:
8+
9+
```svelte
10+
<script lang="ts">
11+
import { useLiveQuery } from "@tanstack/svelte-db"
12+
import { eq } from "@tanstack/db"
13+
import { todoCollection } from "$lib/collections"
14+
15+
const todosQuery = useLiveQuery((query) =>
16+
query
17+
.from({ todos: todoCollection })
18+
.where(({ todos }) => eq(todos.completed, false))
19+
)
20+
</script>
21+
22+
23+
<List items={todosQuery.data} />
24+
```

examples/react/todo/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,15 @@
9393
### Patch Changes
9494

9595
- Move Collections to their own packages ([#252](https://github.com/TanStack/db/pull/252))
96+
9697
- Move local-only and local-storage collections to main `@tanstack/db` package
9798
- Create new `@tanstack/electric-db-collection` package for Electric SQL integration
9899
- Create new `@tanstack/query-db-collection` package for TanStack Query integration
99100
- Delete `@tanstack/db-collections` package (removed from repo)
100101
- Update example app and documentation to use new package structure
101102

102103
Why?
104+
103105
- Better separation of concerns
104106
- Independent versioning for each collection type
105107
- Cleaner dependencies (electric collections don't need query deps, etc.)
@@ -251,6 +253,7 @@
251253
When `collection.insert()`, `.update()`, or `.delete()` are called outside of an explicit transaction (i.e., not within `useOptimisticMutation`), the library now automatically creates a single-operation transaction and invokes the corresponding handler to persist the change.
252254

253255
Key changes:
256+
254257
- **`@tanstack/db`**: The `Collection` class now supports `onInsert`, `onUpdate`, and `onDelete` in its configuration. Direct calls to mutation methods will throw an error if the corresponding handler is not defined.
255258
- **`@tanstack/db-collections`**:
256259
- `queryCollectionOptions` now accepts the new handlers and will automatically `refetch` the collection's query after a handler successfully completes. This behavior can be disabled if the handler returns `{ refetch: false }`.
@@ -262,6 +265,7 @@
262265
***
263266

264267
The documentation and the React Todo example application have been significantly refactored to adopt the new direct persistence handler pattern as the primary way to perform mutations.
268+
265269
- The `README.md` and `docs/overview.md` files have been updated to de-emphasize `useOptimisticMutation` for simple writes. They now showcase the much simpler API of calling `collection.insert()` directly and defining persistence logic in the collection's configuration.
266270
- The React Todo example (`examples/react/todo/src/App.tsx`) has been completely overhauled. All instances of `useOptimisticMutation` have been removed and replaced with the new `onInsert`, `onUpdate`, and `onDelete` handlers, resulting in cleaner and more concise code.
267271

examples/react/todo/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## How to run
44

55
- Go to the root of the repository and run:
6+
67
- `pnpm install`
78
- `pnpm build`
89

packages/db/CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
This comprehensive update replaces all string-based error throws throughout the TanStack DB codebase with named error classes, providing better type safety and developer experience.
3030

3131
## New Features
32+
3233
- **Root `TanStackDBError` class** - all errors inherit from a common base for unified error handling
3334
- **Named error classes** organized by package and functional area
3435
- **Type-safe error handling** using `instanceof` checks instead of string matching
@@ -40,6 +41,7 @@
4041
### Core Package (`@tanstack/db`)
4142

4243
Contains generic errors used across the ecosystem:
44+
4345
- Collection configuration, state, and operation errors
4446
- Transaction lifecycle and mutation errors
4547
- Query building, compilation, and execution errors
@@ -48,11 +50,13 @@
4850
### Adapter Packages
4951

5052
Each adapter now exports its own specific error classes:
53+
5154
- **`@tanstack/electric-db-collection`**: Electric-specific errors
5255
- **`@tanstack/trailbase-db-collection`**: TrailBase-specific errors
5356
- **`@tanstack/query-db-collection`**: Query collection specific errors
5457

5558
## Breaking Changes
59+
5660
- Error handling code using string matching will need to be updated to use `instanceof` checks
5761
- Some error messages may have slight formatting changes
5862
- Adapter-specific errors now need to be imported from their respective packages
@@ -121,6 +125,7 @@
121125
```
122126

123127
## Benefits
128+
124129
- **Type Safety**: All errors now have specific types that can be caught with `instanceof`
125130
- **Unified Error Handling**: Root `TanStackDBError` class allows catching all library errors with a single check
126131
- **Better Package Separation**: Each adapter manages its own error types
@@ -139,6 +144,7 @@
139144
- Add comprehensive documentation for creating collection options creators ([#284](https://github.com/TanStack/db/pull/284))
140145

141146
This adds a new documentation page `collection-options-creator.md` that provides detailed guidance for developers building collection options creators. The documentation covers:
147+
142148
- Core requirements and configuration interfaces
143149
- Sync implementation patterns with transaction lifecycle (begin, write, commit, markReady)
144150
- Data parsing and type conversion using field-specific conversions
@@ -171,6 +177,7 @@
171177
- Fix iterator-based change tracking in proxy system ([#271](https://github.com/TanStack/db/pull/271))
172178

173179
This fixes several issues with iterator-based change tracking for Maps and Sets:
180+
174181
- **Map.entries()** now correctly updates actual Map entries instead of creating duplicate keys
175182
- **Map.values()** now tracks back to original Map keys using value-to-key mapping instead of using symbol placeholders
176183
- **Set iterators** now properly replace objects in Set when modified instead of creating symbol-keyed entries
@@ -181,6 +188,7 @@
181188
This brings the proxy system in line with how mature libraries like Immer handle iterator-based change tracking, using method interception rather than trying to proxy all property access.
182189

183190
- Add explicit collection readiness detection with `isReady()` and `markReady()` ([#270](https://github.com/TanStack/db/pull/270))
191+
184192
- Add `isReady()` method to check if a collection is ready for use
185193
- Add `onFirstReady()` method to register callbacks for when collection becomes ready
186194
- Add `markReady()` to SyncConfig interface for sync implementations to explicitly signal readiness
@@ -222,13 +230,15 @@
222230
### Patch Changes
223231

224232
- Move Collections to their own packages ([#252](https://github.com/TanStack/db/pull/252))
233+
225234
- Move local-only and local-storage collections to main `@tanstack/db` package
226235
- Create new `@tanstack/electric-db-collection` package for Electric SQL integration
227236
- Create new `@tanstack/query-db-collection` package for TanStack Query integration
228237
- Delete `@tanstack/db-collections` package (removed from repo)
229238
- Update example app and documentation to use new package structure
230239

231240
Why?
241+
232242
- Better separation of concerns
233243
- Independent versioning for each collection type
234244
- Cleaner dependencies (electric collections don't need query deps, etc.)
@@ -292,6 +302,7 @@
292302
Adds automatic lifecycle management for collections to optimize resource usage.
293303

294304
**New Features:**
305+
295306
- Added `startSync` option (defaults to `false`, set to `true` to start syncing immediately)
296307
- Automatic garbage collection after `gcTime` (default 5 minutes) of inactivity
297308
- Collection status tracking: "idle" | "loading" | "ready" | "error" | "cleaned-up"
@@ -399,6 +410,7 @@
399410
- refactor the live query comparator and fix an issue with sorting with a null/undefined value in a column of non-null values ([#167](https://github.com/TanStack/db/pull/167))
400411

401412
- A large refactor of the core `Collection` with: ([#155](https://github.com/TanStack/db/pull/155))
413+
402414
- a change to not use Store internally and emit fine grade changes with `subscribeChanges` and `subscribeKeyChanges` methods.
403415
- changes to the `Collection` api to be more `Map` like for reads, with `get`, `has`, `size`, `entries`, `keys`, and `values`.
404416
- renames `config.getId` to `config.getKey` for consistency with the `Map` like api.
@@ -416,6 +428,7 @@
416428
- Expose utilities on collection instances ([#161](https://github.com/TanStack/db/pull/161))
417429

418430
Implemented a utility exposure pattern for TanStack DB collections that allows utility functions to be passed as part of collection options and exposes them under a `.utils` namespace, with full TypeScript typing.
431+
419432
- Refactored `createCollection` in packages/db/src/collection.ts to accept options with utilities directly
420433
- Added `utils` property to CollectionImpl
421434
- Added TypeScript types for utility functions and utility records
@@ -439,6 +452,7 @@
439452
When `collection.insert()`, `.update()`, or `.delete()` are called outside of an explicit transaction (i.e., not within `useOptimisticMutation`), the library now automatically creates a single-operation transaction and invokes the corresponding handler to persist the change.
440453

441454
Key changes:
455+
442456
- **`@tanstack/db`**: The `Collection` class now supports `onInsert`, `onUpdate`, and `onDelete` in its configuration. Direct calls to mutation methods will throw an error if the corresponding handler is not defined.
443457
- **`@tanstack/db-collections`**:
444458
- `queryCollectionOptions` now accepts the new handlers and will automatically `refetch` the collection's query after a handler successfully completes. This behavior can be disabled if the handler returns `{ refetch: false }`.
@@ -450,6 +464,7 @@
450464
***
451465

452466
The documentation and the React Todo example application have been significantly refactored to adopt the new direct persistence handler pattern as the primary way to perform mutations.
467+
453468
- The `README.md` and `docs/overview.md` files have been updated to de-emphasize `useOptimisticMutation` for simple writes. They now showcase the much simpler API of calling `collection.insert()` directly and defining persistence logic in the collection's configuration.
454469
- The React Todo example (`examples/react/todo/src/App.tsx`) has been completely overhauled. All instances of `useOptimisticMutation` have been removed and replaced with the new `onInsert`, `onUpdate`, and `onDelete` handlers, resulting in cleaner and more concise code.
455470

packages/electric-db-collection/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
This comprehensive update replaces all string-based error throws throughout the TanStack DB codebase with named error classes, providing better type safety and developer experience.
1717

1818
## New Features
19+
1920
- **Root `TanStackDBError` class** - all errors inherit from a common base for unified error handling
2021
- **Named error classes** organized by package and functional area
2122
- **Type-safe error handling** using `instanceof` checks instead of string matching
@@ -27,6 +28,7 @@
2728
### Core Package (`@tanstack/db`)
2829

2930
Contains generic errors used across the ecosystem:
31+
3032
- Collection configuration, state, and operation errors
3133
- Transaction lifecycle and mutation errors
3234
- Query building, compilation, and execution errors
@@ -35,11 +37,13 @@
3537
### Adapter Packages
3638

3739
Each adapter now exports its own specific error classes:
40+
3841
- **`@tanstack/electric-db-collection`**: Electric-specific errors
3942
- **`@tanstack/trailbase-db-collection`**: TrailBase-specific errors
4043
- **`@tanstack/query-db-collection`**: Query collection specific errors
4144

4245
## Breaking Changes
46+
4347
- Error handling code using string matching will need to be updated to use `instanceof` checks
4448
- Some error messages may have slight formatting changes
4549
- Adapter-specific errors now need to be imported from their respective packages
@@ -108,6 +112,7 @@
108112
```
109113

110114
## Benefits
115+
111116
- **Type Safety**: All errors now have specific types that can be caught with `instanceof`
112117
- **Unified Error Handling**: Root `TanStackDBError` class allows catching all library errors with a single check
113118
- **Better Package Separation**: Each adapter manages its own error types
@@ -148,6 +153,7 @@
148153
### Patch Changes
149154

150155
- Add explicit collection readiness detection with `isReady()` and `markReady()` ([#270](https://github.com/TanStack/db/pull/270))
156+
151157
- Add `isReady()` method to check if a collection is ready for use
152158
- Add `onFirstReady()` method to register callbacks for when collection becomes ready
153159
- Add `markReady()` to SyncConfig interface for sync implementations to explicitly signal readiness
@@ -187,13 +193,15 @@
187193
### Patch Changes
188194

189195
- Move Collections to their own packages ([#252](https://github.com/TanStack/db/pull/252))
196+
190197
- Move local-only and local-storage collections to main `@tanstack/db` package
191198
- Create new `@tanstack/electric-db-collection` package for Electric SQL integration
192199
- Create new `@tanstack/query-db-collection` package for TanStack Query integration
193200
- Delete `@tanstack/db-collections` package (removed from repo)
194201
- Update example app and documentation to use new package structure
195202

196203
Why?
204+
197205
- Better separation of concerns
198206
- Independent versioning for each collection type
199207
- Cleaner dependencies (electric collections don't need query deps, etc.)

packages/query-db-collection/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
This comprehensive update replaces all string-based error throws throughout the TanStack DB codebase with named error classes, providing better type safety and developer experience.
1717

1818
## New Features
19+
1920
- **Root `TanStackDBError` class** - all errors inherit from a common base for unified error handling
2021
- **Named error classes** organized by package and functional area
2122
- **Type-safe error handling** using `instanceof` checks instead of string matching
@@ -27,6 +28,7 @@
2728
### Core Package (`@tanstack/db`)
2829

2930
Contains generic errors used across the ecosystem:
31+
3032
- Collection configuration, state, and operation errors
3133
- Transaction lifecycle and mutation errors
3234
- Query building, compilation, and execution errors
@@ -35,11 +37,13 @@
3537
### Adapter Packages
3638

3739
Each adapter now exports its own specific error classes:
40+
3841
- **`@tanstack/electric-db-collection`**: Electric-specific errors
3942
- **`@tanstack/trailbase-db-collection`**: TrailBase-specific errors
4043
- **`@tanstack/query-db-collection`**: Query collection specific errors
4144

4245
## Breaking Changes
46+
4347
- Error handling code using string matching will need to be updated to use `instanceof` checks
4448
- Some error messages may have slight formatting changes
4549
- Adapter-specific errors now need to be imported from their respective packages
@@ -108,6 +112,7 @@
108112
```
109113

110114
## Benefits
115+
111116
- **Type Safety**: All errors now have specific types that can be caught with `instanceof`
112117
- **Unified Error Handling**: Root `TanStackDBError` class allows catching all library errors with a single check
113118
- **Better Package Separation**: Each adapter manages its own error types
@@ -148,6 +153,7 @@
148153
### Patch Changes
149154

150155
- Add explicit collection readiness detection with `isReady()` and `markReady()` ([#270](https://github.com/TanStack/db/pull/270))
156+
151157
- Add `isReady()` method to check if a collection is ready for use
152158
- Add `onFirstReady()` method to register callbacks for when collection becomes ready
153159
- Add `markReady()` to SyncConfig interface for sync implementations to explicitly signal readiness
@@ -187,13 +193,15 @@
187193
### Patch Changes
188194

189195
- Move Collections to their own packages ([#252](https://github.com/TanStack/db/pull/252))
196+
190197
- Move local-only and local-storage collections to main `@tanstack/db` package
191198
- Create new `@tanstack/electric-db-collection` package for Electric SQL integration
192199
- Create new `@tanstack/query-db-collection` package for TanStack Query integration
193200
- Delete `@tanstack/db-collections` package (removed from repo)
194201
- Update example app and documentation to use new package structure
195202

196203
Why?
204+
197205
- Better separation of concerns
198206
- Independent versioning for each collection type
199207
- Cleaner dependencies (electric collections don't need query deps, etc.)

packages/react-db/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
Adds automatic lifecycle management for collections to optimize resource usage.
135135

136136
**New Features:**
137+
137138
- Added `startSync` option (defaults to `false`, set to `true` to start syncing immediately)
138139
- Automatic garbage collection after `gcTime` (default 5 minutes) of inactivity
139140
- Collection status tracking: "idle" | "loading" | "ready" | "error" | "cleaned-up"
@@ -240,6 +241,7 @@
240241
### Patch Changes
241242

242243
- A large refactor of the core `Collection` with: ([#155](https://github.com/TanStack/db/pull/155))
244+
243245
- a change to not use Store internally and emit fine grade changes with `subscribeChanges` and `subscribeKeyChanges` methods.
244246
- changes to the `Collection` api to be more `Map` like for reads, with `get`, `has`, `size`, `entries`, `keys`, and `values`.
245247
- renames `config.getId` to `config.getKey` for consistency with the `Map` like api.
@@ -254,6 +256,7 @@
254256
- Expose utilities on collection instances ([#161](https://github.com/TanStack/db/pull/161))
255257

256258
Implemented a utility exposure pattern for TanStack DB collections that allows utility functions to be passed as part of collection options and exposes them under a `.utils` namespace, with full TypeScript typing.
259+
257260
- Refactored `createCollection` in packages/db/src/collection.ts to accept options with utilities directly
258261
- Added `utils` property to CollectionImpl
259262
- Added TypeScript types for utility functions and utility records

packages/svelte-db/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
node_modules
2+
3+
# Output
4+
.output
5+
.vercel
6+
.netlify
7+
.wrangler
8+
/.svelte-kit
9+
/build
10+
/dist
11+
12+
# OS
13+
.DS_Store
14+
Thumbs.db
15+
16+
# Env
17+
.env
18+
.env.*
19+
!.env.example
20+
!.env.test
21+
22+
# Vite
23+
vite.config.js.timestamp-*
24+
vite.config.ts.timestamp-*

packages/svelte-db/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @tanstack/svelte-db
2+
3+
Svelte helpers for TanStack DB. See [TanStack/db](https://github.com/TanStack/db) for more details.

0 commit comments

Comments
 (0)