-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathcollections.ts
More file actions
170 lines (165 loc) · 4.95 KB
/
collections.ts
File metadata and controls
170 lines (165 loc) · 4.95 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
import { createCollection } from "@tanstack/react-db"
import { electricCollectionOptions } from "@tanstack/electric-db-collection"
import { queryCollectionOptions } from "@tanstack/query-db-collection"
import { QueryClient } from "@tanstack/query-core"
import { selectConfigSchema, selectTodoSchema } from "../db/validation"
import { api } from "./api"
// Create a query client for query collections
const queryClient = new QueryClient()
// 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
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
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
return await api.todos.update(original.id, changes)
})
)
},
onDelete: async ({ transaction }) => {
return await Promise.all(
transaction.mutations.map(async (mutation) => {
const { original } = mutation
await api.todos.delete(original.id)
})
)
},
})
)
// 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
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
const response = await api.config.update(original.id, changes)
return response.txid
})
)
return { txid: txids }
},
})
)