forked from imbytecat/fullstack-starter
rename
This commit is contained in:
29
apps/server/src/api/middlewares/db.ts
Normal file
29
apps/server/src/api/middlewares/db.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { os } from '@orpc/server'
|
||||
import { createDb } from '@/db/utils'
|
||||
|
||||
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,
|
||||
},
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user