Files
Vermix-Web/scripts/seed-admin.ts
T
2025-10-06 18:11:03 +02:00

60 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env tsx
import { config } from 'dotenv';
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import * as schema from '../src/schemas/database/schema';
import { hashPassword } from '../src/lib/utils/passwordHash';
// Load environment variables from .env file
config();
async function seedAdmin() {
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
throw new Error('DATABASE_URL environment variable is not set');
}
console.log('👤 Starting admin user seeding...');
const client = postgres(databaseUrl);
const db = drizzle(client, { schema });
try {
// Create admin user
console.log('👤 Creating admin user...');
const adminPasswordHash = await hashPassword('admin123');
const adminUser = await db
.insert(schema.managementUsers)
.values({
firstName: 'Admin',
lastName: 'User',
username: 'admin',
passwordHash: adminPasswordHash,
role: 'ADMIN',
enabled: true
})
.returning()
.onConflictDoNothing();
if (adminUser.length > 0) {
console.log('✅ Admin user created with username: admin, password: admin123');
} else {
console.log('️ Admin user already exists');
}
console.log('🎉 Admin user seeding completed successfully!');
} catch (error) {
console.error('❌ Error during admin seeding:', error);
process.exit(1);
} finally {
await client.end();
}
}
seedAdmin().catch((error) => {
console.error('❌ Admin seeding failed:', error);
process.exit(1);
});