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