refactor(db): migrate app pages and APIs from Prisma facade to Drizzle (3)
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
2cd0863cb8
commit
580972c0a0
40 files changed
+1089
-808
No files matched your search
@@ -1,11 +1,12 @@
|
||||
import { asc, count, eq, inArray } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { disbandGuild } from "@/actions/admin-guilds";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { db, Guilds, GuildsForumsThreads, GuildsMembers, User } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export default async function AdminGuildDetailPage({
|
||||
params,
|
||||
@@ -23,49 +24,65 @@ export default async function AdminGuildDetailPage({
|
||||
const id = Number(rawId);
|
||||
if (!Number.isFinite(id) || id <= 0) notFound();
|
||||
|
||||
const guild = await prisma.guilds.findUnique({
|
||||
where: { id },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
userId: true,
|
||||
roomId: true,
|
||||
badge: true,
|
||||
dateCreated: true,
|
||||
state: true,
|
||||
forum: true,
|
||||
},
|
||||
});
|
||||
const [guild] = await db
|
||||
.select({
|
||||
id: Guilds.id,
|
||||
name: Guilds.name,
|
||||
description: Guilds.description,
|
||||
userId: Guilds.userId,
|
||||
roomId: Guilds.roomId,
|
||||
badge: Guilds.badge,
|
||||
dateCreated: Guilds.dateCreated,
|
||||
state: Guilds.state,
|
||||
forum: Guilds.forum,
|
||||
})
|
||||
.from(Guilds)
|
||||
.where(eq(Guilds.id, id))
|
||||
.limit(1);
|
||||
if (!guild) notFound();
|
||||
|
||||
const [owner, members, memberCount, threadCount] = await Promise.all([
|
||||
prisma.user
|
||||
.findUnique({
|
||||
where: { id: guild.userId },
|
||||
select: { id: true, username: true },
|
||||
})
|
||||
.catch(() => null),
|
||||
prisma.guildsMembers
|
||||
.findMany({
|
||||
where: { guildId: id },
|
||||
orderBy: [{ levelId: "asc" }, { memberSince: "asc" }],
|
||||
take: 100,
|
||||
select: { userId: true, levelId: true, memberSince: true },
|
||||
})
|
||||
.catch(() => []),
|
||||
prisma.guildsMembers.count({ where: { guildId: id } }).catch(() => 0),
|
||||
prisma.guildsForumsThreads.count({ where: { guildId: id } }).catch(() => 0),
|
||||
]);
|
||||
const [ownerRows, members, memberCountRows, threadCountRows] =
|
||||
await Promise.all([
|
||||
db
|
||||
.select({ id: User.id, username: User.username })
|
||||
.from(User)
|
||||
.where(eq(User.id, guild.userId))
|
||||
.limit(1)
|
||||
.catch(() => []),
|
||||
db
|
||||
.select({
|
||||
userId: GuildsMembers.userId,
|
||||
levelId: GuildsMembers.levelId,
|
||||
memberSince: GuildsMembers.memberSince,
|
||||
})
|
||||
.from(GuildsMembers)
|
||||
.where(eq(GuildsMembers.guildId, id))
|
||||
.orderBy(asc(GuildsMembers.levelId), asc(GuildsMembers.memberSince))
|
||||
.limit(100)
|
||||
.catch(() => []),
|
||||
db
|
||||
.select({ total: count() })
|
||||
.from(GuildsMembers)
|
||||
.where(eq(GuildsMembers.guildId, id))
|
||||
.catch(() => [{ total: 0 }]),
|
||||
db
|
||||
.select({ total: count() })
|
||||
.from(GuildsForumsThreads)
|
||||
.where(eq(GuildsForumsThreads.guildId, id))
|
||||
.catch(() => [{ total: 0 }]),
|
||||
]);
|
||||
|
||||
const owner = ownerRows[0] ?? null;
|
||||
const memberCount = Number(memberCountRows[0]?.total ?? 0);
|
||||
const threadCount = Number(threadCountRows[0]?.total ?? 0);
|
||||
|
||||
const memberUserIds = members.map((m) => m.userId).filter((uid) => uid > 0);
|
||||
const memberUsers =
|
||||
memberUserIds.length > 0
|
||||
? await prisma.user
|
||||
.findMany({
|
||||
where: { id: { in: memberUserIds } },
|
||||
select: { id: true, username: true },
|
||||
})
|
||||
? await db
|
||||
.select({ id: User.id, username: User.username })
|
||||
.from(User)
|
||||
.where(inArray(User.id, memberUserIds))
|
||||
.catch(() => [])
|
||||
: [];
|
||||
const usernameById = new Map(memberUsers.map((u) => [u.id, u.username]));
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { count, desc, eq, inArray, like, or } from "drizzle-orm";
|
||||
import { Shield } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
@@ -7,9 +8,9 @@ import { AdminSimplePager } from "@/components/admin/admin-simple-pager";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
|
||||
import { db, Guilds, GuildsMembers, User } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export default async function AdminGuildsPage({
|
||||
searchParams,
|
||||
@@ -40,34 +41,36 @@ export default async function AdminGuildsPage({
|
||||
|
||||
try {
|
||||
const where = q
|
||||
? {
|
||||
OR: [
|
||||
{ name: { contains: q } },
|
||||
{ description: { contains: q } },
|
||||
...(Number.isFinite(Number(q)) && Number(q) > 0
|
||||
? [{ id: Number(q) }]
|
||||
: []),
|
||||
],
|
||||
}
|
||||
: {};
|
||||
? or(
|
||||
like(Guilds.name, `%${q}%`),
|
||||
like(Guilds.description, `%${q}%`),
|
||||
...(Number.isFinite(Number(q)) && Number(q) > 0
|
||||
? [eq(Guilds.id, Number(q))]
|
||||
: []),
|
||||
)
|
||||
: undefined;
|
||||
|
||||
total = await prisma.guilds.count({ where });
|
||||
const [totalRow] = await db
|
||||
.select({ total: count() })
|
||||
.from(Guilds)
|
||||
.where(where);
|
||||
total = Number(totalRow?.total ?? 0);
|
||||
const pagination = calcPagination(total, parsed.page, parsed.perPage);
|
||||
rows = await prisma.guilds.findMany({
|
||||
where,
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
userId: true,
|
||||
badge: true,
|
||||
dateCreated: true,
|
||||
roomId: true,
|
||||
},
|
||||
orderBy: { id: "desc" },
|
||||
skip: pagination.offset,
|
||||
take: pagination.perPage,
|
||||
});
|
||||
rows = await db
|
||||
.select({
|
||||
id: Guilds.id,
|
||||
name: Guilds.name,
|
||||
description: Guilds.description,
|
||||
userId: Guilds.userId,
|
||||
badge: Guilds.badge,
|
||||
dateCreated: Guilds.dateCreated,
|
||||
roomId: Guilds.roomId,
|
||||
})
|
||||
.from(Guilds)
|
||||
.where(where)
|
||||
.orderBy(desc(Guilds.id))
|
||||
.limit(pagination.perPage)
|
||||
.offset(pagination.offset);
|
||||
} catch {
|
||||
loadError = true;
|
||||
}
|
||||
@@ -80,27 +83,28 @@ export default async function AdminGuildsPage({
|
||||
|
||||
const [owners, memberGroups] = await Promise.all([
|
||||
ownerIds.length
|
||||
? prisma.user
|
||||
.findMany({
|
||||
where: { id: { in: ownerIds } },
|
||||
select: { id: true, username: true },
|
||||
})
|
||||
? db
|
||||
.select({ id: User.id, username: User.username })
|
||||
.from(User)
|
||||
.where(inArray(User.id, ownerIds))
|
||||
.catch(() => [])
|
||||
: Promise.resolve([]),
|
||||
guildIds.length
|
||||
? prisma.guildsMembers
|
||||
.groupBy({
|
||||
by: ["guildId"],
|
||||
where: { guildId: { in: guildIds } },
|
||||
_count: { _all: true },
|
||||
? db
|
||||
.select({
|
||||
guildId: GuildsMembers.guildId,
|
||||
total: count(),
|
||||
})
|
||||
.from(GuildsMembers)
|
||||
.where(inArray(GuildsMembers.guildId, guildIds))
|
||||
.groupBy(GuildsMembers.guildId)
|
||||
.catch(() => [])
|
||||
: Promise.resolve([]),
|
||||
]);
|
||||
|
||||
const ownerById = new Map(owners.map((u) => [u.id, u.username]));
|
||||
const membersByGuild = new Map(
|
||||
memberGroups.map((g) => [g.guildId, g._count._all]),
|
||||
memberGroups.map((g) => [g.guildId, Number(g.total)]),
|
||||
);
|
||||
|
||||
return (
|
||||
|
||||
Reference in new issue
Block a user