Fix Seed Image 27 Jan
This commit is contained in:
@@ -4,9 +4,16 @@ import { PrismaClient } from "@prisma/client";
|
||||
|
||||
type SafeSeedOptions = {
|
||||
skipUpdate?: boolean;
|
||||
silent?: boolean; // Opsional: untuk suppress log
|
||||
};
|
||||
|
||||
// prisma/safeseedUnique.ts
|
||||
/**
|
||||
* Safely upsert data with error handling
|
||||
* @param model - Prisma model name
|
||||
* @param where - Unique identifier(s)
|
||||
* @param data - Full data object (will be used for create)
|
||||
* @param options - Additional options
|
||||
*/
|
||||
export async function safeSeedUnique<T extends keyof PrismaClient>(
|
||||
model: T,
|
||||
where: Record<string, any>,
|
||||
@@ -14,23 +21,87 @@ export async function safeSeedUnique<T extends keyof PrismaClient>(
|
||||
options: SafeSeedOptions = {}
|
||||
) {
|
||||
const m = prisma[model] as any;
|
||||
if (!m) throw new Error(`Model ${String(model)} tidak ditemukan`);
|
||||
|
||||
if (!m) {
|
||||
throw new Error(`❌ Model ${String(model)} tidak ditemukan di Prisma Client`);
|
||||
}
|
||||
|
||||
try {
|
||||
// Pastikan `where` berisi field yang benar-benar unique (misal: `id`)
|
||||
const result = await m.upsert({
|
||||
where,
|
||||
update: options.skipUpdate ? {} : data,
|
||||
create: data, // ✅ Jangan duplikasi `where` ke `create`
|
||||
create: data,
|
||||
});
|
||||
console.log(`✅ Seed ${String(model)}:`, where);
|
||||
|
||||
if (!options.silent) {
|
||||
console.log(`✅ Seeded ${String(model)}:`, where);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (err) {
|
||||
console.error(`❌ Gagal seed ${String(model)}:`, where, err);
|
||||
throw err; // ✅ Rethrow agar seeding berhenti jika kritis
|
||||
} catch (err: any) {
|
||||
// Handle specific Prisma errors
|
||||
if (err.code === "P2002") {
|
||||
console.warn(`⚠️ Duplicate ${String(model)} (skipped):`, where);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (err.code === "P2003") {
|
||||
console.error(`❌ Foreign key constraint failed for ${String(model)}:`, where);
|
||||
console.error(" Missing relation:", err.meta?.field_name);
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (err.code === "P2025") {
|
||||
console.error(`❌ Record not found for ${String(model)}:`, where);
|
||||
throw err;
|
||||
}
|
||||
|
||||
// Log unexpected errors with full details
|
||||
console.error(`❌ Failed to seed ${String(model)}:`, where);
|
||||
console.error(" Error:", err.message);
|
||||
console.error(" Code:", err.code);
|
||||
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//ini yang bener pertama
|
||||
|
||||
// /* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
// import prisma from "@/lib/prisma";
|
||||
// import { PrismaClient } from "@prisma/client";
|
||||
|
||||
// type SafeSeedOptions = {
|
||||
// skipUpdate?: boolean;
|
||||
// };
|
||||
|
||||
// // prisma/safeseedUnique.ts
|
||||
// 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 {
|
||||
// // Pastikan `where` berisi field yang benar-benar unique (misal: `id`)
|
||||
// const result = await m.upsert({
|
||||
// where,
|
||||
// update: options.skipUpdate ? {} : data,
|
||||
// create: data, // ✅ Jangan duplikasi `where` ke `create`
|
||||
// });
|
||||
// console.log(`✅ Seed ${String(model)}:`, where);
|
||||
// return result;
|
||||
// } catch (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";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user