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 } from "drizzle-orm";
|
||||
import { Store } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
@@ -8,9 +9,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, MarketplaceItems, 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 AdminMarketplacePage({
|
||||
searchParams,
|
||||
@@ -28,27 +29,37 @@ export default async function AdminMarketplacePage({
|
||||
const parsed = parseListParams(new URLSearchParams(raw));
|
||||
const filter = raw.filter === "sold" ? "sold" : "active";
|
||||
|
||||
const where = filter === "sold" ? { state: 2 } : { state: 1 };
|
||||
const where = eq(MarketplaceItems.state, filter === "sold" ? 2 : 1);
|
||||
|
||||
let total = 0;
|
||||
let rows: Awaited<ReturnType<typeof prisma.marketplaceItems.findMany>> = [];
|
||||
let rows: (typeof MarketplaceItems.$inferSelect)[] = [];
|
||||
let activeCount = 0;
|
||||
let soldCount = 0;
|
||||
let loadError = false;
|
||||
|
||||
try {
|
||||
[total, activeCount, soldCount] = await Promise.all([
|
||||
prisma.marketplaceItems.count({ where }),
|
||||
prisma.marketplaceItems.count({ where: { state: 1 } }),
|
||||
prisma.marketplaceItems.count({ where: { state: 2 } }),
|
||||
const [totalRows, activeRows, soldRows] = await Promise.all([
|
||||
db.select({ total: count() }).from(MarketplaceItems).where(where),
|
||||
db
|
||||
.select({ total: count() })
|
||||
.from(MarketplaceItems)
|
||||
.where(eq(MarketplaceItems.state, 1)),
|
||||
db
|
||||
.select({ total: count() })
|
||||
.from(MarketplaceItems)
|
||||
.where(eq(MarketplaceItems.state, 2)),
|
||||
]);
|
||||
total = Number(totalRows[0]?.total ?? 0);
|
||||
activeCount = Number(activeRows[0]?.total ?? 0);
|
||||
soldCount = Number(soldRows[0]?.total ?? 0);
|
||||
const pagination = calcPagination(total, parsed.page, parsed.perPage);
|
||||
rows = await prisma.marketplaceItems.findMany({
|
||||
where,
|
||||
orderBy: { timestamp: "desc" },
|
||||
skip: pagination.offset,
|
||||
take: pagination.perPage,
|
||||
});
|
||||
rows = await db
|
||||
.select()
|
||||
.from(MarketplaceItems)
|
||||
.where(where)
|
||||
.orderBy(desc(MarketplaceItems.timestamp))
|
||||
.limit(pagination.perPage)
|
||||
.offset(pagination.offset);
|
||||
} catch {
|
||||
loadError = true;
|
||||
}
|
||||
@@ -59,11 +70,10 @@ export default async function AdminMarketplacePage({
|
||||
];
|
||||
const users =
|
||||
userIds.length > 0
|
||||
? await prisma.user
|
||||
.findMany({
|
||||
where: { id: { in: userIds } },
|
||||
select: { id: true, username: true },
|
||||
})
|
||||
? await db
|
||||
.select({ id: User.id, username: User.username })
|
||||
.from(User)
|
||||
.where(inArray(User.id, userIds))
|
||||
.catch(() => [])
|
||||
: [];
|
||||
const usernameById = new Map(users.map((u) => [u.id, u.username]));
|
||||
|
||||
Reference in new issue
Block a user