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,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