Tambah seeder di bagian landing page

This commit is contained in:
2026-01-06 17:54:21 +08:00
parent daaed8089b
commit 503da91ce6
11 changed files with 541 additions and 222 deletions

View File

@@ -1,30 +1,63 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// helpers/safeSeedUnique.ts
import prisma from "@/lib/prisma";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
type SafeSeedOptions = {
skipUpdate?: boolean;
};
/**
* Helper generic buat seed dengan upsert aman
*/
// prisma/safeseedUnique.ts
export async function safeSeedUnique<T extends keyof PrismaClient>(
model: T,
where: Record<string, any>,
data: Record<string, any>
data: Record<string, any>,
options: SafeSeedOptions = {}
) {
const m = prisma[model];
if (!m) throw new Error(`Model ${String(model)} tidak ditemukan di PrismaClient`);
const m = prisma[model] as any;
if (!m) throw new Error(`Model ${String(model)} tidak ditemukan`);
try {
// @ts-expect-error upsert dynamic
await m.upsert({
// Pastikan `where` berisi field yang benar-benar unique (misal: `id`)
const result = await m.upsert({
where,
update: data,
create: { ...where, ...data },
update: options.skipUpdate ? {} : data,
create: data, // ✅ Jangan duplikasi `where` ke `create`
});
console.log(`✅ Seeded ${String(model)} -> ${JSON.stringify(where)}`);
console.log(`✅ Seed ${String(model)}:`, where);
return result;
} catch (err) {
console.error(`❌ Gagal seed ${String(model)} -> ${JSON.stringify(where)}`, err);
console.error(`❌ Gagal seed ${String(model)}:`, where, err);
throw err; // ✅ Rethrow agar seeding berhenti jika kritis
}
}
// /* eslint-disable @typescript-eslint/no-explicit-any */
// import { PrismaClient } from "@prisma/client";
// const prisma = new PrismaClient();
// type SafeSeedOptions = {
// skipUpdate?: boolean;
// };
// export async function safeSeedUnique<T extends keyof PrismaClient>(
// model: T,
// where: Record<string, any>,
// data: Record<string, any>,
// options: SafeSeedOptions = {}
// ) {
// const m = prisma[model] as any;
// if (!m) throw new Error(`Model ${String(model)} tidak ditemukan`);
// try {
// await m.upsert({
// where,
// update: options.skipUpdate ? {} : data,
// create: { ...where, ...data },
// });
// console.log(`✅ Seed ${String(model)}:`, where);
// } catch (err) {
// console.error(`❌ Gagal seed ${String(model)}:`, where, err);
// }
// }