refactor: 重构待办事项模块,统一路由与数据操作逻辑

- 将客户端导出改为私有常量,避免外部直接访问。
- 添加待办事项的完整 CRUD 操作,包括列表查询、创建、更新和删除功能,并使用 Zod 进行输入输出验证和 Drizzle ORM 操作数据库。
- 导出客户端和路由器模块的公共接口
- 添加路由配置,将 todo 处理程序注册到路由系统中。
- 删除已废弃的路由定义文件
- 删除待办事项相关路由和接口定义
- 删除未使用的 TodoSchema 模式定义以清理代码库。
- 将 Todo 路由重命名为复数形式并迁移数据获取与操作逻辑至 Orpc 客户端调用
- 将路由名称和路径从 `/todo` 更新为 `/todos`,并同步更新相关类型定义和引用。
This commit is contained in:
2026-01-18 01:42:53 +08:00
parent e0369d3271
commit f2481c31c7
9 changed files with 114 additions and 132 deletions

View File

@@ -22,6 +22,6 @@ const getORPCClient = createIsomorphicFn()
return createORPCClient(link)
})
export const client: RouterClient<typeof router> = getORPCClient()
const client: RouterClient<typeof router> = getORPCClient()
export const orpc = createTanstackQueryUtils(client)

83
src/orpc/handlers/todo.ts Normal file
View File

