subman-nextjs/src/app/lib/del.ts

34 lines
850 B
TypeScript
Raw Normal View History

"use server"
2024-06-19 17:46:30 +00:00
import { revalidatePath } from "next/cache";
import prisma from "./db";
2024-06-19 17:46:30 +00:00
import { redirect } from "next/navigation";
import { Pathname } from "app/types";
2024-06-27 14:08:03 +00:00
const tableMap = {
"/story": "story",
"/publication": "pub",
"/submission": "sub"
}
export async function deleteRecord(id: number, pathname: Pathname) {
2024-06-27 14:08:03 +00:00
const table = tableMap[pathname]
const res = await prisma[table].delete({ where: { id } })
console.log(`deleted from ${table}: ${res.id}`)
2024-06-27 10:44:31 +00:00
console.log("revalidating: " + pathname)
revalidatePath(pathname)
redirect(pathname)
2024-06-20 08:35:25 +00:00
}
2024-06-20 09:39:35 +00:00
2024-06-27 14:08:03 +00:00
export async function deleteRecords(ids: number[], pathname: "/story" | "/publication" | "/submission") {
const table = tableMap[pathname]
ids.forEach(async (id) => {
const res = await prisma.story.delete({
where: { id }
})
console.log(`deleted from ${table}: ${res.id}`)
})
2024-06-27 10:44:31 +00:00
2024-06-27 14:08:03 +00:00
}