Projektstart
This commit is contained in:
37
backend/prisma/seed.ts
Normal file
37
backend/prisma/seed.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import argon2 from "argon2";
|
||||
import { PrismaClient, UserRole } from "@prisma/client";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
const tenantName = process.env.SEED_TENANT ?? "Default Tenant";
|
||||
const email = process.env.SEED_EMAIL ?? "admin@example.com";
|
||||
const password = process.env.SEED_PASSWORD ?? "change-me-please";
|
||||
const role = (process.env.SEED_ROLE as UserRole | undefined) ?? "ADMIN";
|
||||
|
||||
const main = async () => {
|
||||
const existing = await prisma.user.findUnique({ where: { email } });
|
||||
if (existing) {
|
||||
process.stdout.write("Seed user already exists.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
const hash = await argon2.hash(password);
|
||||
const tenant = await prisma.tenant.create({
|
||||
data: { name: tenantName }
|
||||
});
|
||||
|
||||
await prisma.user.create({
|
||||
data: { tenantId: tenant.id, email, password: hash, role }
|
||||
});
|
||||
|
||||
process.stdout.write("Seeded tenant and user.\n");
|
||||
};
|
||||
|
||||
main()
|
||||
.catch((err) => {
|
||||
process.stderr.write(`${err instanceof Error ? err.message : String(err)}\n`);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user