@@ -0,0 +1,83 @@
import { ORPCError, os } from '@orpc/server'
import { eq } from 'drizzle-orm'
import {
createInsertSchema,
createSelectSchema,
createUpdateSchema,
} from 'drizzle-zod'
import { z } from 'zod'
import { db } from '@/db'
import { todoTable } from '@/db/schema'
const selectSchema = createSelectSchema(todoTable)
const insertSchema = createInsertSchema(todoTable).omit({
id: true,
createdAt: true,
updatedAt: true,
})
const updateSchema = createUpdateSchema(todoTable).omit({
id: true,
createdAt: true,
updatedAt: true,
})
export const list = os
.input(z.void())
.output(z.array(selectSchema))
.handler(async () => {
const todos = await db.query.todoTable.findMany({
orderBy: (todos, { desc }) => [desc(todos.createdAt)],
})
return todos
})
export const create = os
.input(insertSchema)
.output(selectSchema)
.handler(async ({ input }) => {
const [newTodo] = await db
.insert(todoTable)
.values({ title: input.title })
.returning()
if (!newTodo) {
throw new ORPCError('NOT_FOUND')
}
return newTodo
})
export const update = os
.input(
z.object({
id: z.uuid(),
data: updateSchema,
}),
)
.output(selectSchema)
.handler(async ({ input }) => {
const [updatedTodo] = await 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 = os
.input(
z.object({
id: z.uuid(),
}),
)
.output(z.void())
.handler(async ({ input }) => {
await db.delete(todoTable).where(eq(todoTable.id, input.id))
})

2
src/orpc/index.ts Normal file
View File

@@ -0,0 +1,2 @@
export * from './client'
export * from './router'

5
src/orpc/router.ts Normal file
View File

@@ -0,0 +1,5 @@
import * as todo from './handlers/todo'
export const router = {
todo,
}

View File

@@ -1,6 +0,0 @@
import { addTodo, listTodos } from './todos'
export const router = {
listTodos,
addTodo,
}

View File

@@ -1,20 +0,0 @@
import { os } from '@orpc/server'
import { z } from 'zod'
const todos = [
{ id: 1, name: 'Get groceries' },
{ id: 2, name: 'Buy a new phone' },
{ id: 3, name: 'Finish the project' },
]
export const listTodos = os.input(z.object({})).handler(() => {
return todos
})
export const addTodo = os
.input(z.object({ name: z.string() }))
.handler(({ input }) => {
const newTodo = { id: todos.length + 1, name: input.name }
todos.push(newTodo)
return newTodo
})

View File

@@ -1,6 +0,0 @@
import { z } from 'zod'
export const TodoSchema = z.object({
id: z.number().int().min(1),
name: z.string(),
})

View File

@@ -9,13 +9,13 @@
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.
import { Route as rootRouteImport } from './routes/__root'
import { Route as TodoRouteImport } from './routes/todo'
import { Route as TodosRouteImport } from './routes/todos'
import { Route as IndexRouteImport } from './routes/index'
import { Route as ApiRpcSplatRouteImport } from './routes/api/rpc.$'
const TodoRoute = TodoRouteImport.update({
id: '/todo',
path: '/todo',
const TodosRoute = TodosRouteImport.update({
id: '/todos',
path: '/todos',
getParentRoute: () => rootRouteImport,
} as any)
const IndexRoute = IndexRouteImport.update({
@@ -31,41 +31,41 @@ const ApiRpcSplatRoute = ApiRpcSplatRouteImport.update({
export interface FileRoutesByFullPath {
'/': typeof IndexRoute
'/todo': typeof TodoRoute
'/todos': typeof TodosRoute
'/api/rpc/$': typeof ApiRpcSplatRoute
}
export interface FileRoutesByTo {
'/': typeof IndexRoute
'/todo': typeof TodoRoute
'/todos': typeof TodosRoute
'/api/rpc/$': typeof ApiRpcSplatRoute
}
export interface FileRoutesById {
__root__: typeof rootRouteImport
'/': typeof IndexRoute
'/todo': typeof TodoRoute
'/todos': typeof TodosRoute
'/api/rpc/$': typeof ApiRpcSplatRoute
}
export interface FileRouteTypes {
fileRoutesByFullPath: FileRoutesByFullPath
fullPaths: '/' | '/todo' | '/api/rpc/$'
fullPaths: '/' | '/todos' | '/api/rpc/$'
fileRoutesByTo: FileRoutesByTo
to: '/' | '/todo' | '/api/rpc/$'
id: '__root__' | '/' | '/todo' | '/api/rpc/$'
to: '/' | '/todos' | '/api/rpc/$'
id: '__root__' | '/' | '/todos' | '/api/rpc/$'
fileRoutesById: FileRoutesById
}
export interface RootRouteChildren {
IndexRoute: typeof IndexRoute
TodoRoute: typeof TodoRoute
TodosRoute: typeof TodosRoute
ApiRpcSplatRoute: typeof ApiRpcSplatRoute
}
declare module '@tanstack/react-router' {
interface FileRoutesByPath {
'/todo': {
id: '/todo'
path: '/todo'
fullPath: '/todo'
preLoaderRoute: typeof TodoRouteImport
'/todos': {
id: '/todos'
path: '/todos'
fullPath: '/todos'
preLoaderRoute: typeof TodosRouteImport
parentRoute: typeof rootRouteImport
}
'/': {
@@ -87,7 +87,7 @@ declare module '@tanstack/react-router' {
const rootRouteChildren: RootRouteChildren = {
IndexRoute: IndexRoute,
TodoRoute: TodoRoute,
TodosRoute: TodosRoute,
ApiRpcSplatRoute: ApiRpcSplatRoute,
}
export const routeTree = rootRouteImport

View File

@@ -1,97 +1,21 @@
import { useMutation, useSuspenseQuery } from '@tanstack/react-query'
import { createFileRoute } from '@tanstack/react-router'
import { createServerFn } from '@tanstack/react-start'
import { eq } from 'drizzle-orm'
import type { ChangeEvent, FormEvent } from 'react'
import { useState } from 'react'
import { z } from 'zod'
import { db } from '@/db'
import { todoTable } from '@/db/schema'
import { orpc } from '@/orpc'
const createTodoSchema = z.object({
title: z.string().min(1, '标题不能为空'),
})
const updateTodoSchema = z.object({
id: z.uuid(),
completed: z.boolean(),
})
const deleteTodoSchema = z.object({
id: z.uuid(),
})
const getTodos = createServerFn({ method: 'GET' }).handler(async () => {
const todos = await db.query.todoTable.findMany({
orderBy: (todos, { desc }) => [desc(todos.createdAt)],
})
return todos
})
const createTodo = createServerFn({ method: 'POST' })
.inputValidator(createTodoSchema)
.handler(async ({ data }) => {
const [newTodo] = await db
.insert(todoTable)
.values({ title: data.title })
.returning()
return newTodo
})
const updateTodo = createServerFn({ method: 'POST' })
.inputValidator(updateTodoSchema)
.handler(async ({ data }) => {
const [updatedTodo] = await db
.update(todoTable)
.set({ completed: data.completed })
.where(eq(todoTable.id, data.id))
.returning()
return updatedTodo
})
const deleteTodo = createServerFn({ method: 'POST' })
.inputValidator(deleteTodoSchema)
.handler(async ({ data }) => {
await db.delete(todoTable).where(eq(todoTable.id, data.id))
return { success: true }
})
export const Route = createFileRoute('/todo')({
export const Route = createFileRoute('/todos')({
component: Todo,
})
function Todo() {
const [newTodoTitle, setNewTodoTitle] = useState('')
const { data: todos, refetch } = useSuspenseQuery({
queryKey: ['todos'],
queryFn: () => getTodos(),
})
// Mutations
const createMutation = useMutation({
mutationFn: (title: string) => createTodo({ data: { title } }),
onSuccess: () => {
setNewTodoTitle('')
refetch()
},
})
const listQuery = useSuspenseQuery(orpc.todo.list.queryOptions())
const createMutation = useMutation(orpc.todo.create.mutationOptions())
const updateMutation = useMutation(orpc.todo.update.mutationOptions())
const deleteMutation = useMutation(orpc.todo.remove.mutationOptions())
const updateMutation = useMutation({
mutationFn: ({ id, completed }: { id: string; completed: boolean }) =>
updateTodo({ data: { id, completed } }),
onSuccess: () => {
refetch()
},
})
const deleteMutation = useMutation({
mutationFn: (id: string) => deleteTodo({ data: { id } }),
onSuccess: () => {
refetch()
},
})
// Handlers
const handleCreateTodo = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault()
if (newTodoTitle.trim()) {