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

+35 -34
View File
@@ -1,8 +1,9 @@
import { existsSync } from "node:fs";
import { asc, inArray, sql } from "drizzle-orm";
import { withAdmin } from "@/lib/api-handler";
import { apiOk } from "@/lib/api-response";
import { db, ItemsBase } from "@/lib/db";
import { PERMS } from "@/lib/permissions";
import { prisma } from "@/lib/prisma";
import {
appendFurniEntriesBatch,
buildFurniEntry,
@@ -64,19 +65,19 @@ export const POST = withAdmin(
for (let i = 0; i < targetIds.length; i += CHUNK) {
const slice = targetIds.slice(i, i + CHUNK);
const rows = await prisma.itemsBase.findMany({
where: { id: { in: slice } },
select: {
id: true,
spriteId: true,
itemName: true,
publicName: true,
type: true,
width: true,
length: true,
stackHeight: true,
},
});
const rows = await db
.select({
id: ItemsBase.id,
spriteId: ItemsBase.spriteId,
itemName: ItemsBase.itemName,
publicName: ItemsBase.publicName,
type: ItemsBase.type,
width: ItemsBase.width,
length: ItemsBase.length,
stackHeight: ItemsBase.stackHeight,
})
.from(ItemsBase)
.where(inArray(ItemsBase.id, slice));
const entries: Array<{
entry: Record<string, unknown>;
@@ -158,20 +159,20 @@ async function resolveTargetIds(
days: number,
): Promise<number[]> {
if (mode === "all") {
const rows = await prisma.itemsBase.findMany({
select: { id: true },
orderBy: { id: "asc" },
});
const rows = await db
.select({ id: ItemsBase.id })
.from(ItemsBase)
.orderBy(asc(ItemsBase.id));
return rows.map((r) => r.id);
}
if (mode === "missing" || mode === "broken") {
const furniDataPath = await getFurnitureDataPath();
if (!existsSync(furniDataPath)) {
const rows = await prisma.itemsBase.findMany({
select: { id: true },
orderBy: { id: "asc" },
});
const rows = await db
.select({ id: ItemsBase.id })
.from(ItemsBase)
.orderBy(asc(ItemsBase.id));
return rows.map((r) => r.id);
}
const furniData = (await readFurniData()) as {
@@ -184,16 +185,16 @@ async function resolveTargetIds(
if (typeof e?.classname === "string") byClassname.set(e.classname, e);
}
}
const rows = await prisma.itemsBase.findMany({
select: {
id: true,
itemName: true,
spriteId: true,
width: true,
length: true,
},
orderBy: { id: "asc" },
});
const rows = await db
.select({
id: ItemsBase.id,
itemName: ItemsBase.itemName,
spriteId: ItemsBase.spriteId,
width: ItemsBase.width,
length: ItemsBase.length,
})
.from(ItemsBase)
.orderBy(asc(ItemsBase.id));
if (mode === "missing") {
return rows.filter((r) => !byClassname.has(r.itemName)).map((r) => r.id);
@@ -220,7 +221,7 @@ async function resolveTargetIds(
Date.now() - days * 24 * 60 * 60 * 1000,
).toISOString();
const sinceMysql = sinceIso.slice(0, 19).replace("T", " ");
const auditRows = await prisma.$queryRaw<Array<{ target_id: number }>>`
const [auditRows] = (await db.execute(sql`
SELECT DISTINCT target_id
FROM admin_audit_log
WHERE action = 'furni_import'
@@ -228,6 +229,6 @@ async function resolveTargetIds(
AND target_id IS NOT NULL
AND created_at >= ${sinceMysql}
ORDER BY target_id ASC
`;
`)) as unknown as [Array<{ target_id: number }>, unknown];
return auditRows.map((r) => r.target_id).filter(Boolean);
}