2026-05-16 12:02:15 +01:00

56 lines
1.6 KiB
TypeScript

import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { config as loadEnv } from 'dotenv';
// Load repo-root .env so DATABASE_URL is visible when this script runs from
// any CWD (pnpm invokes it from packages/db).
const here = path.dirname(fileURLToPath(import.meta.url));
loadEnv({ path: path.resolve(here, '../../../.env') });
const { PrismaClient, UserRole } = await import('@prisma/client');
const prisma = new PrismaClient();
const DEMO_TENANT_NAME = 'Demo Factory';
const DEMO_ADMIN_EMAIL = 'admin@demo.local';
async function main() {
// Idempotent: if a prior run created the demo tenant, wipe it and recreate.
// Cascade deletes on the relations handle the children.
const existing = await prisma.tenant.findFirst({ where: { name: DEMO_TENANT_NAME } });
if (existing) {
await prisma.tenant.delete({ where: { id: existing.id } });
}
const tenant = await prisma.tenant.create({
data: { name: DEMO_TENANT_NAME },
});
await prisma.user.create({
data: {
tenantId: tenant.id,
email: DEMO_ADMIN_EMAIL,
role: UserRole.ADMIN,
},
});
await prisma.workstation.createMany({
data: [
{ tenantId: tenant.id, code: 'WS-001', name: 'Assembly A', area: 'Floor 1' },
{ tenantId: tenant.id, code: 'WS-002', name: 'Packaging B', area: 'Floor 2' },
],
});
console.warn(
`Seed complete — tenant=${tenant.id} (${tenant.name}), admin=${DEMO_ADMIN_EMAIL}, workstations=2`,
);
}
main()
.catch((err) => {
console.error('Seed failed:', err);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});