Files
2026-04-20 15:49:01 -05:00

47 lines
1.2 KiB
TypeScript

import { PrismaClient } from "@prisma/client";
import bcrypt from "bcryptjs";
const prisma = new PrismaClient();
async function main() {
const existingAdmin = await prisma.user.findFirst({ where: { role: "admin" } });
if (existingAdmin) {
console.log(`[seed] admin already exists (${existingAdmin.email ?? existingAdmin.id}); nothing to do`);
return;
}
const email = process.env.BOOTSTRAP_ADMIN_EMAIL;
const password = process.env.BOOTSTRAP_ADMIN_PASSWORD;
const name = process.env.BOOTSTRAP_ADMIN_NAME ?? "Administrator";
if (!email || !password) {
console.warn(
"[seed] BOOTSTRAP_ADMIN_EMAIL/PASSWORD not set — no admin created. Set them and rerun.",
);
return;
}
const passwordHash = await bcrypt.hash(password, 12);
const user = await prisma.user.create({
data: {
role: "admin",
name,
email,
passwordHash,
active: true,
},
});
console.log(`[seed] created bootstrap admin ${user.email} (id=${user.id})`);
console.log("[seed] IMPORTANT: change this password after first login.");
}
main()
.catch((err) => {
console.error("[seed] failed:", err);
process.exitCode = 1;
})
.finally(async () => {
await prisma.$disconnect();
});