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,8 +1,9 @@
|
||||
import { desc, eq, inArray } from "drizzle-orm";
|
||||
import { apiJson } from "@/lib/api";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { db, User, UsersCurrency } from "@/lib/db";
|
||||
|
||||
// Public REST API — leaderboard. Mirrors src/app/leaderboard/page.tsx.
|
||||
// AtomCMS-faithful currency type ids (see prisma/schema.prisma UsersCurrency):
|
||||
// AtomCMS-faithful currency type ids (see UsersCurrency):
|
||||
// Credits = -1 (lives on users.credits), Duckets = 0, Diamonds = 5.
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
@@ -16,11 +17,15 @@ const CURRENCY_TYPE: Record<Exclude<LeaderboardType, "credits">, number> = {
|
||||
type Row = { rank: number; username: string; look: string; value: number };
|
||||
|
||||
async function loadCreditsRows(): Promise<Row[]> {
|
||||
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, i) => ({
|
||||
rank: i + 1,
|
||||
username: u.username,
|
||||
@@ -30,18 +35,26 @@ async function loadCreditsRows(): Promise<Row[]> {
|
||||
}
|
||||
|
||||
async function loadCurrencyRows(type: number): Promise<Row[]> {
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user