Skip to content

Commit 91d5e2c

Browse files
feat: add drizzle-zod integration for schema validation in database models
1 parent ab75903 commit 91d5e2c

File tree

8 files changed

+32
-1
lines changed

8 files changed

+32
-1
lines changed

bun.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dataloader": "^2.2.3",
1313
"drizzle-kit": "^0.31.8",
1414
"drizzle-orm": "^0.45.1",
15+
"drizzle-zod": "^0.8.3",
1516
"graphql": "^16.12.0",
1617
"jsonwebtoken": "^9.0.3",
1718
"nitroping": "^0.1.0",

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"dataloader": "^2.2.3",
3232
"drizzle-kit": "^0.31.8",
3333
"drizzle-orm": "^0.45.1",
34+
"drizzle-zod": "^0.8.3",
3435
"graphql": "^16.12.0",
3536
"jsonwebtoken": "^9.0.3",
3637
"nitroping": "^0.1.0",

server/database/schema/apiKey.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { relations } from 'drizzle-orm'
22
import { boolean, jsonb, pgTable, text, uuid } from 'drizzle-orm/pg-core'
3+
import { createInsertSchema, createSelectSchema } from 'drizzle-zod'
34
import { customTimestamp, uuidv7Generator } from '../shared'
45
import { app } from './app'
56

@@ -22,3 +23,6 @@ export const apiKeyRelations = relations(apiKey, ({ one }) => ({
2223
references: [app.id],
2324
}),
2425
}))
26+
27+
export const selectApiKeySchema = createSelectSchema(apiKey)
28+
export const insertApiKeySchema = createInsertSchema(apiKey)

server/database/schema/app.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { relations } from 'drizzle-orm'
22
import { boolean, pgTable, text, uuid } from 'drizzle-orm/pg-core'
3+
import { createInsertSchema, createSelectSchema } from 'drizzle-zod'
34
import { customTimestamp, uuidv7Generator } from '../shared'
45
import { apiKey } from './apiKey'
56
import { device } from './device'
@@ -30,3 +31,6 @@ export const appRelations = relations(app, ({ many }) => ({
3031
notifications: many(notification),
3132
apiKeys: many(apiKey),
3233
}))
34+
35+
export const selectAppSchema = createSelectSchema(app)
36+
export const insertAppSchema = createInsertSchema(app)

server/database/schema/deliveryLog.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { relations } from 'drizzle-orm'
22
import { integer, jsonb, pgTable, text, unique, uuid } from 'drizzle-orm/pg-core'
3+
import { createInsertSchema, createSelectSchema } from 'drizzle-zod'
34
import { customTimestamp, uuidv7Generator } from '../shared'
45
import { device } from './device'
56
import { deliveryStatusEnum } from './enums'
@@ -37,3 +38,6 @@ export const deliveryLogRelations = relations(deliveryLog, ({ one }) => ({
3738
references: [device.id],
3839
}),
3940
}))
41+
42+
export const selectDeliveryLogSchema = createSelectSchema(deliveryLog)
43+
export const insertDeliveryLogSchema = createInsertSchema(deliveryLog)

server/database/schema/device.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { relations } from 'drizzle-orm'
22
import { jsonb, pgTable, text, unique, uuid } from 'drizzle-orm/pg-core'
3+
import { createInsertSchema, createSelectSchema } from 'drizzle-zod'
34
import { customTimestamp, uuidv7Generator } from '../shared'
45
import { app } from './app'
56
import { deliveryLog } from './deliveryLog'
@@ -28,3 +29,6 @@ export const deviceRelations = relations(device, ({ one, many }) => ({
2829
}),
2930
deliveryLogs: many(deliveryLog),
3031
}))
32+
33+
export const selectDeviceSchema = createSelectSchema(device)
34+
export const insertDeviceSchema = createInsertSchema(device)

server/database/schema/notification.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { relations } from 'drizzle-orm'
22
import { integer, jsonb, pgTable, text, uuid } from 'drizzle-orm/pg-core'
3+
import { createInsertSchema, createSelectSchema } from 'drizzle-zod'
34
import { customTimestamp, uuidv7Generator } from '../shared'
45
import { app } from './app'
56
import { deliveryLog } from './deliveryLog'
@@ -39,3 +40,6 @@ export const notificationRelations = relations(notification, ({ one, many }) =>
3940
}),
4041
deliveryLogs: many(deliveryLog),
4142
}))
43+
44+
export const selectNotificationSchema = createSelectSchema(notification)
45+
export const insertNotificationSchema = createInsertSchema(notification)

server/graphql/schema.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { defineSchema } from 'nitro-graphql/utils/define'
2+
import { selectApiKeySchema } from '~~/server/database/schema/apiKey'
3+
import { selectAppSchema } from '~~/server/database/schema/app'
4+
import { selectDeliveryLogSchema } from '~~/server/database/schema/deliveryLog'
5+
import { selectDeviceSchema } from '~~/server/database/schema/device'
6+
import { selectNotificationSchema } from '~~/server/database/schema/notification'
27

38
export default defineSchema({
4-
9+
App: selectAppSchema,
10+
Device: selectDeviceSchema,
11+
Notification: selectNotificationSchema,
12+
DeliveryLog: selectDeliveryLogSchema,
13+
ApiKey: selectApiKeySchema,
514
})

0 commit comments

Comments
 (0)