refactor(db): migrate app pages and APIs from Prisma facade to Drizzle (3)
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
2cd0863cb8
commit
580972c0a0
40 files changed
+1089
-808
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { desc } from "drizzle-orm";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import {
|
||||
createApiKey,
|
||||
@@ -6,8 +7,8 @@ import {
|
||||
} from "@/actions/admin-radio-api-keys";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { db, RadioApiKeys } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const metadata = { title: "Radio API Keys" };
|
||||
|
||||
@@ -34,22 +35,21 @@ export default async function AdminRadioApiKeysPage() {
|
||||
let dbError = false;
|
||||
|
||||
try {
|
||||
keys = await prisma.radioApiKeys
|
||||
.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
key: true,
|
||||
allowedIps: true,
|
||||
rateLimit: true,
|
||||
lastUsedAt: true,
|
||||
expiresAt: true,
|
||||
isActive: true,
|
||||
createdAt: true,
|
||||
},
|
||||
orderBy: [{ isActive: "desc" }, { id: "desc" }],
|
||||
take: 500,
|
||||
keys = await db
|
||||
.select({
|
||||
id: RadioApiKeys.id,
|
||||
name: RadioApiKeys.name,
|
||||
key: RadioApiKeys.key,
|
||||
allowedIps: RadioApiKeys.allowedIps,
|
||||
rateLimit: RadioApiKeys.rateLimit,
|
||||
lastUsedAt: RadioApiKeys.lastUsedAt,
|
||||
expiresAt: RadioApiKeys.expiresAt,
|
||||
isActive: RadioApiKeys.isActive,
|
||||
createdAt: RadioApiKeys.createdAt,
|
||||
})
|
||||
.from(RadioApiKeys)
|
||||
.orderBy(desc(RadioApiKeys.isActive), desc(RadioApiKeys.id))
|
||||
.limit(500)
|
||||
.catch(() => {
|
||||
dbError = true;
|
||||
return [];
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { asc } from "drizzle-orm";
|
||||
import Image from "next/image";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import {
|
||||
@@ -7,8 +8,8 @@ import {
|
||||
} from "@/actions/admin-radio-autodj";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { db, RadioAutoDjPlaylist } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export const metadata = { title: "Radio AutoDJ" };
|
||||
|
||||
@@ -39,22 +40,24 @@ export default async function AdminRadioAutoDjPage() {
|
||||
let dbError = false;
|
||||
|
||||
try {
|
||||
tracks = await prisma.radioAutoDjPlaylist
|
||||
.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
artist: true,
|
||||
album: true,
|
||||
artworkUrl: true,
|
||||
duration: true,
|
||||
playCount: true,
|
||||
lastPlayedAt: true,
|
||||
isActive: true,
|
||||
sortOrder: true,
|
||||
},
|
||||
orderBy: [{ sortOrder: "asc" }, { title: "asc" }],
|
||||
tracks = await db
|
||||
.select({
|
||||
id: RadioAutoDjPlaylist.id,
|
||||
title: RadioAutoDjPlaylist.title,
|
||||
artist: RadioAutoDjPlaylist.artist,
|
||||
album: RadioAutoDjPlaylist.album,
|
||||
artworkUrl: RadioAutoDjPlaylist.artworkUrl,
|
||||
duration: RadioAutoDjPlaylist.duration,
|
||||
playCount: RadioAutoDjPlaylist.playCount,
|
||||
lastPlayedAt: RadioAutoDjPlaylist.lastPlayedAt,
|
||||
isActive: RadioAutoDjPlaylist.isActive,
|
||||
sortOrder: RadioAutoDjPlaylist.sortOrder,
|
||||
})
|
||||
.from(RadioAutoDjPlaylist)
|
||||
.orderBy(
|
||||
asc(RadioAutoDjPlaylist.sortOrder),
|
||||
asc(RadioAutoDjPlaylist.title),
|
||||
)
|
||||
.catch(() => {
|
||||
dbError = true;
|
||||
return [];
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { asc } from "drizzle-orm";
|
||||
import NextImage from "next/image";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import {
|
||||
@@ -7,8 +8,8 @@ import {
|
||||
} from "@/actions/admin-radio-extra";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { db, RadioBanners } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
type Banner = {
|
||||
id: bigint;
|
||||
@@ -28,19 +29,19 @@ export default async function AdminRadioBannersPage() {
|
||||
let dbError = false;
|
||||
|
||||
try {
|
||||
banners = await prisma.radioBanners.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
imagePath: true,
|
||||
title: true,
|
||||
description: true,
|
||||
sortOrder: true,
|
||||
isActive: true,
|
||||
createdAt: true,
|
||||
},
|
||||
orderBy: [{ sortOrder: "asc" }, { id: "asc" }],
|
||||
});
|
||||
banners = await db
|
||||
.select({
|
||||
id: RadioBanners.id,
|
||||
userId: RadioBanners.userId,
|
||||
imagePath: RadioBanners.imagePath,
|
||||
title: RadioBanners.title,
|
||||
description: RadioBanners.description,
|
||||
sortOrder: RadioBanners.sortOrder,
|
||||
isActive: RadioBanners.isActive,
|
||||
createdAt: RadioBanners.createdAt,
|
||||
})
|
||||
.from(RadioBanners)
|
||||
.orderBy(asc(RadioBanners.sortOrder), asc(RadioBanners.id));
|
||||
} catch {
|
||||
dbError = true;
|
||||
banners = [];
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { desc, inArray } from "drizzle-orm";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { db, RadioHistory, User } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
type History = {
|
||||
id: bigint;
|
||||
@@ -31,28 +32,28 @@ export default async function AdminRadioHistoryPage() {
|
||||
let dbError = false;
|
||||
|
||||
try {
|
||||
history = await prisma.radioHistory.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
showName: true,
|
||||
startedAt: true,
|
||||
endedAt: true,
|
||||
listenersCount: true,
|
||||
notes: true,
|
||||
},
|
||||
orderBy: { startedAt: "desc" },
|
||||
take: 100,
|
||||
});
|
||||
history = await db
|
||||
.select({
|
||||
id: RadioHistory.id,
|
||||
userId: RadioHistory.userId,
|
||||
showName: RadioHistory.showName,
|
||||
startedAt: RadioHistory.startedAt,
|
||||
endedAt: RadioHistory.endedAt,
|
||||
listenersCount: RadioHistory.listenersCount,
|
||||
notes: RadioHistory.notes,
|
||||
})
|
||||
.from(RadioHistory)
|
||||
.orderBy(desc(RadioHistory.startedAt))
|
||||
.limit(100);
|
||||
|
||||
const ids = Array.from(
|
||||
new Set(history.map((h) => Number(h.userId))),
|
||||
).filter((n) => Number.isFinite(n) && n > 0);
|
||||
if (ids.length > 0) {
|
||||
const users = await prisma.user.findMany({
|
||||
where: { id: { in: ids } },
|
||||
select: { id: true, username: true },
|
||||
});
|
||||
const users = await db
|
||||
.select({ id: User.id, username: User.username })
|
||||
.from(User)
|
||||
.where(inArray(User.id, ids));
|
||||
names = new Map(users.map((u) => [String(u.id), u.username]));
|
||||
}
|
||||
} catch {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { desc } from "drizzle-orm";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { deleteShout } from "@/actions/admin-radio-moderation";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { db, RadioShouts } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
type Shout = {
|
||||
id: bigint;
|
||||
@@ -18,17 +19,17 @@ export default async function AdminRadioModerationPage() {
|
||||
|
||||
let shouts: Shout[] = [];
|
||||
try {
|
||||
shouts = await prisma.radioShouts.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
message: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
},
|
||||
orderBy: { id: "desc" },
|
||||
take: 50,
|
||||
});
|
||||
shouts = await db
|
||||
.select({
|
||||
id: RadioShouts.id,
|
||||
userId: RadioShouts.userId,
|
||||
message: RadioShouts.message,
|
||||
createdAt: RadioShouts.createdAt,
|
||||
updatedAt: RadioShouts.updatedAt,
|
||||
})
|
||||
.from(RadioShouts)
|
||||
.orderBy(desc(RadioShouts.id))
|
||||
.limit(50);
|
||||
} catch {
|
||||
shouts = [];
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { desc } from "drizzle-orm";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { deleteShout } from "@/actions/admin-radio-moderation";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { db, RadioApplications, RadioSchedules, RadioShouts } from "@/lib/db";
|
||||
import { formatDate } from "@/lib/format-date";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
type Shout = {
|
||||
id: bigint;
|
||||
@@ -52,48 +53,53 @@ export default async function AdminRadioPage() {
|
||||
let schedules: Schedule[] = [];
|
||||
|
||||
try {
|
||||
shouts = await prisma.radioShouts.findMany({
|
||||
select: { id: true, userId: true, message: true, createdAt: true },
|
||||
orderBy: { id: "desc" },
|
||||
take: 25,
|
||||
});
|
||||
shouts = await db
|
||||
.select({
|
||||
id: RadioShouts.id,
|
||||
userId: RadioShouts.userId,
|
||||
message: RadioShouts.message,
|
||||
createdAt: RadioShouts.createdAt,
|
||||
})
|
||||
.from(RadioShouts)
|
||||
.orderBy(desc(RadioShouts.id))
|
||||
.limit(25);
|
||||
} catch {
|
||||
shouts = [];
|
||||
}
|
||||
|
||||
try {
|
||||
applications = await prisma.radioApplications.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
realName: true,
|
||||
age: true,
|
||||
status: true,
|
||||
musicStyle: true,
|
||||
discordUsername: true,
|
||||
createdAt: true,
|
||||
},
|
||||
orderBy: { id: "desc" },
|
||||
take: 25,
|
||||
});
|
||||
applications = await db
|
||||
.select({
|
||||
id: RadioApplications.id,
|
||||
userId: RadioApplications.userId,
|
||||
realName: RadioApplications.realName,
|
||||
age: RadioApplications.age,
|
||||
status: RadioApplications.status,
|
||||
musicStyle: RadioApplications.musicStyle,
|
||||
discordUsername: RadioApplications.discordUsername,
|
||||
createdAt: RadioApplications.createdAt,
|
||||
})
|
||||
.from(RadioApplications)
|
||||
.orderBy(desc(RadioApplications.id))
|
||||
.limit(25);
|
||||
} catch {
|
||||
applications = [];
|
||||
}
|
||||
|
||||
try {
|
||||
schedules = await prisma.radioSchedules.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
userId: true,
|
||||
day: true,
|
||||
startTime: true,
|
||||
endTime: true,
|
||||
showName: true,
|
||||
isActive: true,
|
||||
},
|
||||
orderBy: { id: "desc" },
|
||||
take: 25,
|
||||
});
|
||||
schedules = await db
|
||||
.select({
|
||||
id: RadioSchedules.id,
|
||||
userId: RadioSchedules.userId,
|
||||
day: RadioSchedules.day,
|
||||
startTime: RadioSchedules.startTime,
|
||||
endTime: RadioSchedules.endTime,
|
||||
showName: RadioSchedules.showName,
|
||||
isActive: RadioSchedules.isActive,
|
||||
})
|
||||
.from(RadioSchedules)
|
||||
.orderBy(desc(RadioSchedules.id))
|
||||
.limit(25);
|
||||
} catch {
|
||||
schedules = [];
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { asc } from "drizzle-orm";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import {
|
||||
createRadioRank,
|
||||
@@ -6,7 +7,7 @@ import {
|
||||
} from "@/actions/admin-radio-extra";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { db, RadioRanks } from "@/lib/db";
|
||||
|
||||
type Rank = {
|
||||
id: bigint;
|
||||
@@ -23,16 +24,16 @@ export default async function AdminRadioRanksPage() {
|
||||
let dbError = false;
|
||||
|
||||
try {
|
||||
ranks = await prisma.radioRanks.findMany({
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
badgeCode: true,
|
||||
isActive: true,
|
||||
},
|
||||
orderBy: { id: "asc" },
|
||||
});
|
||||
ranks = await db
|
||||
.select({
|
||||
id: RadioRanks.id,
|
||||
name: RadioRanks.name,
|
||||
description: RadioRanks.description,
|
||||
badgeCode: RadioRanks.badgeCode,
|
||||
isActive: RadioRanks.isActive,
|
||||
})
|
||||
.from(RadioRanks)
|
||||
.orderBy(asc(RadioRanks.id));
|
||||
} catch {
|
||||
dbError = true;
|
||||
ranks = [];
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
} from "@/actions/admin-radio-extra";
|
||||
import { StatusCard } from "@/components/admin/dashboard";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { db, WebsiteSetting } from "@/lib/db";
|
||||
|
||||
type Field = { key: string; comment: string };
|
||||
type Group = { title: string; fields: Field[] };
|
||||
@@ -364,9 +364,13 @@ export default async function AdminRadioSettingsPage() {
|
||||
let extra: Field[] = [];
|
||||
|
||||
try {
|
||||
const rows = await prisma.websiteSetting.findMany({
|
||||
select: { key: true, value: true, comment: true },
|
||||
});
|
||||
const rows = await db
|
||||
.select({
|
||||
key: WebsiteSetting.key,
|
||||
value: WebsiteSetting.value,
|
||||
comment: WebsiteSetting.comment,
|
||||
})
|
||||
.from(WebsiteSetting);
|
||||
values = new Map(rows.map((r) => [r.key, r.value]));
|
||||
extra = rows
|
||||
.filter(
|
||||
|
||||
Reference in new issue
Block a user