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,10 +1,11 @@
|
||||
import { and, desc, eq, inArray } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { notFound } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import type { CSSProperties } from "react";
|
||||
import { ContentCard, EmptyState } from "@/components/public/ui";
|
||||
import { db, Guilds, GuildsForumsThreads, User } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const revalidate = 300;
|
||||
|
||||
@@ -53,10 +54,16 @@ export default async function GuildForumPage({
|
||||
// Guild header (read-only). A DB hiccup degrades to notFound rather than 500.
|
||||
let guild: { id: number; name: string; badge: string } | null = null;
|
||||
try {
|
||||
guild = await prisma.guilds.findUnique({
|
||||
where: { id: guildId },
|
||||
select: { id: true, name: true, badge: true },
|
||||
});
|
||||
const [row] = await db
|
||||
.select({
|
||||
id: Guilds.id,
|
||||
name: Guilds.name,
|
||||
badge: Guilds.badge,
|
||||
})
|
||||
.from(Guilds)
|
||||
.where(eq(Guilds.id, guildId))
|
||||
.limit(1);
|
||||
guild = row ?? null;
|
||||
} catch {
|
||||
guild = null;
|
||||
}
|
||||
@@ -66,21 +73,30 @@ export default async function GuildForumPage({
|
||||
// Threads for this guild. Pinned first, then most-recently active.
|
||||
let threads: ThreadRow[] = [];
|
||||
try {
|
||||
threads = await prisma.guildsForumsThreads.findMany({
|
||||
where: { guildId: guild.id, state: 0 },
|
||||
orderBy: [{ pinned: "desc" }, { updatedAt: "desc" }, { id: "desc" }],
|
||||
take: 100,
|
||||
select: {
|
||||
id: true,
|
||||
openerId: true,
|
||||
subject: true,
|
||||
postsCount: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
pinned: true,
|
||||
locked: true,
|
||||
},
|
||||
});
|
||||
threads = await db
|
||||
.select({
|
||||
id: GuildsForumsThreads.id,
|
||||
openerId: GuildsForumsThreads.openerId,
|
||||
subject: GuildsForumsThreads.subject,
|
||||
postsCount: GuildsForumsThreads.postsCount,
|
||||
createdAt: GuildsForumsThreads.createdAt,
|
||||
updatedAt: GuildsForumsThreads.updatedAt,
|
||||
pinned: GuildsForumsThreads.pinned,
|
||||
locked: GuildsForumsThreads.locked,
|
||||
})
|
||||
.from(GuildsForumsThreads)
|
||||
.where(
|
||||
and(
|
||||
eq(GuildsForumsThreads.guildId, guild.id),
|
||||
eq(GuildsForumsThreads.state, 0),
|
||||
),
|
||||
)
|
||||
.orderBy(
|
||||
desc(GuildsForumsThreads.pinned),
|
||||
desc(GuildsForumsThreads.updatedAt),
|
||||
desc(GuildsForumsThreads.id),
|
||||
)
|
||||
.limit(100);
|
||||
} catch {
|
||||
threads = [];
|
||||
}
|
||||
@@ -88,16 +104,16 @@ export default async function GuildForumPage({
|
||||
// Resolve opener usernames in one query (no relation exists in the schema, so
|
||||
// join the users table manually by id). Falls back to the raw author id.
|
||||
const openerIds = Array.from(
|
||||
new Set(threads.map((t) => t.openerId ?? 0).filter((v) => v > 0)),
|
||||
new Set(threads.map((t2) => t2.openerId ?? 0).filter((v) => v > 0)),
|
||||
);
|
||||
|
||||
let users: { id: number; username: string }[] = [];
|
||||
try {
|
||||
users = openerIds.length
|
||||
? await prisma.user.findMany({
|
||||
where: { id: { in: openerIds } },
|
||||
select: { id: true, username: true },
|
||||
})
|
||||
? await db
|
||||
.select({ id: User.id, username: User.username })
|
||||
.from(User)
|
||||
.where(inArray(User.id, openerIds))
|
||||
: [];
|
||||
} catch {
|
||||
users = [];
|
||||
|
||||
Reference in new issue
Block a user