init
This commit is contained in:
53
scripts/seed.ts
Normal file
53
scripts/seed.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||
import { Pool } from 'pg';
|
||||
import { users } from '../src/lib/server/db/schema.js';
|
||||
import bcrypt from 'bcrypt';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
||||
const db = drizzle(pool);
|
||||
|
||||
const ADMIN_EMAIL = process.env.SEED_ADMIN_EMAIL ?? 'admin@unicon.de';
|
||||
const ADMIN_PASSWORD = process.env.SEED_ADMIN_PASSWORD ?? 'ChangeMe123!';
|
||||
const ADMIN_FIRST = process.env.SEED_ADMIN_FIRST ?? 'Admin';
|
||||
const ADMIN_LAST = process.env.SEED_ADMIN_LAST ?? 'UniCon';
|
||||
|
||||
async function seed() {
|
||||
console.log('Seeding database...');
|
||||
|
||||
const existing = await db.select().from(users).where(eq(users.email, ADMIN_EMAIL)).limit(1);
|
||||
if (existing.length > 0) {
|
||||
console.log(`Admin user "${ADMIN_EMAIL}" already exists — skipping.`);
|
||||
await pool.end();
|
||||
return;
|
||||
}
|
||||
|
||||
const passwordHash = await bcrypt.hash(ADMIN_PASSWORD, 12);
|
||||
|
||||
await db.insert(users).values({
|
||||
firstName: ADMIN_FIRST,
|
||||
lastName: ADMIN_LAST,
|
||||
email: ADMIN_EMAIL,
|
||||
passwordHash,
|
||||
role: 'admin',
|
||||
bundesland: 'MV',
|
||||
standardStart: '08:00',
|
||||
standardEnd: '17:00',
|
||||
breakMinutesDefault: 30,
|
||||
weeklyHours: '40.0',
|
||||
vacationDaysPerYear: 30,
|
||||
active: true
|
||||
});
|
||||
|
||||
console.log(`✓ Admin user created:`);
|
||||
console.log(` E-Mail: ${ADMIN_EMAIL}`);
|
||||
console.log(` Passwort: ${ADMIN_PASSWORD}`);
|
||||
console.log(`\nBitte Passwort nach erstem Login ändern!`);
|
||||
|
||||
await pool.end();
|
||||
}
|
||||
|
||||
seed().catch((err) => {
|
||||
console.error('Seed failed:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user