refactor(db): migrate app pages and APIs from Prisma facade to Drizzle (3)
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
2cd0863cb8
commit
580972c0a0
40 files changed
+1089
-808
No files matched your search
@@ -1,11 +1,12 @@
|
||||
import { asc, inArray, sql } from "drizzle-orm";
|
||||
import { redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { db, NavigatorFlatcats, NavigatorPubliccats, Rooms } from "@/lib/db";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
// navigator_flatcats — emulator-owned flat (private-room) categories shown in the
|
||||
// navigator. The Prisma delegate exists because the table has a primary key.
|
||||
// navigator.
|
||||
type Flatcat = {
|
||||
id: number;
|
||||
caption: string;
|
||||
@@ -18,16 +19,13 @@ type Flatcat = {
|
||||
orderNum: number;
|
||||
};
|
||||
|
||||
// navigator_publics — emulator-owned public-room assignments. This table has no
|
||||
// primary/unique key, so Prisma marks the model `@@ignore` and generates NO
|
||||
// delegate (prisma.navigatorPublics does not exist). It must be read via raw SQL.
|
||||
// navigator_publics — keyless table, read via raw SQL.
|
||||
type PublicRow = {
|
||||
public_cat_id: number;
|
||||
room_id: number;
|
||||
visible: string;
|
||||
};
|
||||
|
||||
// Reusable yes/no pill so emulator boolean-ish "1"/"0" columns read consistently.
|
||||
function YesNo({ on }: { on: boolean }) {
|
||||
return (
|
||||
<span
|
||||
@@ -47,48 +45,46 @@ export default async function AdminNavigation() {
|
||||
const t = await getTranslations("pages.admin.navigation");
|
||||
let flatcats: Flatcat[] = [];
|
||||
try {
|
||||
flatcats = await prisma.navigatorFlatcats.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
caption: true,
|
||||
captionSave: true,
|
||||
minRank: true,
|
||||
maxUserCount: true,
|
||||
canTrade: true,
|
||||
public: true,
|
||||
listType: true,
|
||||
orderNum: true,
|
||||
},
|
||||
orderBy: [{ orderNum: "asc" }, { id: "asc" }],
|
||||
});
|
||||
flatcats = await db
|
||||
.select({
|
||||
id: NavigatorFlatcats.id,
|
||||
caption: NavigatorFlatcats.caption,
|
||||
captionSave: NavigatorFlatcats.captionSave,
|
||||
minRank: NavigatorFlatcats.minRank,
|
||||
maxUserCount: NavigatorFlatcats.maxUserCount,
|
||||
canTrade: NavigatorFlatcats.canTrade,
|
||||
public: NavigatorFlatcats.public,
|
||||
listType: NavigatorFlatcats.listType,
|
||||
orderNum: NavigatorFlatcats.orderNum,
|
||||
})
|
||||
.from(NavigatorFlatcats)
|
||||
.orderBy(asc(NavigatorFlatcats.orderNum), asc(NavigatorFlatcats.id));
|
||||
} catch {
|
||||
flatcats = [];
|
||||
}
|
||||
|
||||
// Raw read because navigator_publics is an `@@ignore`d (keyless) model.
|
||||
let publics: PublicRow[] = [];
|
||||
try {
|
||||
publics = await prisma.$queryRaw<PublicRow[]>`
|
||||
const [rows] = (await db.execute(sql`
|
||||
SELECT public_cat_id, room_id, visible
|
||||
FROM navigator_publics
|
||||
ORDER BY public_cat_id ASC, room_id ASC
|
||||
`;
|
||||
`)) as unknown as [PublicRow[], unknown];
|
||||
publics = rows ?? [];
|
||||
} catch {
|
||||
publics = [];
|
||||
}
|
||||
|
||||
// Resolve the public-category names and room names referenced by the rows above
|
||||
// so the read-only view is legible. Each lookup degrades to an empty map on error.
|
||||
const catIds = Array.from(new Set(publics.map((p) => p.public_cat_id)));
|
||||
const roomIds = Array.from(new Set(publics.map((p) => p.room_id)));
|
||||
|
||||
let catNameById = new Map<number, string>();
|
||||
if (catIds.length) {
|
||||
try {
|
||||
const cats = await prisma.navigatorPubliccats.findMany({
|
||||
where: { id: { in: catIds } },
|
||||
select: { id: true, name: true },
|
||||
});
|
||||
const cats = await db
|
||||
.select({ id: NavigatorPubliccats.id, name: NavigatorPubliccats.name })
|
||||
.from(NavigatorPubliccats)
|
||||
.where(inArray(NavigatorPubliccats.id, catIds));
|
||||
catNameById = new Map(cats.map((c) => [c.id, c.name]));
|
||||
} catch {
|
||||
catNameById = new Map();
|
||||
@@ -98,10 +94,10 @@ export default async function AdminNavigation() {
|
||||
let roomNameById = new Map<number, string>();
|
||||
if (roomIds.length) {
|
||||
try {
|
||||
const rooms = await prisma.rooms.findMany({
|
||||
where: { id: { in: roomIds } },
|
||||
select: { id: true, name: true },
|
||||
});
|
||||
const rooms = await db
|
||||
.select({ id: Rooms.id, name: Rooms.name })
|
||||
.from(Rooms)
|
||||
.where(inArray(Rooms.id, roomIds));
|
||||
roomNameById = new Map(rooms.map((r) => [r.id, r.name]));
|
||||
} catch {
|
||||
roomNameById = new Map();
|
||||
@@ -134,7 +130,6 @@ export default async function AdminNavigation() {
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* ── navigator_flatcats ───────────────────────────────── */}
|
||||
<section className="mt-6">
|
||||
<h2 className="admin-section-title">{t("flatCategories")}</h2>
|
||||
<p className="text-xs theme-text-muted dark:theme-text-muted">
|
||||
@@ -186,7 +181,6 @@ export default async function AdminNavigation() {
|
||||
)}
|
||||
</section>
|
||||
|
||||
{/* ── navigator_publics ────────────────────────────────── */}
|
||||
<section className="mt-6">
|
||||
<h2 className="admin-section-title">{t("publicRooms")}</h2>
|
||||
<p className="text-xs theme-text-muted dark:theme-text-muted">
|
||||
|
||||
Reference in new issue
Block a user