2025-07-17 11:12:35 +00:00
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
`);
|
|
|
|
|
|
2025-07-17 11:21:35 +00:00
|
|
|
// Create settings table if it doesn't exist
|
|
|
|
|
db.exec(`
|
|
|
|
|
CREATE TABLE IF NOT EXISTS settings (
|
|
|
|
|
key TEXT PRIMARY KEY,
|
|
|
|
|
value TEXT
|
|
|
|
|
)
|
|
|
|
|
`);
|
|
|
|
|
|
2025-07-17 11:12:35 +00:00
|
|
|
// Check if the admin user already exists
|
2025-07-17 11:21:35 +00:00
|
|
|
const userStmt = db.prepare('SELECT * FROM users WHERE email = ?');
|
|
|
|
|
const adminUser = userStmt.get('admin@example.com');
|
2025-07-17 11:12:35 +00:00
|
|
|
|
|
|
|
|
if (!adminUser) {
|
|
|
|
|
// Insert the default admin user
|
|
|
|
|
// In a real application, you should hash the password!
|
2025-07-17 11:21:35 +00:00
|
|
|
const insertUser = db.prepare(
|
2025-07-17 11:12:35 +00:00
|
|
|
"INSERT INTO users (email, password, name) VALUES (?, ?, ?)"
|
|
|
|
|
);
|
2025-07-17 11:21:35 +00:00
|
|
|
insertUser.run("admin@example.com", "password", "Admin User");
|
2025-07-17 11:12:35 +00:00
|
|
|
console.log('Admin user created.');
|
|
|
|
|
} else {
|
|
|
|
|
console.log('Admin user already exists.');
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-17 11:21:35 +00:00
|
|
|
// 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.');
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-17 11:12:35 +00:00
|
|
|
console.log('Seeding complete.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
seed();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Seeding failed:');
|
|
|
|
|
console.error(e);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
} finally {
|
|
|
|
|
db.close();
|
|
|
|
|
}
|