refactor: 统一表命名规范,简化 DB 单例
- 去掉所有 Drizzle 表变量的 Table 后缀(userTable→user 等) - 修复 Better Auth adapter 找不到 schema model 的问题 - DB 实例从 IIFE 闭包工厂简化为模块级导出 - db middleware 重命名为 dbMiddleware 避免与 db 实例冲突 - 添加 babel-plugin-react-compiler 依赖
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import { ORPCError } from '@orpc/server'
|
||||
import { and, eq } from 'drizzle-orm'
|
||||
import { bookmarkTable, categoryTable } from '@/modules/bookmarks/schema'
|
||||
import { authMiddleware, db } from '@/server/api/middlewares'
|
||||
import * as schema from '@/modules/bookmarks/schema'
|
||||
import { authMiddleware, dbMiddleware } from '@/server/api/middlewares'
|
||||
import { os } from '@/server/api/server'
|
||||
|
||||
export const category = {
|
||||
list: os.bookmarks.category.list
|
||||
.use(db)
|
||||
.use(dbMiddleware)
|
||||
.use(authMiddleware)
|
||||
.handler(async ({ context }) => {
|
||||
return await context.db.query.categoryTable.findMany({
|
||||
return await context.db.query.category.findMany({
|
||||
where: { userId: context.user.id },
|
||||
orderBy: { orderId: 'asc' },
|
||||
with: {
|
||||
@@ -21,11 +21,11 @@ export const category = {
|
||||
}),
|
||||
|
||||
create: os.bookmarks.category.create
|
||||
.use(db)
|
||||
.use(dbMiddleware)
|
||||
.use(authMiddleware)
|
||||
.handler(async ({ context, input }) => {
|
||||
const [created] = await context.db
|
||||
.insert(categoryTable)
|
||||
.insert(schema.category)
|
||||
.values({ ...input, userId: context.user.id })
|
||||
.returning()
|
||||
if (!created) throw new ORPCError('INTERNAL_SERVER_ERROR', { message: 'Failed to create category' })
|
||||
@@ -33,39 +33,39 @@ export const category = {
|
||||
}),
|
||||
|
||||
update: os.bookmarks.category.update
|
||||
.use(db)
|
||||
.use(dbMiddleware)
|
||||
.use(authMiddleware)
|
||||
.handler(async ({ context, input }) => {
|
||||
const [updated] = await context.db
|
||||
.update(categoryTable)
|
||||
.update(schema.category)
|
||||
.set(input.data)
|
||||
.where(and(eq(categoryTable.id, input.id), eq(categoryTable.userId, context.user.id)))
|
||||
.where(and(eq(schema.category.id, input.id), eq(schema.category.userId, context.user.id)))
|
||||
.returning()
|
||||
if (!updated) throw new ORPCError('NOT_FOUND')
|
||||
return updated
|
||||
}),
|
||||
|
||||
remove: os.bookmarks.category.remove
|
||||
.use(db)
|
||||
.use(dbMiddleware)
|
||||
.use(authMiddleware)
|
||||
.handler(async ({ context, input }) => {
|
||||
const [deleted] = await context.db
|
||||
.delete(categoryTable)
|
||||
.where(and(eq(categoryTable.id, input.id), eq(categoryTable.userId, context.user.id)))
|
||||
.returning({ id: categoryTable.id })
|
||||
.delete(schema.category)
|
||||
.where(and(eq(schema.category.id, input.id), eq(schema.category.userId, context.user.id)))
|
||||
.returning({ id: schema.category.id })
|
||||
if (!deleted) throw new ORPCError('NOT_FOUND')
|
||||
}),
|
||||
|
||||
reorder: os.bookmarks.category.reorder
|
||||
.use(db)
|
||||
.use(dbMiddleware)
|
||||
.use(authMiddleware)
|
||||
.handler(async ({ context, input }) => {
|
||||
await context.db.transaction(async (tx) => {
|
||||
for (const item of input) {
|
||||
await tx
|
||||
.update(categoryTable)
|
||||
.update(schema.category)
|
||||
.set({ orderId: item.orderId })
|
||||
.where(and(eq(categoryTable.id, item.id), eq(categoryTable.userId, context.user.id)))
|
||||
.where(and(eq(schema.category.id, item.id), eq(schema.category.userId, context.user.id)))
|
||||
}
|
||||
})
|
||||
}),
|
||||
@@ -73,11 +73,11 @@ export const category = {
|
||||
|
||||
export const bookmark = {
|
||||
create: os.bookmarks.bookmark.create
|
||||
.use(db)
|
||||
.use(dbMiddleware)
|
||||
.use(authMiddleware)
|
||||
.handler(async ({ context, input }) => {
|
||||
const [created] = await context.db
|
||||
.insert(bookmarkTable)
|
||||
.insert(schema.bookmark)
|
||||
.values({ ...input, userId: context.user.id })
|
||||
.returning()
|
||||
if (!created) throw new ORPCError('INTERNAL_SERVER_ERROR', { message: 'Failed to create bookmark' })
|
||||
@@ -85,39 +85,39 @@ export const bookmark = {
|
||||
}),
|
||||
|
||||
update: os.bookmarks.bookmark.update
|
||||
.use(db)
|
||||
.use(dbMiddleware)
|
||||
.use(authMiddleware)
|
||||
.handler(async ({ context, input }) => {
|
||||
const [updated] = await context.db
|
||||
.update(bookmarkTable)
|
||||
.update(schema.bookmark)
|
||||
.set(input.data)
|
||||
.where(and(eq(bookmarkTable.id, input.id), eq(bookmarkTable.userId, context.user.id)))
|
||||
.where(and(eq(schema.bookmark.id, input.id), eq(schema.bookmark.userId, context.user.id)))
|
||||
.returning()
|
||||
if (!updated) throw new ORPCError('NOT_FOUND')
|
||||
return updated
|
||||
}),
|
||||
|
||||
remove: os.bookmarks.bookmark.remove
|
||||
.use(db)
|
||||
.use(dbMiddleware)
|
||||
.use(authMiddleware)
|
||||
.handler(async ({ context, input }) => {
|
||||
const [deleted] = await context.db
|
||||
.delete(bookmarkTable)
|
||||
.where(and(eq(bookmarkTable.id, input.id), eq(bookmarkTable.userId, context.user.id)))
|
||||
.returning({ id: bookmarkTable.id })
|
||||
.delete(schema.bookmark)
|
||||
.where(and(eq(schema.bookmark.id, input.id), eq(schema.bookmark.userId, context.user.id)))
|
||||
.returning({ id: schema.bookmark.id })
|
||||
if (!deleted) throw new ORPCError('NOT_FOUND')
|
||||
}),
|
||||
|
||||
reorder: os.bookmarks.bookmark.reorder
|
||||
.use(db)
|
||||
.use(dbMiddleware)
|
||||
.use(authMiddleware)
|
||||
.handler(async ({ context, input }) => {
|
||||
await context.db.transaction(async (tx) => {
|
||||
for (const item of input) {
|
||||
await tx
|
||||
.update(bookmarkTable)
|
||||
.update(schema.bookmark)
|
||||
.set({ orderId: item.orderId })
|
||||
.where(and(eq(bookmarkTable.id, item.id), eq(bookmarkTable.userId, context.user.id)))
|
||||
.where(and(eq(schema.bookmark.id, item.id), eq(schema.bookmark.userId, context.user.id)))
|
||||
}
|
||||
})
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user