refactor(db): migrate app pages and APIs from Prisma facade to Drizzle (5)
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
9aa4f331bf
commit
30b54e99e7
40 files changed
+1183
-784
No files matched your search
@@ -1,12 +1,13 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { notFound } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { requireModPermission } from "@/lib/admin/guard";
|
||||
import { db, User } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { getAvatarUrl } from "@/lib/imager";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export default async function ModUserDetailPage({
|
||||
params,
|
||||
@@ -19,20 +20,21 @@ export default async function ModUserDetailPage({
|
||||
const id = Number(rawId);
|
||||
if (!Number.isFinite(id) || id <= 0) notFound();
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id },
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
rank: true,
|
||||
online: true,
|
||||
motto: true,
|
||||
look: true,
|
||||
accountCreated: true,
|
||||
lastLogin: true,
|
||||
lastOnline: true,
|
||||
},
|
||||
});
|
||||
const [user] = await db
|
||||
.select({
|
||||
id: User.id,
|
||||
username: User.username,
|
||||
rank: User.rank,
|
||||
online: User.online,
|
||||
motto: User.motto,
|
||||
look: User.look,
|
||||
accountCreated: User.accountCreated,
|
||||
lastLogin: User.lastLogin,
|
||||
lastOnline: User.lastOnline,
|
||||
})
|
||||
.from(User)
|
||||
.where(eq(User.id, id))
|
||||
.limit(1);
|
||||
if (!user) notFound();
|
||||
|
||||
return (
|
||||
|
||||
+55
-34
@@ -1,12 +1,13 @@
|
||||
import { and, asc, count, desc, eq, like, or, type SQL } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { DataTable } from "@/components/admin/data-table";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { requireModPermission } from "@/lib/admin/guard";
|
||||
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
|
||||
import { db, User } from "@/lib/db";
|
||||
import { getAvatarUrl } from "@/lib/imager";
|
||||
import { PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import type { DataTableColumn } from "@/types/common";
|
||||
|
||||
type ModUserRow = {
|
||||
@@ -89,6 +90,15 @@ const columns: DataTableColumn<ModUserRow>[] = [
|
||||
},
|
||||
];
|
||||
|
||||
const SORT_COLS = {
|
||||
id: User.id,
|
||||
username: User.username,
|
||||
rank: User.rank,
|
||||
online: User.online,
|
||||
motto: User.motto,
|
||||
lastLogin: User.lastLogin,
|
||||
} as const;
|
||||
|
||||
export default async function ModUsersPage({
|
||||
searchParams,
|
||||
}: {
|
||||
@@ -99,44 +109,55 @@ export default async function ModUsersPage({
|
||||
const raw = await searchParams;
|
||||
const parsed = parseListParams(new URLSearchParams(raw));
|
||||
|
||||
const where = parsed.search.trim()
|
||||
? {
|
||||
OR: [
|
||||
{ username: { contains: parsed.search.trim() } },
|
||||
{ motto: { contains: parsed.search.trim() } },
|
||||
...(Number.isFinite(Number(parsed.search)) &&
|
||||
Number(parsed.search) > 0
|
||||
? [{ id: Number(parsed.search) }]
|
||||
: []),
|
||||
],
|
||||
}
|
||||
: {};
|
||||
const conditions: SQL[] = [];
|
||||
if (parsed.search.trim()) {
|
||||
const q = parsed.search.trim();
|
||||
const ors = [like(User.username, `%${q}%`), like(User.motto, `%${q}%`)];
|
||||
if (Number.isFinite(Number(q)) && Number(q) > 0) {
|
||||
ors.push(eq(User.id, Number(q)));
|
||||
}
|
||||
conditions.push(or(...ors)!);
|
||||
}
|
||||
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
||||
|
||||
const orderBy = parsed.sort
|
||||
? { [parsed.sort]: parsed.order }
|
||||
: { id: "desc" as const };
|
||||
const sortKey =
|
||||
parsed.sort && parsed.sort in SORT_COLS
|
||||
? (parsed.sort as keyof typeof SORT_COLS)
|
||||
: "id";
|
||||
const sortCol = SORT_COLS[sortKey];
|
||||
const finalOrder =
|
||||
parsed.sort && parsed.sort in SORT_COLS
|
||||
? parsed.order === "asc"
|
||||
? asc(sortCol)
|
||||
: desc(sortCol)
|
||||
: desc(User.id);
|
||||
|
||||
const [total, users] = await Promise.all([
|
||||
prisma.user.count({ where }).catch(() => 0),
|
||||
prisma.user
|
||||
.findMany({
|
||||
where,
|
||||
orderBy,
|
||||
skip: (parsed.page - 1) * parsed.perPage,
|
||||
take: parsed.perPage,
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
rank: true,
|
||||
online: true,
|
||||
motto: true,
|
||||
look: true,
|
||||
lastLogin: true,
|
||||
},
|
||||
const [totals, users] = await Promise.all([
|
||||
db
|
||||
.select({ total: count() })
|
||||
.from(User)
|
||||
.where(where)
|
||||
.then((rows) => rows[0]?.total ?? 0)
|
||||
.catch(() => 0),
|
||||
db
|
||||
.select({
|
||||
id: User.id,
|
||||
username: User.username,
|
||||
rank: User.rank,
|
||||
online: User.online,
|
||||
motto: User.motto,
|
||||
look: User.look,
|
||||
lastLogin: User.lastLogin,
|
||||
})
|
||||
.catch(() => []),
|
||||
.from(User)
|
||||
.where(where)
|
||||
.orderBy(finalOrder)
|
||||
.offset((parsed.page - 1) * parsed.perPage)
|
||||
.limit(parsed.perPage)
|
||||
.catch(() => [] as ModUserRow[]),
|
||||
]);
|
||||
|
||||
const total = totals;
|
||||
const pagination = calcPagination(total, parsed.page, parsed.perPage);
|
||||
const rows: ModUserRow[] = users;
|
||||
|
||||
|
||||
Reference in new issue
Block a user