import Database from 'better-sqlite3'; const db = new Database('local.db'); function seed() { console.log('Seeding database...'); // Create users table if it doesn't exist db.exec(` CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT UNIQUE NOT NULL, password TEXT NOT NULL, name TEXT NOT NULL ) `); // Create settings table if it doesn't exist db.exec(` CREATE TABLE IF NOT EXISTS settings ( key TEXT PRIMARY KEY, value TEXT ) `); // Check if the admin user already exists const userStmt = db.prepare('SELECT * FROM users WHERE email = ?'); const adminUser = userStmt.get('admin@example.com'); if (!adminUser) { // Insert the default admin user // In a real application, you should hash the password! const insertUser = db.prepare( "INSERT INTO users (email, password, name) VALUES (?, ?, ?)" ); insertUser.run("admin@example.com", "password", "Admin User"); console.log('Admin user created.'); } else { console.log('Admin user already exists.'); } // Check if the hourly_rate setting already exists const settingStmt = db.prepare('SELECT * FROM settings WHERE key = ?'); const hourlyRateSetting = settingStmt.get('hourly_rate'); if (!hourlyRateSetting) { // Insert the default hourly rate const insertSetting = db.prepare( "INSERT INTO settings (key, value) VALUES (?, ?)" ); insertSetting.run("hourly_rate", "100"); console.log('Default hourly rate set.'); } else { console.log('Hourly rate setting already exists.'); } console.log('Seeding complete.'); } try { seed(); } catch (e) { console.error('Seeding failed:'); console.error(e); process.exit(1); } finally { db.close(); }