refactor(db): migrate app pages and APIs from Prisma facade to Drizzle (3)

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
SimoandCursor committed 2026-08-01 14:15:32 +02:00
1 parent 2cd0863cb8
commit 580972c0a0
40 files changed
+1089 -808

No files matched your search

+23 -16
View File
@@ -1,11 +1,12 @@
import { sql } from "drizzle-orm";
import { redirect } from "next/navigation";
import { getTranslations } from "next-intl/server";
import { AdminSimplePager } from "@/components/admin/admin-simple-pager";
import { StatusCard } from "@/components/admin/dashboard";
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
import { db } from "@/lib/db";
import { formatDate } from "@/lib/format-date";
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
import { prisma } from "@/lib/prisma";
type SubRow = {
id: number;
@@ -14,6 +15,19 @@ type SubRow = {
expiresAt: number;
};
type CountRow = { c: number | bigint };
type SubSqlRow = {
id: number;
user_id: number | null;
subscription_type: string | null;
expires_at: number;
};
async function rawRows<T>(query: ReturnType<typeof sql>): Promise<T[]> {
const [rows] = (await db.execute(query)) as unknown as [T[], unknown];
return rows ?? [];
}
export default async function AdminSubscriptions({
searchParams,
}: {
@@ -36,39 +50,32 @@ export default async function AdminSubscriptions({
let loadError = false;
try {
const [countRows, typeRows, soonRows] = await Promise.all([
prisma.$queryRaw<{ c: bigint }[]>`
rawRows<CountRow>(sql`
SELECT COUNT(*) AS c
FROM users_subscriptions
WHERE active = 1
AND COALESCE(timestamp_start, 0) + COALESCE(duration, 0) > ${now}
`,
prisma.$queryRaw<{ c: bigint }[]>`
`),
rawRows<CountRow>(sql`
SELECT COUNT(DISTINCT subscription_type) AS c
FROM users_subscriptions
WHERE active = 1
AND COALESCE(timestamp_start, 0) + COALESCE(duration, 0) > ${now}
`,
prisma.$queryRaw<{ c: bigint }[]>`
`),
rawRows<CountRow>(sql`
SELECT COUNT(*) AS c
FROM users_subscriptions
WHERE active = 1
AND COALESCE(timestamp_start, 0) + COALESCE(duration, 0) > ${now}
AND COALESCE(timestamp_start, 0) + COALESCE(duration, 0) <= ${now + 7 * 24 * 60 * 60}
`,
`),
]);
total = Number(countRows[0]?.c ?? 0);
types = Number(typeRows[0]?.c ?? 0);
expiringSoon = Number(soonRows[0]?.c ?? 0);
const pagination = calcPagination(total, parsed.page, parsed.perPage);
const rows = await prisma.$queryRaw<
{
id: number;
user_id: number | null;
subscription_type: string | null;
expires_at: number;
}[]
>`
const rows = await rawRows<SubSqlRow>(sql`
SELECT id,
user_id,
subscription_type,
@@ -78,7 +85,7 @@ export default async function AdminSubscriptions({
AND COALESCE(timestamp_start, 0) + COALESCE(duration, 0) > ${now}
ORDER BY expires_at ASC
LIMIT ${pagination.perPage} OFFSET ${pagination.offset}
`;
`);
active = rows.map((r) => ({
id: Number(r.id),