79 lines
2.0 KiB
TypeScript
79 lines
2.0 KiB
TypeScript
// prisma/seeds/CrimeCategoriesSeeder.ts
|
|
import { generateId } from "../../app/_utils/common";
|
|
import { PrismaClient } from "@prisma/client";
|
|
import { crimeCategoriesData } from '../data/jsons/crime-category';
|
|
|
|
import path from 'path';
|
|
import XLSX from 'xlsx';
|
|
|
|
interface ICrimeCategory {
|
|
id: string;
|
|
name: string;
|
|
description: string;
|
|
type: string;
|
|
created_at: Date;
|
|
updated_at: Date;
|
|
}
|
|
|
|
export class CrimeCategoriesSeeder {
|
|
constructor(private prisma: PrismaClient) {}
|
|
|
|
async run(): Promise<void> {
|
|
console.log('Seeding crime categories...');
|
|
|
|
// Hapus data yang ada untuk menghindari duplikasi
|
|
await this.prisma.crime_categories.deleteMany({});
|
|
|
|
// Truncate table jika diperlukan
|
|
// await this.prisma.$executeRaw`TRUNCATE TABLE "crime_categories" CASCADE`;
|
|
|
|
const filePath = path.join(
|
|
__dirname,
|
|
'../data/excels/others/crime_categories.xlsx'
|
|
);
|
|
const workbook = XLSX.readFile(filePath);
|
|
const sheet = workbook.Sheets[workbook.SheetNames[0]];
|
|
const data = XLSX.utils.sheet_to_json(sheet) as ICrimeCategory[];
|
|
|
|
for (const category of crimeCategoriesData) {
|
|
const newId = generateId({
|
|
prefix: 'CC',
|
|
segments: {
|
|
sequentialDigits: 4,
|
|
},
|
|
randomSequence: false,
|
|
uniquenessStrategy: 'counter',
|
|
separator: '-',
|
|
});
|
|
|
|
await this.prisma.crime_categories.create({
|
|
data: {
|
|
id: newId.trim(),
|
|
name: category.name,
|
|
description: category.description,
|
|
},
|
|
});
|
|
|
|
console.log(`Seeding crime category: ${category.name}`);
|
|
}
|
|
|
|
// Update existing records
|
|
for (const row of data) {
|
|
const id = row['id'].trim();
|
|
const name = row['name'].trim();
|
|
const type = row['type'].trim();
|
|
|
|
await this.prisma.crime_categories.updateMany({
|
|
where: { id },
|
|
data: {
|
|
type,
|
|
},
|
|
});
|
|
|
|
console.log(`Updating crime category: ${name} to type ${type}`);
|
|
}
|
|
|
|
console.log(`✅ ${crimeCategoriesData.length} crime categories seeded`);
|
|
}
|
|
}
|