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

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
SimoandCursor committed 2026-08-01 14:15:32 +02:00
1 parent 2cd0863cb8
commit 580972c0a0
40 files changed
+1089 -808

No files matched your search

+39 -27
View File
@@ -1,3 +1,4 @@
import { count, desc, eq, gt, inArray, or } from "drizzle-orm";
import {
Activity,
ArrowRight,
@@ -9,57 +10,68 @@ import {
import Link from "next/link";
import { getTranslations } from "next-intl/server";
import { StatusCard } from "@/components/admin/dashboard";
import { activeBanWhere } from "@/lib/bans";
import { unixNow } from "@/lib/bans";
import {
Ban as BanTable,
db,
StaffActivities,
User,
WebsiteArticles,
} from "@/lib/db";
import { formatDate } from "@/lib/format-date";
import { prisma } from "@/lib/prisma";
type RankCount = { rank: number; count: number };
export default async function AdminDashboard() {
const t = await getTranslations("pages.admin.dashboard");
const now = unixNow();
let users = -1;
let online = 0;
let articles = 0;
let bans = 0;
try {
[users, online, articles, bans] = await Promise.all([
prisma.user.count(),
prisma.user.count({ where: { online: "1" } }),
prisma.websiteArticles.count(),
prisma.ban.count({
where: activeBanWhere(),
}),
const [usersRow, onlineRow, articlesRow, bansRow] = await Promise.all([
db.select({ total: count() }).from(User),
db.select({ total: count() }).from(User).where(eq(User.online, "1")),
db.select({ total: count() }).from(WebsiteArticles),
db
.select({ total: count() })
.from(BanTable)
.where(or(eq(BanTable.banExpire, 0), gt(BanTable.banExpire, now))),
]);
users = usersRow[0]?.total ?? 0;
online = onlineRow[0]?.total ?? 0;
articles = articlesRow[0]?.total ?? 0;
bans = bansRow[0]?.total ?? 0;
} catch {}
const dbOk = users >= 0;
const rankGroups = await prisma.user
.groupBy({
by: ["rank"],
_count: { _all: true },
orderBy: { rank: "desc" },
})
.catch(() => [] as Array<{ rank: number; _count: { _all: number } }>);
const rankGroups = await db
.select({ rank: User.rank, total: count() })
.from(User)
.groupBy(User.rank)
.orderBy(desc(User.rank))
.catch(() => [] as Array<{ rank: number; total: number }>);
const rankCounts: RankCount[] = rankGroups
.map((g) => ({ rank: g.rank, count: g._count._all }))
.map((g) => ({ rank: g.rank, count: Number(g.total) }))
.sort((a, b) => b.count - a.count);
const maxRankCount = rankCounts.reduce((m, r) => Math.max(m, r.count), 0);
const activity = await prisma.staffActivities
.findMany({ orderBy: { createdAt: "desc" }, take: 8 })
.catch(
() => [] as Awaited<ReturnType<typeof prisma.staffActivities.findMany>>,
);
const activity = await db
.select()
.from(StaffActivities)
.orderBy(desc(StaffActivities.createdAt))
.limit(8)
.catch(() => [] as (typeof StaffActivities.$inferSelect)[]);
const staffIds = Array.from(
new Set(activity.map((a) => Number(a.userId))),
).filter((n) => n > 0);
let nameById = new Map<number, string>();
if (staffIds.length) {
const u = await prisma.user
.findMany({
where: { id: { in: staffIds } },
select: { id: true, username: true },
})
const u = await db
.select({ id: User.id, username: User.username })
.from(User)
.where(inArray(User.id, staffIds))
.catch(() => []);
nameById = new Map(u.map((x) => [x.id, x.username]));
}