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,20 +1,21 @@
|
||||
import { and, asc, count, like, or, type SQL } from "drizzle-orm";
|
||||
import { ExternalLink } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { EmulatorSettingsForm } from "@/components/admin/emulator-settings-form";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { calcPagination } from "@/lib/admin-helpers";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { db, EmulatorSettings } from "@/lib/db";
|
||||
|
||||
const TRANSLATION_KEY_PATTERNS = [
|
||||
"commands.description.%",
|
||||
"commands.keys.%",
|
||||
"commands.cmd_%",
|
||||
"wordfilter.%",
|
||||
"pet.%message%",
|
||||
"matchmaking.%message%",
|
||||
"message.%",
|
||||
"notification.%",
|
||||
"commands.description.",
|
||||
"commands.keys.",
|
||||
"commands.cmd_",
|
||||
"wordfilter.",
|
||||
"pet.",
|
||||
"matchmaking.",
|
||||
"message.",
|
||||
"notification.",
|
||||
] as const;
|
||||
|
||||
export default async function EmulatorTranslationsPage({
|
||||
@@ -29,35 +30,39 @@ export default async function EmulatorTranslationsPage({
|
||||
const perPage = Math.min(Math.max(parseInt(sp.perPage || "50", 10), 10), 200);
|
||||
const page = Math.max(parseInt(sp.page || "1", 10), 1);
|
||||
|
||||
const patternFilters: any[] = TRANSLATION_KEY_PATTERNS.map((p) => ({
|
||||
key: { contains: p.replace(/%/g, "") },
|
||||
}));
|
||||
const patternFilters = or(
|
||||
...TRANSLATION_KEY_PATTERNS.map((p) => like(EmulatorSettings.key, `${p}%`)),
|
||||
)!;
|
||||
|
||||
const baseFilter: any = { OR: patternFilters };
|
||||
|
||||
const conditions: any[] = [baseFilter];
|
||||
if (group) conditions.push({ key: { startsWith: `${group}.` } });
|
||||
const conditions: SQL[] = [patternFilters];
|
||||
if (group) conditions.push(like(EmulatorSettings.key, `${group}.%`));
|
||||
if (search) {
|
||||
conditions.push({
|
||||
OR: [{ key: { contains: search } }, { value: { contains: search } }],
|
||||
});
|
||||
conditions.push(
|
||||
or(
|
||||
like(EmulatorSettings.key, `%${search}%`),
|
||||
like(EmulatorSettings.value, `%${search}%`),
|
||||
)!,
|
||||
);
|
||||
}
|
||||
const where: any = { AND: conditions };
|
||||
const where = and(...conditions);
|
||||
|
||||
const [settings, total, allKeys] = await Promise.all([
|
||||
prisma.emulatorSettings.findMany({
|
||||
where,
|
||||
orderBy: { key: "asc" },
|
||||
skip: (page - 1) * perPage,
|
||||
take: perPage,
|
||||
}),
|
||||
prisma.emulatorSettings.count({ where }),
|
||||
prisma.emulatorSettings.findMany({
|
||||
where: baseFilter,
|
||||
select: { key: true },
|
||||
}),
|
||||
const [settings, totalRows, allKeys] = await Promise.all([
|
||||
db
|
||||
.select()
|
||||
.from(EmulatorSettings)
|
||||
.where(where)
|
||||
.orderBy(asc(EmulatorSettings.key))
|
||||
.limit(perPage)
|
||||
.offset((page - 1) * perPage),
|
||||
db.select({ total: count() }).from(EmulatorSettings).where(where),
|
||||
db
|
||||
.select({ key: EmulatorSettings.key })
|
||||
.from(EmulatorSettings)
|
||||
.where(patternFilters),
|
||||
]);
|
||||
|
||||
const total = Number(totalRows[0]?.total ?? 0);
|
||||
|
||||
const groupCounts = new Map<string, number>();
|
||||
for (const { key } of allKeys) {
|
||||
const g = key.includes(".") ? key.split(".")[0] : "general";
|
||||
|
||||
Reference in new issue
Block a user