Skip to content

Commit

Permalink
Created insert and select schema for User and Car tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy Miller committed Nov 10, 2023
1 parent 72b733c commit 9065b0d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 0 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20231025.0",
"bun-types": "^1.0.11",
"drizzle-kit": "^0.20.1",
"typescript": "^5.2.2",
"wrangler": "^3.15.0"
Expand Down
8 changes: 5 additions & 3 deletions packages/api/src/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InferInsertModel, InferSelectModel } from 'drizzle-orm'
import { integer, real, sqliteTable, text } from 'drizzle-orm/sqlite-core'
import { createInsertSchema } from 'drizzle-valibot'
import { createInsertSchema, createSelectSchema } from 'drizzle-valibot'

// User
export const UserTable = sqliteTable('User', {
Expand All @@ -10,7 +10,8 @@ export const UserTable = sqliteTable('User', {

export type User = InferSelectModel<typeof UserTable>
export type InsertUser = InferInsertModel<typeof UserTable>
export const UserSchema = createInsertSchema(UserTable)
export const insertUserSchema = createInsertSchema(UserTable)
export const selectUserSchema = createSelectSchema(UserTable)

// Car
export const CarTable = sqliteTable('Car', {
Expand All @@ -27,4 +28,5 @@ export const CarTable = sqliteTable('Car', {

export type Car = InferSelectModel<typeof CarTable>
export type InsertCar = InferInsertModel<typeof CarTable>
export const CarSchema = createInsertSchema(CarTable)
export const insertCarSchema = createInsertSchema(CarTable)
export const selectCarSchema = createSelectSchema(CarTable)
4 changes: 2 additions & 2 deletions packages/api/src/routes/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { eq } from 'drizzle-orm'
import { parse } from 'valibot'
import { type User, UserSchema, UserTable } from '../db/schema'
import { insertUserSchema, UserTable } from '../db/schema'
import { protectedProcedure, router } from '../trpc'

export const userRouter = router({
Expand All @@ -10,7 +10,7 @@ export const userRouter = router({
return user
}),
create: protectedProcedure
.input((raw) => parse(UserSchema, raw))
.input((raw) => parse(insertUserSchema, raw))
.mutation(async ({ ctx, input }) => {
const { db } = ctx
await db.insert(UserTable).values(input).run()
Expand Down

0 comments on commit 9065b0d

Please sign in to comment.