refactor(db): migrate app pages and APIs from Prisma facade to Drizzle (4)
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
580972c0a0
commit
9aa4f331bf
40 files changed
+1259
-927
No files matched your search
@@ -1,9 +1,11 @@
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import type { ResultSetHeader } from "mysql2";
|
||||
import { revalidateTag } from "next/cache";
|
||||
import { NextResponse } from "next/server";
|
||||
import { withAdmin } from "@/lib/api-handler";
|
||||
import { apiError } from "@/lib/api-response";
|
||||
import { AclModelPermission, AclModelRole, AclRole, db } from "@/lib/db";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const POST = withAdmin(
|
||||
{ permission: PERMS.PERMISSIONS_MANAGE },
|
||||
@@ -15,7 +17,10 @@ export const POST = withAdmin(
|
||||
const title = String(body.title ?? "").trim();
|
||||
if (!/^[a-z0-9._-]{2,64}$/.test(slug) || !title)
|
||||
return apiError("Valid slug and title required", 400);
|
||||
const role = await prisma.aclRole.create({ data: { slug, title } });
|
||||
const [result] = (await db
|
||||
.insert(AclRole)
|
||||
.values({ slug, title })) as unknown as [ResultSetHeader];
|
||||
const role = { id: Number(result.insertId), slug, title };
|
||||
revalidateTag("permissions", { expire: 0 });
|
||||
return NextResponse.json({ role });
|
||||
},
|
||||
@@ -27,20 +32,26 @@ export const DELETE = withAdmin(
|
||||
const id = Number(request.nextUrl.searchParams.get("id"));
|
||||
if (!Number.isInteger(id) || id <= 0)
|
||||
return apiError("Valid ID required", 400);
|
||||
const role = await prisma.aclRole.findUnique({
|
||||
where: { id },
|
||||
select: { slug: true },
|
||||
});
|
||||
const [role] = await db
|
||||
.select({ slug: AclRole.slug })
|
||||
.from(AclRole)
|
||||
.where(eq(AclRole.id, id))
|
||||
.limit(1);
|
||||
if (!role) return apiError("Role not found", 404);
|
||||
if (role.slug.startsWith("rank_"))
|
||||
return apiError("Rank roles must be managed through emulator ranks", 409);
|
||||
await prisma.$transaction([
|
||||
prisma.aclModelPermission.deleteMany({
|
||||
where: { modelType: "Role", modelId: id },
|
||||
}),
|
||||
prisma.aclModelRole.deleteMany({ where: { roleId: id } }),
|
||||
prisma.aclRole.delete({ where: { id } }),
|
||||
]);
|
||||
await db.transaction(async (tx) => {
|
||||
await tx
|
||||
.delete(AclModelPermission)
|
||||
.where(
|
||||
and(
|
||||
eq(AclModelPermission.modelType, "Role"),
|
||||
eq(AclModelPermission.modelId, id),
|
||||
),
|
||||
);
|
||||
await tx.delete(AclModelRole).where(eq(AclModelRole.roleId, id));
|
||||
await tx.delete(AclRole).where(eq(AclRole.id, id));
|
||||
});
|
||||
revalidateTag("permissions", { expire: 0 });
|
||||
return NextResponse.json({ success: true });
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user