Open History to see the last commit
1import { createCollection } from "@tanstack/solid-db"
2import { electricCollectionOptions } from "@tanstack/electric-db-collection"
3import { queryCollectionOptions } from "@tanstack/query-db-collection"
4import { trailBaseCollectionOptions } from "@tanstack/trailbase-db-collection"
5import { QueryClient } from "@tanstack/query-core"
6import { initClient } from "trailbase"
7import { selectConfigSchema, selectTodoSchema } from "../db/validation"
8import { api } from "./api"
9import type { SelectConfig, SelectTodo } from "../db/validation"
10
11// Create a query client for query collections
12const queryClient = new QueryClient()
13
14// Create a TrailBase client.
15const trailBaseClient = initClient(`http://localhost:4000`)
16
17// Electric Todo Collection
18export const electricTodoCollection = createCollection(
19 electricCollectionOptions({
20 id: `todos`,
21 shapeOptions: {
22 url: `http://localhost:3003/v1/shape`,
23 params: {
24 table: `todos`,
25 },
26 parser: {
27 timestamptz: (date: string) => new Date(date),
28 },
29 },
30 getKey: (item) => item.id,
31 schema: selectTodoSchema,
32 onInsert: async ({ transaction }) => {
33 const {
34 id: _id,
35 created_at: _f,
36 updated_at: _ff,
37 ...modified
38 } = transaction.mutations[0].modified
39 const response = await api.todos.create(modified)
40 return { txid: response.txid }
41 },
42 onUpdate: async ({ transaction }) => {
43 const txids = await Promise.all(
44 transaction.mutations.map(async (mutation) => {
45 const { original, changes } = mutation
46 const response = await api.todos.update(original.id, changes)
47 return response.txid
48 })
49 )
50 return { txid: txids }
51 },
52 onDelete: async ({ transaction }) => {
53 const txids = await Promise.all(
54 transaction.mutations.map(async (mutation) => {
55 const { original } = mutation
56 const response = await api.todos.delete(original.id)
57 return response.txid
58 })
59 )
60 return { txid: txids }
61 },
62 })
63)
64
65// Query Todo Collection
66export const queryTodoCollection = createCollection(
67 queryCollectionOptions({
68 id: `todos`,
69 queryKey: [`todos`],
70 refetchInterval: 3000,
71 queryFn: async () => {
72 const todos = await api.todos.getAll()
73 return todos.map((todo) => ({
74 ...todo,
75 created_at: new Date(todo.created_at),
76 updated_at: new Date(todo.updated_at),
77 }))
78 },
79 getKey: (item) => item.id,
80 schema: selectTodoSchema,
81 queryClient,
82 onInsert: async ({ transaction }) => {
83 const {
84 id: _id,
85 created_at: _crea,
86 updated_at: _up,
87 ...modified
88 } = transaction.mutations[0].modified
89 return await api.todos.create(modified)
90 },
91 onUpdate: async ({ transaction }) => {
92 return await Promise.all(
93 transaction.mutations.map(async (mutation) => {
94 const { original, changes } = mutation
95 return await api.todos.update(original.id, changes)
96 })
97 )
98 },
99 onDelete: async ({ transaction }) => {
100 return await Promise.all(
101 transaction.mutations.map(async (mutation) => {
102 const { original } = mutation
103 await api.todos.delete(original.id)
104 })
105 )
106 },
107 })
108)
109
110type Todo = {
111 id: number
112 text: string
113 completed: boolean
114 created_at: number
115 updated_at: number
116}
117
118// TrailBase Todo Collection
119export const trailBaseTodoCollection = createCollection(
120 trailBaseCollectionOptions<SelectTodo, Todo>({
121 id: `todos`,
122 getKey: (item) => item.id,
123 schema: selectTodoSchema,
124 recordApi: trailBaseClient.records(`todos`),
125 // Re-using the example's drizzle-schema requires remapping the items.
126 parse: {
127 created_at: (ts) => new Date(ts * 1000),
128 updated_at: (ts) => new Date(ts * 1000),
129 },
130 serialize: {
131 created_at: (date) => Math.floor(date.valueOf() / 1000),
132 updated_at: (date) => Math.floor(date.valueOf() / 1000),
133 },
134 })
135)
136
137// Electric Config Collection
138export const electricConfigCollection = createCollection(
139 electricCollectionOptions({
140 id: `config`,
141 shapeOptions: {
142 url: `http://localhost:3003/v1/shape`,
143 params: {
144 table: `config`,
145 },
146 parser: {
147 timestamptz: (date: string) => new Date(date),
148 },
149 },
150 getKey: (item) => item.id,
151 schema: selectConfigSchema,
152 onInsert: async ({ transaction }) => {
153 const modified = transaction.mutations[0].modified
154 const response = await api.config.create(modified)
155 return { txid: response.txid }
156 },
157 onUpdate: async ({ transaction }) => {
158 const txids = await Promise.all(
159 transaction.mutations.map(async (mutation) => {
160 const { original, changes } = mutation
161 const response = await api.config.update(original.id, changes)
162 return response.txid
163 })
164 )
165 return { txid: txids }
166 },
167 })
168)
169
170// Query Config Collection
171export const queryConfigCollection = createCollection(
172 queryCollectionOptions({
173 id: `config`,
174 queryKey: [`config`],
175 refetchInterval: 3000,
176 queryFn: async () => {
177 const configs = await api.config.getAll()
178 return configs.map((config) => ({
179 ...config,
180 created_at: new Date(config.created_at),
181 updated_at: new Date(config.updated_at),
182 }))
183 },
184 getKey: (item) => item.id,
185 schema: selectConfigSchema,
186 queryClient,
187 onInsert: async ({ transaction }) => {
188 const modified = transaction.mutations[0].modified
189 const response = await api.config.create(modified)
190 return { txid: response.txid }
191 },
192 onUpdate: async ({ transaction }) => {
193 const txids = await Promise.all(
194 transaction.mutations.map(async (mutation) => {
195 const { original, changes } = mutation
196 const response = await api.config.update(original.id, changes)
197 return response.txid
198 })
199 )
200 return { txid: txids }
201 },
202 })
203)
204
205type Config = {
206 id: number
207 key: string
208 value: string
209 created_at: number
210 updated_at: number
211}
212
213// TrailBase Config Collection
214export const trailBaseConfigCollection = createCollection(
215 trailBaseCollectionOptions<SelectConfig, Config>({
216 id: `config`,
217 getKey: (item) => item.id,
218 schema: selectConfigSchema,
219 recordApi: trailBaseClient.records(`config`),
220 // Re-using the example's drizzle-schema requires remapping the items.
221 parse: {
222 created_at: (ts) => new Date(ts * 1000),
223 updated_at: (ts) => new Date(ts * 1000),
224 },
225 serialize: {
226 created_at: (date) => Math.floor(date.valueOf() / 1000),
227 updated_at: (date) => Math.floor(date.valueOf() / 1000),
228 },
229 })
230)