11import { createCollection } from "@tanstack/react-db"
2+ import { initializeDbDevtools } from "@tanstack/react-db-devtools"
23import { electricCollectionOptions } from "@tanstack/electric-db-collection"
34import { queryCollectionOptions } from "@tanstack/query-db-collection"
45import { trailBaseCollectionOptions } from "@tanstack/trailbase-db-collection"
@@ -11,13 +12,16 @@ import type { SelectConfig, SelectTodo } from "../db/validation"
1112// Create a query client for query collections
1213const queryClient = new QueryClient ( )
1314
15+ // Initialize DB devtools early (idempotent - safe to call multiple times)
16+ initializeDbDevtools ( )
17+
1418// Create a TrailBase client.
1519const trailBaseClient = initClient ( `http://localhost:4000` )
1620
1721// Electric Todo Collection
1822export const electricTodoCollection = createCollection (
1923 electricCollectionOptions ( {
20- id : `todos` ,
24+ id : `electric- todos` ,
2125 shapeOptions : {
2226 url : `http://localhost:3003/v1/shape` ,
2327 params : {
@@ -43,6 +47,9 @@ export const electricTodoCollection = createCollection(
4347 const txids = await Promise . all (
4448 transaction . mutations . map ( async ( mutation ) => {
4549 const { original, changes } = mutation
50+ if ( ! ( `id` in original ) ) {
51+ throw new Error ( `Original todo not found for update` )
52+ }
4653 const response = await api . todos . update ( original . id , changes )
4754 return response . txid
4855 } )
@@ -53,6 +60,9 @@ export const electricTodoCollection = createCollection(
5360 const txids = await Promise . all (
5461 transaction . mutations . map ( async ( mutation ) => {
5562 const { original } = mutation
63+ if ( ! ( `id` in original ) ) {
64+ throw new Error ( `Original todo not found for delete` )
65+ }
5666 const response = await api . todos . delete ( original . id )
5767 return response . txid
5868 } )
@@ -65,7 +75,7 @@ export const electricTodoCollection = createCollection(
6575// Query Todo Collection
6676export const queryTodoCollection = createCollection (
6777 queryCollectionOptions ( {
68- id : `todos` ,
78+ id : `query- todos` ,
6979 queryKey : [ `todos` ] ,
7080 refetchInterval : 3000 ,
7181 queryFn : async ( ) => {
@@ -92,6 +102,9 @@ export const queryTodoCollection = createCollection(
92102 return await Promise . all (
93103 transaction . mutations . map ( async ( mutation ) => {
94104 const { original, changes } = mutation
105+ if ( ! ( `id` in original ) ) {
106+ throw new Error ( `Original todo not found for update` )
107+ }
95108 return await api . todos . update ( original . id , changes )
96109 } )
97110 )
@@ -100,6 +113,9 @@ export const queryTodoCollection = createCollection(
100113 return await Promise . all (
101114 transaction . mutations . map ( async ( mutation ) => {
102115 const { original } = mutation
116+ if ( ! ( `id` in original ) ) {
117+ throw new Error ( `Original todo not found for delete` )
118+ }
103119 await api . todos . delete ( original . id )
104120 } )
105121 )
@@ -118,7 +134,7 @@ type Todo = {
118134// TrailBase Todo Collection
119135export const trailBaseTodoCollection = createCollection (
120136 trailBaseCollectionOptions < SelectTodo , Todo > ( {
121- id : `todos` ,
137+ id : `trailbase- todos` ,
122138 getKey : ( item ) => item . id ,
123139 schema : selectTodoSchema ,
124140 recordApi : trailBaseClient . records ( `todos` ) ,
@@ -137,7 +153,7 @@ export const trailBaseTodoCollection = createCollection(
137153// Electric Config Collection
138154export const electricConfigCollection = createCollection (
139155 electricCollectionOptions ( {
140- id : `config` ,
156+ id : `electric- config` ,
141157 shapeOptions : {
142158 url : `http://localhost:3003/v1/shape` ,
143159 params : {
@@ -158,6 +174,9 @@ export const electricConfigCollection = createCollection(
158174 const txids = await Promise . all (
159175 transaction . mutations . map ( async ( mutation ) => {
160176 const { original, changes } = mutation
177+ if ( ! ( `id` in original ) ) {
178+ throw new Error ( `Original config not found for update` )
179+ }
161180 const response = await api . config . update ( original . id , changes )
162181 return response . txid
163182 } )
@@ -170,7 +189,7 @@ export const electricConfigCollection = createCollection(
170189// Query Config Collection
171190export const queryConfigCollection = createCollection (
172191 queryCollectionOptions ( {
173- id : `config` ,
192+ id : `query- config` ,
174193 queryKey : [ `config` ] ,
175194 refetchInterval : 3000 ,
176195 queryFn : async ( ) => {
@@ -193,6 +212,9 @@ export const queryConfigCollection = createCollection(
193212 const txids = await Promise . all (
194213 transaction . mutations . map ( async ( mutation ) => {
195214 const { original, changes } = mutation
215+ if ( ! ( `id` in original ) ) {
216+ throw new Error ( `Original config not found for update` )
217+ }
196218 const response = await api . config . update ( original . id , changes )
197219 return response . txid
198220 } )
@@ -213,7 +235,7 @@ type Config = {
213235// TrailBase Config Collection
214236export const trailBaseConfigCollection = createCollection (
215237 trailBaseCollectionOptions < SelectConfig , Config > ( {
216- id : `config` ,
238+ id : `trailbase- config` ,
217239 getKey : ( item ) => item . id ,
218240 schema : selectConfigSchema ,
219241 recordApi : trailBaseClient . records ( `config` ) ,
0 commit comments