refactor(orpc): simplify architecture following KISS principle

- Fix NotFount.tsx typo -> NotFound.tsx
- Restructure ORPC: server -> middlewares -> procedures -> handlers
- Remove std-env dependency and over-engineered ephemeral detection
- Add procedures.ts as middleware composition layer
- Use globalThis for DB singleton (survives HMR)
- Preserve context in middleware (fix context merge bug)
- Remove unused ORPCContextWithDb type
This commit is contained in:
2026-02-07 03:13:29 +08:00
parent 5cc476cedc
commit f7f86e4462
9 changed files with 138 additions and 164 deletions

View File

@@ -1,21 +1,17 @@
import { ORPCError } from '@orpc/server'
import { eq } from 'drizzle-orm'
import { todoTable } from '@/db/schema'
import { dbProvider } from '@/orpc/middlewares'
import { os } from '@/orpc/server'
import { baseProcedure } from '@/orpc/procedures'
export const list = os.todo.list
.use(dbProvider)
.handler(async ({ context }) => {
const todos = await context.db.query.todoTable.findMany({
orderBy: (todos, { desc }) => [desc(todos.createdAt)],
})
return todos
export const list = baseProcedure.todo.list.handler(async ({ context }) => {
const todos = await context.db.query.todoTable.findMany({
orderBy: (todos, { desc }) => [desc(todos.createdAt)],
})
return todos
})
export const create = os.todo.create
.use(dbProvider)
.handler(async ({ context, input }) => {
export const create = baseProcedure.todo.create.handler(
async ({ context, input }) => {
const [newTodo] = await context.db
.insert(todoTable)
.values(input)
@@ -26,11 +22,11 @@ export const create = os.todo.create
}
return newTodo
})
},
)
export const update = os.todo.update
.use(dbProvider)
.handler(async ({ context, input }) => {
export const update = baseProcedure.todo.update.handler(
async ({ context, input }) => {
const [updatedTodo] = await context.db
.update(todoTable)
.set(input.data)
@@ -42,10 +38,11 @@ export const update = os.todo.update
}
return updatedTodo
})
},
)
export const remove = os.todo.remove
.use(dbProvider)
.handler(async ({ context, input }) => {
export const remove = baseProcedure.todo.remove.handler(
async ({ context, input }) => {
await context.db.delete(todoTable).where(eq(todoTable.id, input.id))
})
},
)