forked from imbytecat/fullstack-starter
refactor: 重构数据库实例管理,支持按需创建与上下文注入
- 移除默认导出的 db 实例,改为按需创建数据库实例 - 使用数据库中间件提供上下文中的数据库实例,统一通过上下文获取数据库连接并执行操作。 - 添加数据库中间件以在请求上下文中注入数据库实例,支持服务器端全局缓存和无服务器环境下的每次新建实例。 - 导出中间件模块中的数据库相关功能。
This commit is contained in:
@@ -11,5 +11,3 @@ export function createDb() {
|
|||||||
schema,
|
schema,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const db = createDb()
|
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import {
|
|||||||
createUpdateSchema,
|
createUpdateSchema,
|
||||||
} from 'drizzle-zod'
|
} from 'drizzle-zod'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { db } from '@/db'
|
|
||||||
import { todoTable } from '@/db/schema'
|
import { todoTable } from '@/db/schema'
|
||||||
|
import { dbProvider } from '../middlewares'
|
||||||
|
|
||||||
const selectSchema = createSelectSchema(todoTable)
|
const selectSchema = createSelectSchema(todoTable)
|
||||||
|
|
||||||
@@ -26,8 +26,9 @@ const updateSchema = createUpdateSchema(todoTable).omit({
|
|||||||
export const list = os
|
export const list = os
|
||||||
.input(z.void())
|
.input(z.void())
|
||||||
.output(z.array(selectSchema))
|
.output(z.array(selectSchema))
|
||||||
.handler(async () => {
|
.use(dbProvider)
|
||||||
const todos = await db.query.todoTable.findMany({
|
.handler(async ({ context }) => {
|
||||||
|
const todos = await context.db.query.todoTable.findMany({
|
||||||
orderBy: (todos, { desc }) => [desc(todos.createdAt)],
|
orderBy: (todos, { desc }) => [desc(todos.createdAt)],
|
||||||
})
|
})
|
||||||
return todos
|
return todos
|
||||||
@@ -36,10 +37,11 @@ export const list = os
|
|||||||
export const create = os
|
export const create = os
|
||||||
.input(insertSchema)
|
.input(insertSchema)
|
||||||
.output(selectSchema)
|
.output(selectSchema)
|
||||||
.handler(async ({ input }) => {
|
.use(dbProvider)
|
||||||
const [newTodo] = await db
|
.handler(async ({ context, input }) => {
|
||||||
|
const [newTodo] = await context.db
|
||||||
.insert(todoTable)
|
.insert(todoTable)
|
||||||
.values({ title: input.title })
|
.values(input)
|
||||||
.returning()
|
.returning()
|
||||||
|
|
||||||
if (!newTodo) {
|
if (!newTodo) {
|
||||||
@@ -57,8 +59,9 @@ export const update = os
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.output(selectSchema)
|
.output(selectSchema)
|
||||||
.handler(async ({ input }) => {
|
.use(dbProvider)
|
||||||
const [updatedTodo] = await db
|
.handler(async ({ context, input }) => {
|
||||||
|
const [updatedTodo] = await context.db
|
||||||
.update(todoTable)
|
.update(todoTable)
|
||||||
.set(input.data)
|
.set(input.data)
|
||||||
.where(eq(todoTable.id, input.id))
|
.where(eq(todoTable.id, input.id))
|
||||||
@@ -78,6 +81,7 @@ export const remove = os
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.output(z.void())
|
.output(z.void())
|
||||||
.handler(async ({ input }) => {
|
.use(dbProvider)
|
||||||
await db.delete(todoTable).where(eq(todoTable.id, input.id))
|
.handler(async ({ context, input }) => {
|
||||||
|
await context.db.delete(todoTable).where(eq(todoTable.id, input.id))
|
||||||
})
|
})
|
||||||
|
|||||||
29
src/orpc/middlewares/db.ts
Normal file
29
src/orpc/middlewares/db.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
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,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
1
src/orpc/middlewares/index.ts
Normal file
1
src/orpc/middlewares/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from './db'
|
||||||
Reference in New Issue
Block a user