Skip to content

Commit 1d98a77

Browse files
authored
fix zod and lint errors in the react todo example (#394)
1 parent bbed2f2 commit 1d98a77

File tree

6 files changed

+66
-29
lines changed

6 files changed

+66
-29
lines changed

eslint.config.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import { tanstackConfig } from "@tanstack/config/eslint"
55

66
export default [
77
...tanstackConfig,
8-
{ ignores: [`dist/`] },
8+
{
9+
ignores: [
10+
`**/dist/**`,
11+
`**/.output/**`,
12+
`**/.nitro/**`,
13+
`**/traildepot/**`,
14+
],
15+
},
916
{
1017
plugins: {
1118
stylistic: stylisticPlugin,

examples/react/todo/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"@tanstack/trailbase-db-collection": "^0.1.0",
1313
"cors": "^2.8.5",
1414
"drizzle-orm": "^0.40.1",
15-
"drizzle-zod": "^0.7.0",
15+
"drizzle-zod": "^0.8.3",
16+
"zod": "^4.0.17",
1617
"express": "^4.19.2",
1718
"postgres": "^3.4.7",
1819
"react": "^19.1.0",

examples/react/todo/src/db/validation.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
11
import { createInsertSchema, createSelectSchema } from "drizzle-zod"
2-
import { z } from "zod"
32
import { config, todos } from "./schema"
3+
import type { z } from "zod"
44

5-
// Date transformation schema - handles Date objects, ISO strings, and parseable date strings
6-
const dateStringToDate = z
7-
.union([
8-
z.date(), // Already a Date object
9-
z
10-
.string()
11-
.datetime()
12-
.transform((str) => new Date(str)), // ISO datetime string
13-
z.string().transform((str) => new Date(str)), // Any parseable date string
14-
])
15-
.optional()
16-
17-
// Auto-generated schemas from Drizzle schema with date transformation
18-
export const insertTodoSchema = createInsertSchema(todos, {
19-
created_at: dateStringToDate,
20-
updated_at: dateStringToDate,
5+
// Auto-generated schemas from Drizzle schema (omit auto-generated fields)
6+
export const insertTodoSchema = createInsertSchema(todos).omit({
7+
id: true,
8+
created_at: true,
9+
updated_at: true,
2110
})
2211
export const selectTodoSchema = createSelectSchema(todos)
2312

2413
// Partial schema for updates
2514
export const updateTodoSchema = insertTodoSchema.partial().strict()
2615

27-
// Config schemas with date transformation
28-
export const insertConfigSchema = createInsertSchema(config, {
29-
created_at: dateStringToDate,
30-
updated_at: dateStringToDate,
31-
}).strict()
16+
// Config schemas (omit auto-generated fields)
17+
export const insertConfigSchema = createInsertSchema(config).omit({
18+
id: true,
19+
created_at: true,
20+
updated_at: true,
21+
})
3222
export const selectConfigSchema = createSelectSchema(config)
3323
export const updateConfigSchema = insertConfigSchema.partial().strict()
3424

examples/react/todo/src/lib/collections.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ export const electricTodoCollection = createCollection(
4343
const txids = await Promise.all(
4444
transaction.mutations.map(async (mutation) => {
4545
const { original, changes } = mutation
46+
if (!(`id` in original)) {
47+
throw new Error(`Original todo not found for update`)
48+
}
4649
const response = await api.todos.update(original.id, changes)
4750
return response.txid
4851
})
@@ -53,6 +56,9 @@ export const electricTodoCollection = createCollection(
5356
const txids = await Promise.all(
5457
transaction.mutations.map(async (mutation) => {
5558
const { original } = mutation
59+
if (!(`id` in original)) {
60+
throw new Error(`Original todo not found for delete`)
61+
}
5662
const response = await api.todos.delete(original.id)
5763
return response.txid
5864
})
@@ -92,6 +98,9 @@ export const queryTodoCollection = createCollection(
9298
return await Promise.all(
9399
transaction.mutations.map(async (mutation) => {
94100
const { original, changes } = mutation
101+
if (!(`id` in original)) {
102+
throw new Error(`Original todo not found for update`)
103+
}
95104
return await api.todos.update(original.id, changes)
96105
})
97106
)
@@ -100,6 +109,9 @@ export const queryTodoCollection = createCollection(
100109
return await Promise.all(
101110
transaction.mutations.map(async (mutation) => {
102111
const { original } = mutation
112+
if (!(`id` in original)) {
113+
throw new Error(`Original todo not found for delete`)
114+
}
103115
await api.todos.delete(original.id)
104116
})
105117
)
@@ -158,6 +170,9 @@ export const electricConfigCollection = createCollection(
158170
const txids = await Promise.all(
159171
transaction.mutations.map(async (mutation) => {
160172
const { original, changes } = mutation
173+
if (!(`id` in original)) {
174+
throw new Error(`Original config not found for update`)
175+
}
161176
const response = await api.config.update(original.id, changes)
162177
return response.txid
163178
})
@@ -193,6 +208,9 @@ export const queryConfigCollection = createCollection(
193208
const txids = await Promise.all(
194209
transaction.mutations.map(async (mutation) => {
195210
const { original, changes } = mutation
211+
if (!(`id` in original)) {
212+
throw new Error(`Original config not found for update`)
213+
}
196214
const response = await api.config.update(original.id, changes)
197215
return response.txid
198216
})

examples/react/todo/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"baseUrl": ".",
55
"module": "ES2022",
66
"moduleResolution": "bundler",
7+
"jsx": "react-jsx",
78
"paths": {
89
"@/*": ["./src/*"]
910
}

pnpm-lock.yaml

Lines changed: 25 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)