forked from imbytecat/fullstack-starter
30 lines
488 B
TypeScript
30 lines
488 B
TypeScript
import { os } from '@orpc/server'
|
|
import { createDb } 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()
|
|
|
|
return next({
|
|
context: {
|
|
...context,
|
|
db,
|
|
},
|
|
})
|
|
})
|