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,3 +1,4 @@
|
||||
import { and, asc, eq, inArray } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { notFound } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
@@ -5,8 +6,14 @@ import type { CSSProperties } from "react";
|
||||
import { replyToThread } from "@/actions/social";
|
||||
import { ContentCard, EmptyState } from "@/components/public/ui";
|
||||
import { auth } from "@/lib/auth";
|
||||
import {
|
||||
db,
|
||||
Guilds,
|
||||
GuildsForumsComments,
|
||||
GuildsForumsThreads,
|
||||
User,
|
||||
} from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const revalidate = 60;
|
||||
|
||||
@@ -63,22 +70,34 @@ export default async function GuildForumThreadPage({
|
||||
}[] = [];
|
||||
|
||||
try {
|
||||
[guild, thread] = await Promise.all([
|
||||
prisma.guilds.findUnique({
|
||||
where: { id: guildId },
|
||||
select: { id: true, name: true },
|
||||
}),
|
||||
prisma.guildsForumsThreads.findFirst({
|
||||
where: { id: threadId, guildId, state: 0 },
|
||||
select: {
|
||||
id: true,
|
||||
subject: true,
|
||||
locked: true,
|
||||
pinned: true,
|
||||
openerId: true,
|
||||
},
|
||||
}),
|
||||
const [guildRow, threadRow] = await Promise.all([
|
||||
db
|
||||
.select({ id: Guilds.id, name: Guilds.name })
|
||||
.from(Guilds)
|
||||
.where(eq(Guilds.id, guildId))
|
||||
.limit(1)
|
||||
.then((rows) => rows[0] ?? null),
|
||||
db
|
||||
.select({
|
||||
id: GuildsForumsThreads.id,
|
||||
subject: GuildsForumsThreads.subject,
|
||||
locked: GuildsForumsThreads.locked,
|
||||
pinned: GuildsForumsThreads.pinned,
|
||||
openerId: GuildsForumsThreads.openerId,
|
||||
})
|
||||
.from(GuildsForumsThreads)
|
||||
.where(
|
||||
and(
|
||||
eq(GuildsForumsThreads.id, threadId),
|
||||
eq(GuildsForumsThreads.guildId, guildId),
|
||||
eq(GuildsForumsThreads.state, 0),
|
||||
),
|
||||
)
|
||||
.limit(1)
|
||||
.then((rows) => rows[0] ?? null),
|
||||
]);
|
||||
guild = guildRow;
|
||||
thread = threadRow;
|
||||
} catch {
|
||||
guild = null;
|
||||
thread = null;
|
||||
@@ -87,16 +106,24 @@ export default async function GuildForumThreadPage({
|
||||
if (!guild || !thread) notFound();
|
||||
|
||||
try {
|
||||
posts = await prisma.guildsForumsComments.findMany({
|
||||
where: { threadId: thread.id, state: 0 },
|
||||
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
message: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
posts = await db
|
||||
.select({
|
||||
id: GuildsForumsComments.id,
|
||||
userId: GuildsForumsComments.userId,
|
||||
message: GuildsForumsComments.message,
|
||||
createdAt: GuildsForumsComments.createdAt,
|
||||
})
|
||||
.from(GuildsForumsComments)
|
||||
.where(
|
||||
and(
|
||||
eq(GuildsForumsComments.threadId, thread.id),
|
||||
eq(GuildsForumsComments.state, 0),
|
||||
),
|
||||
)
|
||||
.orderBy(
|
||||
asc(GuildsForumsComments.createdAt),
|
||||
asc(GuildsForumsComments.id),
|
||||
);
|
||||
} catch {
|
||||
posts = [];
|
||||
}
|
||||
@@ -107,10 +134,10 @@ export default async function GuildForumThreadPage({
|
||||
let users: { id: number; username: string }[] = [];
|
||||
try {
|
||||
users = authorIds.length
|
||||
? await prisma.user.findMany({
|
||||
where: { id: { in: authorIds } },
|
||||
select: { id: true, username: true },
|
||||
})
|
||||
? await db
|
||||
.select({ id: User.id, username: User.username })
|
||||
.from(User)
|
||||
.where(inArray(User.id, authorIds))
|
||||
: [];
|
||||
} catch {
|
||||
users = [];
|
||||
|
||||
Reference in new issue
Block a user