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 { eq } from "drizzle-orm";
|
||||
import { Package } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { deleteShopArticle, updateShopArticle } from "@/actions/admin-shop";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { db, WebsiteShopArticles } from "@/lib/db";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export default async function EditShopArticle({
|
||||
params,
|
||||
@@ -20,12 +21,14 @@ export default async function EditShopArticle({
|
||||
const t = await getTranslations("pages.admin.shop");
|
||||
|
||||
const { id } = await params;
|
||||
let pkg: Awaited<ReturnType<typeof prisma.websiteShopArticles.findUnique>> =
|
||||
null;
|
||||
let pkg: typeof WebsiteShopArticles.$inferSelect | null = null;
|
||||
try {
|
||||
pkg = await prisma.websiteShopArticles.findUnique({
|
||||
where: { id: BigInt(id) },
|
||||
});
|
||||
const [row] = await db
|
||||
.select()
|
||||
.from(WebsiteShopArticles)
|
||||
.where(eq(WebsiteShopArticles.id, BigInt(id)))
|
||||
.limit(1);
|
||||
pkg = row ?? null;
|
||||
} catch {
|
||||
notFound();
|
||||
}
|
||||
|
||||
+19
-17
@@ -1,3 +1,4 @@
|
||||
import { asc, count, desc } from "drizzle-orm";
|
||||
import { ShoppingCart } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
@@ -8,9 +9,9 @@ import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { CurrencyIcon } from "@/components/shared/currency-icon";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
|
||||
import { db, WebsitePaypalTransactions, WebsiteShopArticles } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
const PAID_STATUS = new Set(["completed", "paid", "success", "approved"]);
|
||||
const RECENT_ORDERS = 25;
|
||||
@@ -65,22 +66,24 @@ export default async function AdminShop({
|
||||
const parsed = parseListParams(new URLSearchParams(raw));
|
||||
|
||||
let packageTotal = 0;
|
||||
let packages: Awaited<
|
||||
ReturnType<typeof prisma.websiteShopArticles.findMany>
|
||||
> = [];
|
||||
let packages: (typeof WebsiteShopArticles.$inferSelect)[] = [];
|
||||
let loadError = false;
|
||||
try {
|
||||
packageTotal = await prisma.websiteShopArticles.count();
|
||||
const [totalRow] = await db
|
||||
.select({ total: count() })
|
||||
.from(WebsiteShopArticles);
|
||||
packageTotal = Number(totalRow?.total ?? 0);
|
||||
const pagination = calcPagination(
|
||||
packageTotal,
|
||||
parsed.page,
|
||||
parsed.perPage,
|
||||
);
|
||||
packages = await prisma.websiteShopArticles.findMany({
|
||||
orderBy: [{ position: "asc" }, { name: "asc" }],
|
||||
skip: pagination.offset,
|
||||
take: pagination.perPage,
|
||||
});
|
||||
packages = await db
|
||||
.select()
|
||||
.from(WebsiteShopArticles)
|
||||
.orderBy(asc(WebsiteShopArticles.position), asc(WebsiteShopArticles.name))
|
||||
.limit(pagination.perPage)
|
||||
.offset(pagination.offset);
|
||||
} catch {
|
||||
packages = [];
|
||||
packageTotal = 0;
|
||||
@@ -93,14 +96,13 @@ export default async function AdminShop({
|
||||
parsed.perPage,
|
||||
);
|
||||
|
||||
let orders: Awaited<
|
||||
ReturnType<typeof prisma.websitePaypalTransactions.findMany>
|
||||
> = [];
|
||||
let orders: (typeof WebsitePaypalTransactions.$inferSelect)[] = [];
|
||||
try {
|
||||
orders = await prisma.websitePaypalTransactions.findMany({
|
||||
orderBy: { createdAt: "desc" },
|
||||
take: RECENT_ORDERS,
|
||||
});
|
||||
orders = await db
|
||||
.select()
|
||||
.from(WebsitePaypalTransactions)
|
||||
.orderBy(desc(WebsitePaypalTransactions.createdAt))
|
||||
.limit(RECENT_ORDERS);
|
||||
} catch {
|
||||
orders = [];
|
||||
loadError = true;
|
||||
|
||||
Reference in new issue
Block a user