Files
metabuilder/postgres/scripts/seed-admin.ts
johndoe6345789 a51130a127 feat: Add external low-code and postgres repositories
- codegen: Low-code React app with JSON-driven component system
- packagerepo: Schema-driven package repository with backend/frontend
- postgres: Next.js app with Drizzle ORM and PostgreSQL

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-21 16:48:52 +00:00

54 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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.
import * as bcrypt from 'bcryptjs';
import { drizzle } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';
import { adminUserSchema } from '../src/models/Schema';
import { generateSecurePassword } from './generate-password';
async function seedAdminUser() {
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
const db = drizzle(pool);
const username = process.env.ADMIN_USERNAME || 'admin';
// Generate secure password if not provided
let password = process.env.ADMIN_PASSWORD;
let passwordGenerated = false;
if (!password) {
password = generateSecurePassword(32);
passwordGenerated = true;
}
const passwordHash = await bcrypt.hash(password, 10);
try {
await db.insert(adminUserSchema).values({
username,
passwordHash,
});
console.log('✅ Admin user created successfully!');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log(`📧 Username: ${username}`);
console.log(`🔑 Password: ${password}`);
if (passwordGenerated) {
console.log('⚠️ This password was auto-generated. Save it securely!');
}
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('🌐 Login at: http://localhost:3000/admin/login');
} catch (error: any) {
if (error.code === '23505') {
console.log(' Admin user already exists');
} else {
console.error('❌ Error creating admin user:', error);
}
} finally {
await pool.end();
}
}
seedAdminUser();