1
1
import { createCollection } from "@tanstack/react-db"
2
+ import { initializeDbDevtools } from "@tanstack/react-db-devtools"
2
3
import { electricCollectionOptions } from "@tanstack/electric-db-collection"
3
4
import { queryCollectionOptions } from "@tanstack/query-db-collection"
4
5
import { trailBaseCollectionOptions } from "@tanstack/trailbase-db-collection"
@@ -11,13 +12,16 @@ import type { SelectConfig, SelectTodo } from "../db/validation"
11
12
// Create a query client for query collections
12
13
const queryClient = new QueryClient ( )
13
14
15
+ // Initialize DB devtools early (idempotent - safe to call multiple times)
16
+ initializeDbDevtools ( )
17
+
14
18
// Create a TrailBase client.
15
19
const trailBaseClient = initClient ( `http://localhost:4000` )
16
20
17
21
// Electric Todo Collection
18
22
export const electricTodoCollection = createCollection (
19
23
electricCollectionOptions ( {
20
- id : `todos` ,
24
+ id : `electric- todos` ,
21
25
shapeOptions : {
22
26
url : `http://localhost:3003/v1/shape` ,
23
27
params : {
@@ -43,6 +47,9 @@ export const electricTodoCollection = createCollection(
43
47
const txids = await Promise . all (
44
48
transaction . mutations . map ( async ( mutation ) => {
45
49
const { original, changes } = mutation
50
+ if ( ! ( `id` in original ) ) {
51
+ throw new Error ( `Original todo not found for update` )
52
+ }
46
53
const response = await api . todos . update ( original . id , changes )
47
54
return response . txid
48
55
} )
@@ -53,6 +60,9 @@ export const electricTodoCollection = createCollection(
53
60
const txids = await Promise . all (
54
61
transaction . mutations . map ( async ( mutation ) => {
55
62
const { original } = mutation
63
+ if ( ! ( `id` in original ) ) {
64
+ throw new Error ( `Original todo not found for delete` )
65
+ }
56
66
const response = await api . todos . delete ( original . id )
57
67
return response . txid
58
68
} )
@@ -65,7 +75,7 @@ export const electricTodoCollection = createCollection(
65
75
// Query Todo Collection
66
76
export const queryTodoCollection = createCollection (
67
77
queryCollectionOptions ( {
68
- id : `todos` ,
78
+ id : `query- todos` ,
69
79
queryKey : [ `todos` ] ,
70
80
refetchInterval : 3000 ,
71
81
queryFn : async ( ) => {
@@ -92,6 +102,9 @@ export const queryTodoCollection = createCollection(
92
102
return await Promise . all (
93
103
transaction . mutations . map ( async ( mutation ) => {
94
104
const { original, changes } = mutation
105
+ if ( ! ( `id` in original ) ) {
106
+ throw new Error ( `Original todo not found for update` )
107
+ }
95
108
return await api . todos . update ( original . id , changes )
96
109
} )
97
110
)
@@ -100,6 +113,9 @@ export const queryTodoCollection = createCollection(
100
113
return await Promise . all (
101
114
transaction . mutations . map ( async ( mutation ) => {
102
115
const { original } = mutation
116
+ if ( ! ( `id` in original ) ) {
117
+ throw new Error ( `Original todo not found for delete` )
118
+ }
103
119
await api . todos . delete ( original . id )
104
120
} )
105
121
)
@@ -118,7 +134,7 @@ type Todo = {
118
134
// TrailBase Todo Collection
119
135
export const trailBaseTodoCollection = createCollection (
120
136
trailBaseCollectionOptions < SelectTodo , Todo > ( {
121
- id : `todos` ,
137
+ id : `trailbase- todos` ,
122
138
getKey : ( item ) => item . id ,
123
139
schema : selectTodoSchema ,
124
140
recordApi : trailBaseClient . records ( `todos` ) ,
@@ -137,7 +153,7 @@ export const trailBaseTodoCollection = createCollection(
137
153
// Electric Config Collection
138
154
export const electricConfigCollection = createCollection (
139
155
electricCollectionOptions ( {
140
- id : `config` ,
156
+ id : `electric- config` ,
141
157
shapeOptions : {
142
158
url : `http://localhost:3003/v1/shape` ,
143
159
params : {
@@ -158,6 +174,9 @@ export const electricConfigCollection = createCollection(
158
174
const txids = await Promise . all (
159
175
transaction . mutations . map ( async ( mutation ) => {
160
176
const { original, changes } = mutation
177
+ if ( ! ( `id` in original ) ) {
178
+ throw new Error ( `Original config not found for update` )
179
+ }
161
180
const response = await api . config . update ( original . id , changes )
162
181
return response . txid
163
182
} )
@@ -170,7 +189,7 @@ export const electricConfigCollection = createCollection(
170
189
// Query Config Collection
171
190
export const queryConfigCollection = createCollection (
172
191
queryCollectionOptions ( {
173
- id : `config` ,
192
+ id : `query- config` ,
174
193
queryKey : [ `config` ] ,
175
194
refetchInterval : 3000 ,
176
195
queryFn : async ( ) => {
@@ -193,6 +212,9 @@ export const queryConfigCollection = createCollection(
193
212
const txids = await Promise . all (
194
213
transaction . mutations . map ( async ( mutation ) => {
195
214
const { original, changes } = mutation
215
+ if ( ! ( `id` in original ) ) {
216
+ throw new Error ( `Original config not found for update` )
217
+ }
196
218
const response = await api . config . update ( original . id , changes )
197
219
return response . txid
198
220
} )
@@ -213,7 +235,7 @@ type Config = {
213
235
// TrailBase Config Collection
214
236
export const trailBaseConfigCollection = createCollection (
215
237
trailBaseCollectionOptions < SelectConfig , Config > ( {
216
- id : `config` ,
238
+ id : `trailbase- config` ,
217
239
getKey : ( item ) => item . id ,
218
240
schema : selectConfigSchema ,
219
241
recordApi : trailBaseClient . records ( `config` ) ,
0 commit comments