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 = [];
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
@@ -5,7 +6,7 @@ import type { CSSProperties } from "react";
|
||||
import { postThread } from "@/actions/social";
|
||||
import { ContentCard } from "@/components/public/ui";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { db, Guilds } from "@/lib/db";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
@@ -47,10 +48,12 @@ export default async function NewThreadPage({
|
||||
// Guild header (read-only). A DB hiccup degrades to notFound rather than 500.
|
||||
let guild: { id: number; name: string } | null = null;
|
||||
try {
|
||||
guild = await prisma.guilds.findUnique({
|
||||
where: { id: guildId },
|
||||
select: { id: true, name: true },
|
||||
});
|
||||
const [row] = await db
|
||||
.select({ id: Guilds.id, name: Guilds.name })
|
||||
.from(Guilds)
|
||||
.where(eq(Guilds.id, guildId))
|
||||
.limit(1);
|
||||
guild = row ?? null;
|
||||
} catch {
|
||||
guild = null;
|
||||
}
|
||||
|
||||
@@ -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 = [];
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { asc, eq, inArray } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { notFound } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { ContentCard, EmptyState, StatBlock } from "@/components/public/ui";
|
||||
import { db, Guilds, GuildsMembers, User } from "@/lib/db";
|
||||
import { avatarImageUrl } from "@/lib/format";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { siteSettings } from "@/lib/services/site-settings";
|
||||
|
||||
export const revalidate = 300;
|
||||
@@ -39,17 +40,19 @@ export default async function GuildPage({
|
||||
|
||||
let guild: GuildView | null = null;
|
||||
try {
|
||||
guild = await prisma.guilds.findUnique({
|
||||
where: { id: guildId },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
badge: true,
|
||||
userId: true,
|
||||
dateCreated: true,
|
||||
},
|
||||
});
|
||||
const [row] = await db
|
||||
.select({
|
||||
id: Guilds.id,
|
||||
name: Guilds.name,
|
||||
description: Guilds.description,
|
||||
badge: Guilds.badge,
|
||||
userId: Guilds.userId,
|
||||
dateCreated: Guilds.dateCreated,
|
||||
})
|
||||
.from(Guilds)
|
||||
.where(eq(Guilds.id, guildId))
|
||||
.limit(1);
|
||||
guild = row ?? null;
|
||||
} catch {
|
||||
guild = null;
|
||||
}
|
||||
@@ -60,12 +63,15 @@ export default async function GuildPage({
|
||||
// join the users table manually by id.
|
||||
let memberRows: { userId: number; memberSince: number }[] = [];
|
||||
try {
|
||||
memberRows = await prisma.guildsMembers.findMany({
|
||||
where: { guildId: guild.id },
|
||||
orderBy: { memberSince: "asc" },
|
||||
take: 200,
|
||||
select: { userId: true, memberSince: true },
|
||||
});
|
||||
memberRows = await db
|
||||
.select({
|
||||
userId: GuildsMembers.userId,
|
||||
memberSince: GuildsMembers.memberSince,
|
||||
})
|
||||
.from(GuildsMembers)
|
||||
.where(eq(GuildsMembers.guildId, guild.id))
|
||||
.orderBy(asc(GuildsMembers.memberSince))
|
||||
.limit(200);
|
||||
} catch {
|
||||
memberRows = [];
|
||||
}
|
||||
@@ -80,10 +86,15 @@ export default async function GuildPage({
|
||||
[];
|
||||
try {
|
||||
users = memberIds.length
|
||||
? await prisma.user.findMany({
|
||||
where: { id: { in: memberIds } },
|
||||
select: { id: true, username: true, look: true, motto: true },
|
||||
})
|
||||
? await db
|
||||
.select({
|
||||
id: User.id,
|
||||
username: User.username,
|
||||
look: User.look,
|
||||
motto: User.motto,
|
||||
})
|
||||
.from(User)
|
||||
.where(inArray(User.id, memberIds))
|
||||
: [];
|
||||
} catch {
|
||||
users = [];
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { desc } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { ContentCard, EmptyState } from "@/components/public/ui";
|
||||
import { db, Guilds } from "@/lib/db";
|
||||
import { excerpt } from "@/lib/format";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const revalidate = 300;
|
||||
|
||||
@@ -17,12 +18,16 @@ type GuildCard = {
|
||||
|
||||
async function getGuilds(): Promise<GuildCard[]> {
|
||||
try {
|
||||
const guilds = await prisma.guilds.findMany({
|
||||
orderBy: { id: "desc" },
|
||||
take: 40,
|
||||
select: { id: true, name: true, description: true, badge: true },
|
||||
});
|
||||
return guilds;
|
||||
return await db
|
||||
.select({
|
||||
id: Guilds.id,
|
||||
name: Guilds.name,
|
||||
description: Guilds.description,
|
||||
badge: Guilds.badge,
|
||||
})
|
||||
.from(Guilds)
|
||||
.orderBy(desc(Guilds.id))
|
||||
.limit(40);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user