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,11 @@
|
||||
import { asc, eq, ne } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { notFound } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { ContentCard } from "@/components/public/ui";
|
||||
import { SanitizedHtml } from "@/components/shared/sanitized-html";
|
||||
import { db, WebsiteHelpCenterCategories } from "@/lib/db";
|
||||
import { resolveHotelName } from "@/lib/hotel-name";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { sanitize } from "@/lib/sanitize";
|
||||
|
||||
export const revalidate = 300;
|
||||
@@ -54,21 +55,23 @@ export default async function HelpCategoryPage({
|
||||
|
||||
let cat: HelpCategory | null = null;
|
||||
try {
|
||||
cat = await prisma.websiteHelpCenterCategories.findUnique({
|
||||
where: { id: categoryId },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
content: true,
|
||||
position: true,
|
||||
imageUrl: true,
|
||||
buttonText: true,
|
||||
buttonUrl: true,
|
||||
buttonColor: true,
|
||||
buttonBorderColor: true,
|
||||
smallBox: true,
|
||||
},
|
||||
});
|
||||
const [row] = await db
|
||||
.select({
|
||||
id: WebsiteHelpCenterCategories.id,
|
||||
name: WebsiteHelpCenterCategories.name,
|
||||
content: WebsiteHelpCenterCategories.content,
|
||||
position: WebsiteHelpCenterCategories.position,
|
||||
imageUrl: WebsiteHelpCenterCategories.imageUrl,
|
||||
buttonText: WebsiteHelpCenterCategories.buttonText,
|
||||
buttonUrl: WebsiteHelpCenterCategories.buttonUrl,
|
||||
buttonColor: WebsiteHelpCenterCategories.buttonColor,
|
||||
buttonBorderColor: WebsiteHelpCenterCategories.buttonBorderColor,
|
||||
smallBox: WebsiteHelpCenterCategories.smallBox,
|
||||
})
|
||||
.from(WebsiteHelpCenterCategories)
|
||||
.where(eq(WebsiteHelpCenterCategories.id, categoryId))
|
||||
.limit(1);
|
||||
cat = row ?? null;
|
||||
} catch {
|
||||
cat = null;
|
||||
}
|
||||
@@ -81,12 +84,16 @@ export default async function HelpCategoryPage({
|
||||
// query: a DB hiccup degrades this list to empty rather than 500-ing.
|
||||
let siblings: HelpCategoryLink[] = [];
|
||||
try {
|
||||
siblings = await prisma.websiteHelpCenterCategories.findMany({
|
||||
where: { id: { not: categoryId } },
|
||||
orderBy: { position: "asc" },
|
||||
select: { id: true, name: true, position: true },
|
||||
take: 50,
|
||||
});
|
||||
siblings = await db
|
||||
.select({
|
||||
id: WebsiteHelpCenterCategories.id,
|
||||
name: WebsiteHelpCenterCategories.name,
|
||||
position: WebsiteHelpCenterCategories.position,
|
||||
})
|
||||
.from(WebsiteHelpCenterCategories)
|
||||
.where(ne(WebsiteHelpCenterCategories.id, categoryId))
|
||||
.orderBy(asc(WebsiteHelpCenterCategories.position))
|
||||
.limit(50);
|
||||
} catch {
|
||||
siblings = [];
|
||||
}
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import { asc } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { ContentCard, EmptyState } from "@/components/public/ui";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import {
|
||||
db,
|
||||
WebsiteHelpCenterCategories,
|
||||
WebsiteRuleCategories,
|
||||
WebsiteRules,
|
||||
} from "@/lib/db";
|
||||
|
||||
export const revalidate = 300;
|
||||
|
||||
@@ -40,40 +46,57 @@ export default async function HelpCenterPage() {
|
||||
|
||||
let categories: HelpCategory[] = [];
|
||||
try {
|
||||
categories = await prisma.websiteHelpCenterCategories.findMany({
|
||||
orderBy: [{ position: "asc" }, { id: "asc" }],
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
content: true,
|
||||
imageUrl: true,
|
||||
buttonText: true,
|
||||
buttonUrl: true,
|
||||
buttonColor: true,
|
||||
buttonBorderColor: true,
|
||||
smallBox: true,
|
||||
},
|
||||
});
|
||||
categories = await db
|
||||
.select({
|
||||
id: WebsiteHelpCenterCategories.id,
|
||||
name: WebsiteHelpCenterCategories.name,
|
||||
content: WebsiteHelpCenterCategories.content,
|
||||
imageUrl: WebsiteHelpCenterCategories.imageUrl,
|
||||
buttonText: WebsiteHelpCenterCategories.buttonText,
|
||||
buttonUrl: WebsiteHelpCenterCategories.buttonUrl,
|
||||
buttonColor: WebsiteHelpCenterCategories.buttonColor,
|
||||
buttonBorderColor: WebsiteHelpCenterCategories.buttonBorderColor,
|
||||
smallBox: WebsiteHelpCenterCategories.smallBox,
|
||||
})
|
||||
.from(WebsiteHelpCenterCategories)
|
||||
.orderBy(
|
||||
asc(WebsiteHelpCenterCategories.position),
|
||||
asc(WebsiteHelpCenterCategories.id),
|
||||
);
|
||||
} catch {
|
||||
// DB unavailable — render the shell without help categories.
|
||||
}
|
||||
|
||||
let ruleCategories: RuleCategory[] = [];
|
||||
try {
|
||||
ruleCategories = await prisma.websiteRuleCategories.findMany({
|
||||
orderBy: { id: "asc" },
|
||||
select: { id: true, name: true, description: true, badge: true },
|
||||
});
|
||||
ruleCategories = await db
|
||||
.select({
|
||||
id: WebsiteRuleCategories.id,
|
||||
name: WebsiteRuleCategories.name,
|
||||
description: WebsiteRuleCategories.description,
|
||||
badge: WebsiteRuleCategories.badge,
|
||||
})
|
||||
.from(WebsiteRuleCategories)
|
||||
.orderBy(asc(WebsiteRuleCategories.id));
|
||||
} catch {
|
||||
// DB unavailable — render the shell without rule categories.
|
||||
}
|
||||
|
||||
let rules: Rule[] = [];
|
||||
try {
|
||||
rules = await prisma.websiteRules.findMany({
|
||||
orderBy: [{ categoryId: "asc" }, { paragraph: "asc" }, { id: "asc" }],
|
||||
select: { id: true, categoryId: true, paragraph: true, rule: true },
|
||||
});
|
||||
rules = await db
|
||||
.select({
|
||||
id: WebsiteRules.id,
|
||||
categoryId: WebsiteRules.categoryId,
|
||||
paragraph: WebsiteRules.paragraph,
|
||||
rule: WebsiteRules.rule,
|
||||
})
|
||||
.from(WebsiteRules)
|
||||
.orderBy(
|
||||
asc(WebsiteRules.categoryId),
|
||||
asc(WebsiteRules.paragraph),
|
||||
asc(WebsiteRules.id),
|
||||
);
|
||||
} catch {
|
||||
// DB unavailable — render the shell without rules.
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { asc, eq, inArray } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
@@ -6,8 +7,13 @@ import { closeHelpTicket, replyHelpTicket } from "@/actions/help-tickets";
|
||||
import { ContentCard } from "@/components/public/ui";
|
||||
import { positiveBigInt } from "@/lib/api";
|
||||
import { auth } from "@/lib/auth";
|
||||
import {
|
||||
db,
|
||||
User,
|
||||
WebsiteHelpCenterTicketReplies,
|
||||
WebsiteHelpCenterTickets,
|
||||
} from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
@@ -67,17 +73,19 @@ export default async function HelpTicketDetailPage({
|
||||
}[] = [];
|
||||
|
||||
try {
|
||||
ticket = await prisma.websiteHelpCenterTickets.findUnique({
|
||||
where: { id: ticketId },
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
title: true,
|
||||
content: true,
|
||||
open: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
const [row] = await db
|
||||
.select({
|
||||
id: WebsiteHelpCenterTickets.id,
|
||||
userId: WebsiteHelpCenterTickets.userId,
|
||||
title: WebsiteHelpCenterTickets.title,
|
||||
content: WebsiteHelpCenterTickets.content,
|
||||
open: WebsiteHelpCenterTickets.open,
|
||||
createdAt: WebsiteHelpCenterTickets.createdAt,
|
||||
})
|
||||
.from(WebsiteHelpCenterTickets)
|
||||
.where(eq(WebsiteHelpCenterTickets.id, ticketId))
|
||||
.limit(1);
|
||||
ticket = row ?? null;
|
||||
} catch {
|
||||
ticket = null;
|
||||
}
|
||||
@@ -85,16 +93,16 @@ export default async function HelpTicketDetailPage({
|
||||
if (!ticket || ticket.userId !== userId) notFound();
|
||||
|
||||
try {
|
||||
replies = await prisma.websiteHelpCenterTicketReplies.findMany({
|
||||
where: { ticketId: ticket.id },
|
||||
orderBy: { id: "asc" },
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
content: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
replies = await db
|
||||
.select({
|
||||
id: WebsiteHelpCenterTicketReplies.id,
|
||||
userId: WebsiteHelpCenterTicketReplies.userId,
|
||||
content: WebsiteHelpCenterTicketReplies.content,
|
||||
createdAt: WebsiteHelpCenterTicketReplies.createdAt,
|
||||
})
|
||||
.from(WebsiteHelpCenterTicketReplies)
|
||||
.where(eq(WebsiteHelpCenterTicketReplies.ticketId, ticket.id))
|
||||
.orderBy(asc(WebsiteHelpCenterTicketReplies.id));
|
||||
} catch {
|
||||
replies = [];
|
||||
}
|
||||
@@ -102,10 +110,10 @@ export default async function HelpTicketDetailPage({
|
||||
const authorIds = [...new Set([userId, ...replies.map((r) => r.userId)])];
|
||||
let users: { id: number; username: string }[] = [];
|
||||
try {
|
||||
users = await prisma.user.findMany({
|
||||
where: { id: { in: authorIds } },
|
||||
select: { id: true, username: true },
|
||||
});
|
||||
users = await db
|
||||
.select({ id: User.id, username: User.username })
|
||||
.from(User)
|
||||
.where(inArray(User.id, authorIds));
|
||||
} catch {
|
||||
users = [];
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
import Link from "next/link";
|
||||
import { redirect } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
@@ -5,8 +6,8 @@ import type { CSSProperties } from "react";
|
||||
import { createTicket } from "@/actions/help-tickets";
|
||||
import { ContentCard, EmptyState, StatBlock } from "@/components/public/ui";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { db, WebsiteHelpCenterTickets } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
@@ -41,15 +42,14 @@ export default async function HelpTicketsPage({
|
||||
|
||||
const userId = Number(session.user.id);
|
||||
|
||||
let tickets: Awaited<
|
||||
ReturnType<typeof prisma.websiteHelpCenterTickets.findMany>
|
||||
> = [];
|
||||
let tickets: (typeof WebsiteHelpCenterTickets.$inferSelect)[] = [];
|
||||
try {
|
||||
tickets = await prisma.websiteHelpCenterTickets.findMany({
|
||||
where: { userId },
|
||||
orderBy: { createdAt: "desc" },
|
||||
take: 100,
|
||||
});
|
||||
tickets = await db
|
||||
.select()
|
||||
.from(WebsiteHelpCenterTickets)
|
||||
.where(eq(WebsiteHelpCenterTickets.userId, userId))
|
||||
.orderBy(desc(WebsiteHelpCenterTickets.createdAt))
|
||||
.limit(100);
|
||||
} catch {
|
||||
tickets = [];
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user