UNPKG

get-express-starter

Version:

Get production ready express boilerplate with a single command

53 lines (42 loc) 1.27 kB
import readline from 'node:readline'; import { env } from '@/config'; import User from '@/models/user.model'; import type { IUser } from '@/types'; import mongoose from 'mongoose'; const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const adminDetails: Partial<IUser> = { name: 'Admin', role: 'admin', }; async function seedDatabase() { try { await mongoose.connect(env.mongoose.url); console.log('Database connected'); const existingAdmin = await User.findOne({ role: 'admin' }); if (existingAdmin) { console.log('Existing admin found. Removing the current admin...'); await User.deleteOne({ role: 'admin' }); console.log('Existing admin removed.'); } await User.create(adminDetails); console.log('Admin user created successfully!'); } catch (err) { console.error('Error seeding database: ', err); } finally { await mongoose.connection.close(); } } 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();