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

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
SimoandCursor committed 2026-08-01 14:15:36 +02:00
1 parent 580972c0a0
commit 9aa4f331bf
40 files changed
+1259 -927

No files matched your search

+20 -16
View File
@@ -2,8 +2,9 @@
// (guilds_members). AtomCMS JSON API parity. Returns { error } (404) when the
// id is unknown. The owner is the user_id column (accessor userId), surfaced
// here as ownerId.
import { count, eq } from "drizzle-orm";
import { apiJson } from "@/lib/api";
import { prisma } from "@/lib/prisma";
import { db, Guilds, GuildsMembers } from "@/lib/db";
export const dynamic = "force-dynamic";
@@ -19,18 +20,19 @@ export async function GET(
const guildId = Number(id);
try {
const guild = await prisma.guilds.findUnique({
where: { id: guildId },
select: {
id: true,
name: true,
description: true,
userId: true,
roomId: true,
badge: true,
dateCreated: true,
},
});
const [guild] = await db
.select({
id: Guilds.id,
name: Guilds.name,
description: Guilds.description,
userId: Guilds.userId,
roomId: Guilds.roomId,
badge: Guilds.badge,
dateCreated: Guilds.dateCreated,
})
.from(Guilds)
.where(eq(Guilds.id, guildId))
.limit(1);
if (!guild) {
return apiJson({ error: "Guild not found" }, { status: 404 });
@@ -39,9 +41,11 @@ export async function GET(
// Member count is a separate guarded query (no relation is modelled).
let memberCount = 0;
try {
memberCount = await prisma.guildsMembers.count({
where: { guildId: guild.id },
});
const [row] = await db
.select({ total: count() })
.from(GuildsMembers)
.where(eq(GuildsMembers.guildId, guild.id));
memberCount = row?.total ?? 0;
} catch {
memberCount = 0;
}
+16 -14
View File
@@ -2,8 +2,9 @@
// paginated list ordered newest first (by id), exposing the basic public
// fields. The owner is stored as the user_id column (accessor userId), surfaced
// here as ownerId for API clarity.
import { count, desc } from "drizzle-orm";
import { apiJson, pagination } from "@/lib/api";
import { prisma } from "@/lib/prisma";
import { db, Guilds } from "@/lib/db";
export const dynamic = "force-dynamic";
@@ -12,20 +13,21 @@ export async function GET(req: Request) {
const { page, perPage, skip, take } = pagination(sp);
try {
const [total, rows] = await Promise.all([
prisma.guilds.count(),
prisma.guilds.findMany({
orderBy: { id: "desc" },
skip,
take,
select: {
id: true,
name: true,
description: true,
userId: true,
},
}),
const [totalRows, rows] = await Promise.all([
db.select({ total: count() }).from(Guilds),
db
.select({
id: Guilds.id,
name: Guilds.name,
description: Guilds.description,
userId: Guilds.userId,
})
.from(Guilds)
.orderBy(desc(Guilds.id))
.limit(take)
.offset(skip),
]);
const total = totalRows[0]?.total ?? 0;
// Rename userId → ownerId for the public payload.
const data = rows.map(({ userId, ...rest }) => ({