Added seed function

This commit is contained in:
lucast committed 2026-11-20 18:52:31 +01:00
1 parent 6b6cb90f72
commit 57e4827d42
3 files changed
+153 -1

No files matched your search

+149
View File
@@ -0,0 +1,149 @@
import "dotenv/config";
import { PrismaPg } from "@prisma/adapter-pg";
import { Pool } from "pg";
import { PrismaClient } from "../generated/prisma/client";
const connectionString = process.env.DATABASE_URL;
if (!connectionString) {
throw new Error("DATABASE_URL must be set to run the Prisma seed.");
}
const pool = new Pool({ connectionString });
const adapter = new PrismaPg(pool);
const prisma = new PrismaClient({ adapter });
async function main() {
const customers = await Promise.all([
prisma.customer.upsert({
where: { email: "anna@example.com" },
update: {},
create: { name: "Anna Jensen", email: "anna@example.com", phone: "+45 20 11 22 33" },
}),
prisma.customer.upsert({
where: { email: "mikkel@example.com" },
update: {},
create: { name: "Mikkel Sørensen", email: "mikkel@example.com", phone: "+45 30 44 55 66" },
}),
]);
const [volvo, toyota] = await Promise.all([
prisma.carBrand.upsert({ where: { name: "Volvo" }, update: {}, create: { name: "Volvo" } }),
prisma.carBrand.upsert({ where: { name: "Toyota" }, update: {}, create: { name: "Toyota" } }),
]);
const [xc60, corolla] = await Promise.all([
prisma.carModel.upsert({
where: { carBrandId_name: { carBrandId: volvo.id, name: "XC60" } },
update: {},
create: { name: "XC60", carBrandId: volvo.id },
}),
prisma.carModel.upsert({
where: { carBrandId_name: { carBrandId: toyota.id, name: "Corolla" } },
update: {},
create: { name: "Corolla", carBrandId: toyota.id },
}),
]);
const cars = await Promise.all([
prisma.car.upsert({
where: { licensePlate: "AB12345" },
update: {},
create: { licensePlate: "AB12345", productionYear: 2021, carModelId: xc60.id, customerId: customers[0].id },
}),
prisma.car.upsert({
where: { licensePlate: "CD67890" },
update: {},
create: { licensePlate: "CD67890", productionYear: 2018, carModelId: corolla.id, customerId: customers[1].id },
}),
]);
const skillNames = ["Diagnostics", "Brakes", "Engine"];
const skills = await Promise.all(skillNames.map((name) =>
prisma.mechanicSkill.upsert({ where: { name }, update: {}, create: { name } }),
));
const mechanicData = [
{ name: "Sofie Nielsen", hourlyRate: 425, skills: ["Diagnostics", "Engine"] },
{ name: "Lars Holm", hourlyRate: 390, skills: ["Brakes", "Diagnostics"] },
];
const mechanics = await Promise.all(mechanicData.map(async (data) => {
let mechanic = await prisma.mechanic.findFirst({ where: { name: data.name } });
mechanic ??= await prisma.mechanic.create({ data: { name: data.name, hourlyRate: data.hourlyRate } });
const mechanicSkills = skills.filter((skill) => data.skills.includes(skill.name));
await Promise.all(mechanicSkills.map((skill) => prisma.mechanicToSkill.upsert({
where: { mechanicId_skillId: { mechanicId: mechanic.id, skillId: skill.id } },
update: {},
create: { mechanicId: mechanic.id, skillId: skill.id },
})));
return mechanic;
}));
const supplier = await prisma.supplier.findFirst({ where: { name: "Nordic Auto Parts" } })
?? await prisma.supplier.create({ data: { name: "Nordic Auto Parts" } });
const [oilFilter, brakePads] = await Promise.all([
prisma.sparePart.upsert({
where: { supplierId_partNumber: { supplierId: supplier.id, partNumber: "OF-204" } },
update: {},
create: { name: "Oil filter", partNumber: "OF-204", supplierId: supplier.id },
}),
prisma.sparePart.upsert({
where: { supplierId_partNumber: { supplierId: supplier.id, partNumber: "BP-118" } },
update: {},
create: { name: "Brake pads", partNumber: "BP-118", supplierId: supplier.id },
}),
]);
const workOrderData = [
{ carId: cars[0].id, currentMileage: 48_200, problem: "Annual service and oil change", status: "IN_PROGRESS" as const },
{ carId: cars[1].id, currentMileage: 92_450, problem: "Inspect and replace worn front brakes", status: "OPEN" as const },
];
for (const [index, data] of workOrderData.entries()) {
let workOrder = await prisma.workOrder.findFirst({ where: { carId: data.carId, problem: data.problem } });
if (!workOrder) {
workOrder = await prisma.workOrder.create({ data });
}
const mechanic = mechanics[index];
if (mechanic) {
await prisma.workOrderToMechanic.upsert({
where: { mechanicId_workOrderId: { mechanicId: mechanic.id, workOrderId: workOrder.id } },
update: {},
create: { mechanicId: mechanic.id, workOrderId: workOrder.id, minutesWorked: index === 0 ? 75 : 0 },
});
}
const part = index === 0 ? oilFilter : brakePads;
await prisma.workOrderToSparePart.upsert({
where: { sparePartId_workOrderId: { sparePartId: part.id, workOrderId: workOrder.id } },
update: {},
create: {
sparePartId: part.id,
workOrderId: workOrder.id,
quantity: 1,
pricePerUnit: index === 0 ? 120 : 680,
},
});
if (index === 0) {
await prisma.invoice.upsert({
where: { workOrderId: workOrder.id },
update: {},
create: { workOrderId: workOrder.id, total: 1_250 },
});
}
}
console.log("Seed data is ready.");
}
main()
.catch((error: unknown) => {
console.error("Failed to seed the database:", error);
process.exitCode = 1;
})
.finally(async () => {
await prisma.$disconnect();
await pool.end();
});