refactor(db): migrate app pages and APIs from Prisma facade to Drizzle
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
53b350057d
commit
7c39aef5d9
40 files changed
+1124
-692
No files matched your search
@@ -1,10 +1,16 @@
|
||||
import { count, desc, eq, inArray } from "drizzle-orm";
|
||||
import Image from "next/image";
|
||||
import Link from "next/link";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { ContentCard, EmptyState } from "@/components/public/ui";
|
||||
import {
|
||||
db,
|
||||
WebsiteEvent,
|
||||
WebsiteEventRegistration,
|
||||
WebsiteEventType,
|
||||
} from "@/lib/db";
|
||||
import { excerpt, slugify } from "@/lib/format";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const revalidate = 60;
|
||||
|
||||
@@ -13,18 +19,53 @@ export const metadata = { title: "Events" };
|
||||
export default async function EventsPage() {
|
||||
const t = await getTranslations("pages.events");
|
||||
|
||||
const events = await prisma.websiteEvent
|
||||
.findMany({
|
||||
where: { status: { in: ["published", "completed"] } },
|
||||
orderBy: { startsAt: "desc" },
|
||||
take: 50,
|
||||
include: {
|
||||
type: { select: { name: true, color: true, slug: true } },
|
||||
_count: { select: { registrations: true } },
|
||||
},
|
||||
const eventRows = await db
|
||||
.select({
|
||||
id: WebsiteEvent.id,
|
||||
title: WebsiteEvent.title,
|
||||
description: WebsiteEvent.description,
|
||||
status: WebsiteEvent.status,
|
||||
image: WebsiteEvent.image,
|
||||
startsAt: WebsiteEvent.startsAt,
|
||||
maxPlayers: WebsiteEvent.maxPlayers,
|
||||
typeName: WebsiteEventType.name,
|
||||
typeColor: WebsiteEventType.color,
|
||||
typeSlug: WebsiteEventType.slug,
|
||||
})
|
||||
.from(WebsiteEvent)
|
||||
.innerJoin(WebsiteEventType, eq(WebsiteEvent.typeId, WebsiteEventType.id))
|
||||
.where(inArray(WebsiteEvent.status, ["published", "completed"]))
|
||||
.orderBy(desc(WebsiteEvent.startsAt))
|
||||
.limit(50)
|
||||
.catch(() => []);
|
||||
|
||||
const eventIds = eventRows.map((e) => e.id);
|
||||
const regCounts =
|
||||
eventIds.length > 0
|
||||
? await db
|
||||
.select({
|
||||
eventId: WebsiteEventRegistration.eventId,
|
||||
value: count(),
|
||||
})
|
||||
.from(WebsiteEventRegistration)
|
||||
.where(inArray(WebsiteEventRegistration.eventId, eventIds))
|
||||
.groupBy(WebsiteEventRegistration.eventId)
|
||||
.catch(() => [])
|
||||
: [];
|
||||
const countByEvent = new Map(
|
||||
regCounts.map((r) => [r.eventId, Number(r.value)]),
|
||||
);
|
||||
|
||||
const events = eventRows.map((event) => ({
|
||||
...event,
|
||||
type: {
|
||||
name: event.typeName,
|
||||
color: event.typeColor,
|
||||
slug: event.typeSlug,
|
||||
},
|
||||
_count: { registrations: countByEvent.get(event.id) ?? 0 },
|
||||
}));
|
||||
|
||||
return (
|
||||
<main style={{ display: "grid", gap: "1.5rem" }}>
|
||||
<ContentCard icon="📅" title={t("title")} subtitle={t("subtitle")} />
|
||||
|
||||
Reference in new issue
Block a user