Files
desa-darmasaba/prisma/safeseedUnique.ts
nico 171d7f7947 fix(biome-lint): resolve critical and medium priority lint issues
CRITICAL FIXES:
- Fix noAsyncPromiseExecutor in xcoba.ts and xcoba2.ts
  * Removed async promise executor pattern
  * Refactored to proper promise chain with .then()/.catch()
  * Added proper error handling for unhandled rejections

- Fix useIterableCallbackReturn in seed_berita.ts
  * Replaced forEach with for...of loop to avoid returning values in callbacks

MEDIUM FIXES:
- Fix useNodejsImportProtocol (728 files auto-fixed)
  * Updated Node.js builtin imports to use node: protocol
  * Files: eslint.config.mjs, vitest.config.ts, zgen/image.ts, and 725+ more

- Fix useOptionalChain in xcoba.ts (auto-fixed)
  * Changed 'resOut && resOut.body' to 'resOut?.body'

- Fix noImportantStyles in dark-mode-table.css
  * Added biome-ignore suppression comments with justification
  * Required to override Mantine UI library styles

- Fix noUselessContinue in find-port.ts (auto-fixed)
  * Removed unnecessary continue statement

- Fix useLiteralKeys (700+ files auto-fixed)
  * Simplified computed expressions to use literal keys
  * Example: obj['create'] -> obj.create

RESULTS:
- Errors reduced: 4,516 → 3,521 (-22%)
- Warnings reduced: 3,861 → 2,083 (-46%)
- Total issues reduced: 8,991 → 6,115 (-32%)
- 735 files auto-fixed by biome lint --fix

Remaining issues (~6,115):
- Mostly noExplicitAny warnings requiring manual refactoring
- Will be addressed in gradual code quality improvements

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2026-04-09 17:27:17 +08:00

135 lines
3.6 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import prisma from "@/lib/prisma";
import type { PrismaClient } from "@prisma/client";
type SafeSeedOptions = {
skipUpdate?: boolean;
silent?: boolean; // Opsional: untuk suppress log
};
/**
* 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>,
data: Record<string, any>,
options: SafeSeedOptions = {}
) {
const m = prisma[model] as any;
if (!m) {
throw new Error(`❌ Model ${String(model)} tidak ditemukan di Prisma Client`);
}
try {
const result = await m.upsert({
where,
update: options.skipUpdate ? {} : data,
create: data,
});
if (!options.silent) {
console.log(`✅ Seeded ${String(model)}:`, where);
}
return result;
} 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";
// 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);
// }
// }