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