forked from imbytecat/fullstack-starter
- 添加 @orpc/contract 依赖以支持合约定义和类型安全。 - 添加 @orpc/contract 依赖以支持契约定义和类型安全。 - 更新客户端类型定义并移除冗余的 APIRouterClient 引入,确保客户端实例类型与路由定义一致。 - 添加基于 zod 的类型安全接口定义,包含待办事项的增删改查操作契约及对应的输入输出验证规则。 - 使用合约定义重构 Todo 处理函数,统一接口输入输出验证并移除冗余的 Zod 模式定义。 - 更新导出模块,将路由功能改为导出合约定义。 - 移除未使用的导入和类型定义,精简路由配置文件。
52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { implement, ORPCError } from '@orpc/server'
|
|
import { eq } from 'drizzle-orm'
|
|
import { todoTable } from '@/db/schema'
|
|
import { todoContract } from '@/orpc/contract'
|
|
import { dbProvider } from '@/orpc/middlewares'
|
|
|
|
export const list = implement(todoContract.list)
|
|
.use(dbProvider)
|
|
.handler(async ({ context }) => {
|
|
const todos = await context.db.query.todoTable.findMany({
|
|
orderBy: (todos, { desc }) => [desc(todos.createdAt)],
|
|
})
|
|
return todos
|
|
})
|
|
|
|
export const create = implement(todoContract.create)
|
|
.use(dbProvider)
|
|
.handler(async ({ context, input }) => {
|
|
const [newTodo] = await context.db
|
|
.insert(todoTable)
|
|
.values(input)
|
|
.returning()
|
|
|
|
if (!newTodo) {
|
|
throw new ORPCError('NOT_FOUND')
|
|
}
|
|
|
|
return newTodo
|
|
})
|
|
|
|
export const update = implement(todoContract.update)
|
|
.use(dbProvider)
|
|
.handler(async ({ context, input }) => {
|
|
const [updatedTodo] = await context.db
|
|
.update(todoTable)
|
|
.set(input.data)
|
|
.where(eq(todoTable.id, input.id))
|
|
.returning()
|
|
|
|
if (!updatedTodo) {
|
|
throw new ORPCError('NOT_FOUND')
|
|
}
|
|
|
|
return updatedTodo
|
|
})
|
|
|
|
export const remove = implement(todoContract.remove)
|
|
.use(dbProvider)
|
|
.handler(async ({ context, input }) => {
|
|
await context.db.delete(todoTable).where(eq(todoTable.id, input.id))
|
|
})
|