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
File diff suppressed because it is too large.
Load diff
@@ -1,9 +1,10 @@
|
||||
import { and, count, desc, inArray, isNull, lte, or } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { ContentCard, EmptyState } from "@/components/public/ui";
|
||||
import { db, WebsitePoll, WebsitePollQuestion } from "@/lib/db";
|
||||
import { excerpt } from "@/lib/format";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const revalidate = 60;
|
||||
|
||||
@@ -13,20 +14,41 @@ export default async function PollsPage() {
|
||||
const t = await getTranslations("pages.polls");
|
||||
const now = new Date();
|
||||
|
||||
const polls = await prisma.websitePoll
|
||||
.findMany({
|
||||
where: {
|
||||
status: { in: ["active", "closed"] },
|
||||
OR: [{ startsAt: null }, { startsAt: { lte: now } }],
|
||||
},
|
||||
orderBy: { createdAt: "desc" },
|
||||
take: 50,
|
||||
include: {
|
||||
_count: { select: { questions: true } },
|
||||
},
|
||||
})
|
||||
const pollRows = await db
|
||||
.select()
|
||||
.from(WebsitePoll)
|
||||
.where(
|
||||
and(
|
||||
inArray(WebsitePoll.status, ["active", "closed"]),
|
||||
or(isNull(WebsitePoll.startsAt), lte(WebsitePoll.startsAt, now)),
|
||||
),
|
||||
)
|
||||
.orderBy(desc(WebsitePoll.createdAt))
|
||||
.limit(50)
|
||||
.catch(() => []);
|
||||
|
||||
const pollIds = pollRows.map((p) => p.id);
|
||||
const questionCounts =
|
||||
pollIds.length > 0
|
||||
? await db
|
||||
.select({
|
||||
pollId: WebsitePollQuestion.pollId,
|
||||
value: count(),
|
||||
})
|
||||
.from(WebsitePollQuestion)
|
||||
.where(inArray(WebsitePollQuestion.pollId, pollIds))
|
||||
.groupBy(WebsitePollQuestion.pollId)
|
||||
.catch(() => [])
|
||||
: [];
|
||||
const countByPoll = new Map(
|
||||
questionCounts.map((r) => [r.pollId, Number(r.value)]),
|
||||
);
|
||||
|
||||
const polls = pollRows.map((poll) => ({
|
||||
...poll,
|
||||
_count: { questions: countByPoll.get(poll.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