refactor(db): migrate app pages and APIs from Prisma facade to Drizzle
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
53b350057d
commit
7c39aef5d9
40 files changed
+1124
-692
No files matched your search
@@ -1,8 +1,9 @@
|
||||
import { desc, eq, inArray } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { ContentCard, EmptyState, RankBadge } from "@/components/public/ui";
|
||||
import { db, User, UsersCurrency, UsersSettings } from "@/lib/db";
|
||||
import { avatarImageUrl } from "@/lib/format";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { siteSettings } from "@/lib/services/site-settings";
|
||||
|
||||
export const revalidate = 300;
|
||||
@@ -41,11 +42,15 @@ type Row = { username: string; look: string; value: number };
|
||||
|
||||
async function loadCreditsRows(): Promise<Row[]> {
|
||||
try {
|
||||
const users = await prisma.user.findMany({
|
||||
orderBy: { credits: "desc" },
|
||||
take: 20,
|
||||
select: { username: true, look: true, credits: true },
|
||||
});
|
||||
const users = await db
|
||||
.select({
|
||||
username: User.username,
|
||||
look: User.look,
|
||||
credits: User.credits,
|
||||
})
|
||||
.from(User)
|
||||
.orderBy(desc(User.credits))
|
||||
.limit(20);
|
||||
return users.map((u) => ({
|
||||
username: u.username,
|
||||
look: u.look,
|
||||
@@ -58,18 +63,26 @@ async function loadCreditsRows(): Promise<Row[]> {
|
||||
|
||||
async function loadCurrencyRows(type: number): Promise<Row[]> {
|
||||
try {
|
||||
const top = await prisma.usersCurrency.findMany({
|
||||
where: { type },
|
||||
orderBy: { amount: "desc" },
|
||||
take: 20,
|
||||
select: { userId: true, amount: true },
|
||||
});
|
||||
const top = await db
|
||||
.select({
|
||||
userId: UsersCurrency.userId,
|
||||
amount: UsersCurrency.amount,
|
||||
})
|
||||
.from(UsersCurrency)
|
||||
.where(eq(UsersCurrency.type, type))
|
||||
.orderBy(desc(UsersCurrency.amount))
|
||||
.limit(20);
|
||||
if (top.length === 0) return [];
|
||||
|
||||
const users = await prisma.user.findMany({
|
||||
where: { id: { in: top.map((t) => t.userId) } },
|
||||
select: { id: true, username: true, look: true },
|
||||
});
|
||||
const users = await db
|
||||
.select({ id: User.id, username: User.username, look: User.look })
|
||||
.from(User)
|
||||
.where(
|
||||
inArray(
|
||||
User.id,
|
||||
top.map((t) => t.userId),
|
||||
),
|
||||
);
|
||||
const byId = new Map(users.map((u) => [u.id, u]));
|
||||
|
||||
return top
|
||||
@@ -85,31 +98,39 @@ async function loadCurrencyRows(type: number): Promise<Row[]> {
|
||||
}
|
||||
|
||||
async function loadSettingsRows(
|
||||
field: "achievementScore" | "respectsReceived" | "onlineTime" | "hofPoints",
|
||||
field: "achievementScore" | "respectsReceived" | "onlineTime",
|
||||
): Promise<Row[]> {
|
||||
try {
|
||||
const top = await prisma.usersSettings.findMany({
|
||||
orderBy: { [field]: "desc" },
|
||||
take: 20,
|
||||
select: { userId: true, [field]: true },
|
||||
});
|
||||
const column = UsersSettings[field];
|
||||
const top = await db
|
||||
.select({
|
||||
userId: UsersSettings.userId,
|
||||
value: column,
|
||||
})
|
||||
.from(UsersSettings)
|
||||
.orderBy(desc(column))
|
||||
.limit(20);
|
||||
if (top.length === 0) return [];
|
||||
|
||||
const users = await prisma.user.findMany({
|
||||
where: { id: { in: top.map((t) => t.userId) } },
|
||||
select: { id: true, username: true, look: true },
|
||||
});
|
||||
const users = await db
|
||||
.select({ id: User.id, username: User.username, look: User.look })
|
||||
.from(User)
|
||||
.where(
|
||||
inArray(
|
||||
User.id,
|
||||
top.map((t) => t.userId),
|
||||
),
|
||||
);
|
||||
const byId = new Map(users.map((u) => [u.id, u]));
|
||||
|
||||
return (top as unknown as Array<{ userId: number; [k: string]: unknown }>)
|
||||
return top
|
||||
.map((t) => {
|
||||
const u = byId.get(t.userId as number);
|
||||
const u = byId.get(t.userId);
|
||||
if (!u) return null;
|
||||
// eslint-disable-next-line security/detect-object-injection -- field is union of known keys
|
||||
return {
|
||||
username: u.username,
|
||||
look: u.look,
|
||||
value: Number(t[field] ?? 0),
|
||||
value: Number(t.value ?? 0),
|
||||
};
|
||||
})
|
||||
.filter((r): r is Row => r !== null);
|
||||
@@ -134,10 +155,10 @@ export default async function LeaderboardPage({
|
||||
}) {
|
||||
const t = await getTranslations("pages.leaderboard");
|
||||
const { type } = await searchParams;
|
||||
const active: TabKey = TABS.some((t) => t.key === type)
|
||||
const active: TabKey = TABS.some((tab) => tab.key === type)
|
||||
? (type as TabKey)
|
||||
: "credits";
|
||||
const activeTab = TABS.find((t) => t.key === active) ?? TABS[0];
|
||||
const activeTab = TABS.find((tab) => tab.key === active) ?? TABS[0];
|
||||
|
||||
const [rows, imagerBase] = await Promise.all([
|
||||
active === "credits"
|
||||
|
||||
Reference in new issue
Block a user