get-express-starter
Version:
Get production ready express boilerplate with a single command
66 lines (53 loc) • 1.61 kB
text/typescript
import prisma from '@/lib/prisma';
import { hashPassword } from '@/utils/passwordHash';
import type { UserRole } from '@prisma/client';
import readline from 'node:readline';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const adminDetails: Partial<{ email: string; password: string; name: string; role: UserRole }> = {
name: 'Admin',
role: 'ADMIN',
};
async function seedDatabase() {
try {
console.log('Seeding database...');
const existingAdmin = await prisma.user.findFirst({
where: { role: 'ADMIN' },
});
if (existingAdmin) {
console.log('Existing admin found. Removing the current admin...');
await prisma.user.delete({
where: { id: existingAdmin.id },
});
console.log('Existing admin removed.');
}
// Hash the password before saving
const hashedPassword = await hashPassword(adminDetails.password!);
await prisma.user.create({
data: {
name: adminDetails.name!,
role: adminDetails.role!,
email: adminDetails.email!,
password: hashedPassword,
},
});
console.log('Admin user created successfully!');
} catch (err) {
console.error('Error seeding database:', err);
} finally {
await prisma.$disconnect();
}
}
async function promptForAdminDetails() {
rl.question('Enter admin email: ', (email) => {
adminDetails.email = email;
rl.question('Enter admin password: ', (password) => {
adminDetails.password = password;
seedDatabase();
rl.close();
});
});
}
promptForAdminDetails();