Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

collections.ts

228 lines6.6 KB

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 }),
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 }),
109​
110type Todo = {
111 id: number
112 text: string
113 completed: boolean
114 created_at: number
115 updated_at: number
117​
118// TrailBase Todo Collection
119export const trailBaseTodoCollection = createCollection(
120 trailBaseCollectionOptions<SelectTodo, Todo>({
121 id: `todos`,
122 getKey: (item) => item.id,
123 recordApi: trailBaseClient.records(`todos`),
124 // Re-using the example's drizzle-schema requires remapping the items.
125 parse: {
126 created_at: (ts) => new Date(ts * 1000),
127 updated_at: (ts) => new Date(ts * 1000),
128 },
129 serialize: {
130 created_at: (date) => Math.floor(date.valueOf() / 1000),
131 updated_at: (date) => Math.floor(date.valueOf() / 1000),
132 },
133 }),
135​
136// Electric Config Collection
137export const electricConfigCollection = createCollection(
138 electricCollectionOptions({
139 id: `config`,
140 shapeOptions: {
141 url: `http://localhost:3003/v1/shape`,
142 params: {
143 table: `config`,
144 },
145 parser: {
146 timestamptz: (date: string) => new Date(date),
147 },
148 },
149 getKey: (item) => item.id,
150 schema: selectConfigSchema,
151 onInsert: async ({ transaction }) => {
152 const modified = transaction.mutations[0].modified
153 const response = await api.config.create(modified)
154 return { txid: response.txid }
155 },
156 onUpdate: async ({ transaction }) => {
157 const txids = await Promise.all(
158 transaction.mutations.map(async (mutation) => {
159 const { original, changes } = mutation
160 const response = await api.config.update(original.id, changes)
161 return response.txid
162 }),
163 )
164 return { txid: txids }
165 },
166 }),
168​
169// Query Config Collection
170export const queryConfigCollection = createCollection(
171 queryCollectionOptions({
172 id: `config`,
173 queryKey: [`config`],
174 refetchInterval: 3000,
175 queryFn: async () => {
176 const configs = await api.config.getAll()
177 return configs.map((config) => ({
178 ...config,
179 created_at: new Date(config.created_at),
180 updated_at: new Date(config.updated_at),
181 }))
182 },
183 getKey: (item) => item.id,
184 schema: selectConfigSchema,
185 queryClient,
186 onInsert: async ({ transaction }) => {
187 const modified = transaction.mutations[0].modified
188 const response = await api.config.create(modified)
189 return { txid: response.txid }
190 },
191 onUpdate: async ({ transaction }) => {
192 const txids = await Promise.all(
193 transaction.mutations.map(async (mutation) => {
194 const { original, changes } = mutation
195 const response = await api.config.update(original.id, changes)
196 return response.txid
197 }),
198 )
199 return { txid: txids }
200 },
201 }),
203​
204type Config = {
205 id: number
206 key: string
207 value: string
208 created_at: number
209 updated_at: number
211​
212// TrailBase Config Collection
213export const trailBaseConfigCollection = createCollection(
214 trailBaseCollectionOptions<SelectConfig, Config>({
215 id: `config`,
216 getKey: (item) => item.id,
217 recordApi: trailBaseClient.records(`config`),
218 // Re-using the example's drizzle-schema requires remapping the items.
219 parse: {
220 created_at: (ts) => new Date(ts * 1000),
221 updated_at: (ts) => new Date(ts * 1000),
222 },
223 serialize: {
224 created_at: (date) => Math.floor(date.valueOf() / 1000),
225 updated_at: (date) => Math.floor(date.valueOf() / 1000),
226 },
227 }),