@@ -0,0 +1,50 @@
|
||||
import { config } from "dotenv";
|
||||
config();
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import { PrismaPg } from "@prisma/adapter-pg";
|
||||
import bcrypt from "bcryptjs";
|
||||
|
||||
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL! });
|
||||
const prisma = new PrismaClient({ adapter } as any);
|
||||
|
||||
async function main() {
|
||||
const [, , email, password, name] = process.argv;
|
||||
|
||||
if (!email || !password) {
|
||||
console.error("Usage: npm run db:create-user -- <email> <password> [name]");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (password.length < 8) {
|
||||
console.error("Password must be at least 8 characters long.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const hashedPassword = await bcrypt.hash(password, 12);
|
||||
const normalizedEmail = email.toLowerCase().trim();
|
||||
|
||||
const user = await prisma.user.upsert({
|
||||
where: { email: normalizedEmail },
|
||||
create: {
|
||||
email: normalizedEmail,
|
||||
password: hashedPassword,
|
||||
name: name ?? null,
|
||||
role: "ADMIN",
|
||||
},
|
||||
update: {
|
||||
password: hashedPassword,
|
||||
...(name ? { name } : {}),
|
||||
},
|
||||
});
|
||||
|
||||
console.log(`✅ User ready: ${user.email} (role: ${user.role})`);
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user