refactor(db): migrate app pages and APIs from Prisma facade to Drizzle (4)

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
SimoandCursor committed 2026-08-01 14:15:36 +02:00
1 parent 580972c0a0
commit 9aa4f331bf
40 files changed
+1259 -927

No files matched your search

+21 -21
View File
@@ -1,7 +1,8 @@
import { sql } from "drizzle-orm";
import { withAdmin } from "@/lib/api-handler";
import { apiError, apiOk } from "@/lib/api-response";
import { db } from "@/lib/db";
import { PERMS } from "@/lib/permissions";
import { Prisma, prisma } from "@/lib/prisma";
interface UserPrefix {
id: number;
@@ -24,23 +25,24 @@ export const GET = withAdmin(
const offset = (page - 1) * limit;
const whereFragment = q
? Prisma.sql`WHERE up.text LIKE ${`%${q}%`} OR u.username LIKE ${`%${q}%`}`
: Prisma.empty;
? sql`WHERE up.text LIKE ${`%${q}%`} OR u.username LIKE ${`%${q}%`}`
: sql``;
const prefixes = await prisma.$queryRaw<UserPrefix[]>(
Prisma.sql`SELECT up.id, up.user_id, up.text, up.color, up.icon, up.effect, up.active, u.username
const [prefixes] =
(await db.execute(sql`SELECT up.id, up.user_id, up.text, up.color, up.icon, up.effect, up.active, u.username
FROM custom_prefixes up
LEFT JOIN users u ON u.id = up.user_id
${whereFragment}
ORDER BY up.id DESC
LIMIT ${limit} OFFSET ${offset}`,
);
LIMIT ${limit} OFFSET ${offset}`)) as unknown as [
UserPrefix[],
unknown,
];
const countResult = await prisma.$queryRaw<[{ total: bigint }]>(
Prisma.sql`SELECT COUNT(*) as total FROM custom_prefixes up
const [countResult] =
(await db.execute(sql`SELECT COUNT(*) as total FROM custom_prefixes up
LEFT JOIN users u ON u.id = up.user_id
${whereFragment}`,
);
${whereFragment}`)) as unknown as [[{ total: bigint }], unknown];
const total = Number(countResult[0]?.total || 0);
@@ -69,9 +71,9 @@ export const POST = withAdmin(
return apiError("Missing required fields: username, text, color");
}
const users = await prisma.$queryRaw<{ id: number }[]>(
Prisma.sql`SELECT id FROM users WHERE username = ${username} LIMIT 1`,
);
const [users] = (await db.execute(
sql`SELECT id FROM users WHERE username = ${username} LIMIT 1`,
)) as unknown as [{ id: number }[], unknown];
if (!users || users.length === 0) {
return apiError("User not found");
@@ -79,8 +81,8 @@ export const POST = withAdmin(
const userId = users[0].id;
await prisma.$executeRaw(
Prisma.sql`INSERT INTO custom_prefixes (user_id, text, color, icon, effect, active)
await db.execute(
sql`INSERT INTO custom_prefixes (user_id, text, color, icon, effect, active)
VALUES (${userId}, ${text}, ${color}, ${icon || ""}, ${effect || ""}, ${active ? 1 : 0})`,
);
@@ -96,8 +98,8 @@ export const PUT = withAdmin(
if (!id) return apiError("Missing prefix id");
await prisma.$executeRaw(
Prisma.sql`UPDATE custom_prefixes
await db.execute(
sql`UPDATE custom_prefixes
SET text = ${text}, color = ${color}, icon = ${icon || ""}, effect = ${effect || ""}, active = ${active ? 1 : 0}
WHERE id = ${id}`,
);
@@ -114,9 +116,7 @@ export const DELETE = withAdmin(
if (!id) return apiError("Missing prefix id");
await prisma.$executeRaw(
Prisma.sql`DELETE FROM custom_prefixes WHERE id = ${id}`,
);
await db.execute(sql`DELETE FROM custom_prefixes WHERE id = ${id}`);
return apiOk({ deleted: id });
},