refactor: 优化数据库连接管理与统一接入方式

- 优化数据库连接管理,直接使用获取数据库实例的函数并传入是否为无服务器环境的标识。
- 统一使用db中间件替代dbProvider,简化数据库连接处理并保持代码一致性。
- 添加数据库实例的单例获取机制并定义类型接口
This commit is contained in:
2026-01-22 15:56:00 +08:00
parent 70252fbd94
commit af807eeb53
3 changed files with 27 additions and 30 deletions

View File

@@ -1,29 +1,13 @@
import { os } from '@orpc/server' import { os } from '@orpc/server'
import { createDb } from '@/db' import { getDb } from '@/db'
const IS_SERVERLESS = false // TODO: 这里需要优化 const IS_SERVERLESS = false // TODO: 这里需要优化
let globalDb: ReturnType<typeof createDb> | null = null export const db = os.middleware(async ({ context, next }) => {
function getDb() {
if (IS_SERVERLESS) {
return createDb()
}
if (!globalDb) {
globalDb = createDb()
}
return globalDb
}
export const dbProvider = os.middleware(async ({ context, next }) => {
const db = getDb()
return next({ return next({
context: { context: {
...context, ...context,
db, db: getDb(IS_SERVERLESS),
}, },
}) })
}) })

View File

@@ -1,12 +1,10 @@
import { ORPCError } from '@orpc/server' import { ORPCError } from '@orpc/server'
import { eq } from 'drizzle-orm' import { eq } from 'drizzle-orm'
import { dbProvider } from '@/api/middlewares' import { db } from '@/api/middlewares'
import { os } from '@/api/server' import { os } from '@/api/server'
import { todoTable } from '@/db/schema' import { todoTable } from '@/db/schema'
export const list = os.todo.list export const list = os.todo.list.use(db).handler(async ({ context }) => {
.use(dbProvider)
.handler(async ({ context }) => {
const todos = await context.db.query.todoTable.findMany({ const todos = await context.db.query.todoTable.findMany({
orderBy: (todos, { desc }) => [desc(todos.createdAt)], orderBy: (todos, { desc }) => [desc(todos.createdAt)],
}) })
@@ -14,7 +12,7 @@ export const list = os.todo.list
}) })
export const create = os.todo.create export const create = os.todo.create
.use(dbProvider) .use(db)
.handler(async ({ context, input }) => { .handler(async ({ context, input }) => {
const [newTodo] = await context.db const [newTodo] = await context.db
.insert(todoTable) .insert(todoTable)
@@ -29,7 +27,7 @@ export const create = os.todo.create
}) })
export const update = os.todo.update export const update = os.todo.update
.use(dbProvider) .use(db)
.handler(async ({ context, input }) => { .handler(async ({ context, input }) => {
const [updatedTodo] = await context.db const [updatedTodo] = await context.db
.update(todoTable) .update(todoTable)
@@ -45,7 +43,7 @@ export const update = os.todo.update
}) })
export const remove = os.todo.remove export const remove = os.todo.remove
.use(dbProvider) .use(db)
.handler(async ({ context, input }) => { .handler(async ({ context, input }) => {
await context.db.delete(todoTable).where(eq(todoTable.id, input.id)) await context.db.delete(todoTable).where(eq(todoTable.id, input.id))
}) })

View File

@@ -11,3 +11,18 @@ export function createDb() {
schema, schema,
}) })
} }
export type Db = ReturnType<typeof createDb>
export const getDb = (() => {
let db: Db | null = null
return (serverless: boolean = false) => {
if (serverless) {
return createDb()
}
db ??= createDb()
return db
}
})()