diff --git a/backend/src/db.js b/backend/src/db.js new file mode 100644 index 0000000..1ed30a0 --- /dev/null +++ b/backend/src/db.js @@ -0,0 +1,76 @@ +const Database = require('better-sqlite3'); +const bcrypt = require('bcryptjs'); +const path = require('path'); + +const db = new Database(process.env.DB_PATH || '/data/dickendock.db'); +db.pragma('journal_mode = WAL'); +db.pragma('foreign_keys = ON'); + +db.exec(` + CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT UNIQUE NOT NULL, + password_hash TEXT NOT NULL, + role TEXT NOT NULL DEFAULT 'user', + created_at DATETIME DEFAULT CURRENT_TIMESTAMP + ); + + -- Dashboard-Widgets + CREATE TABLE IF NOT EXISTS quick_links ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, + title TEXT NOT NULL, + url TEXT NOT NULL, + icon TEXT NOT NULL DEFAULT 'πŸ”—', + sort_order INTEGER DEFAULT 0 + ); + CREATE TABLE IF NOT EXISTS todos ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, + text TEXT NOT NULL, + done INTEGER NOT NULL DEFAULT 0, + sort_order INTEGER DEFAULT 0, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP + ); + CREATE TABLE IF NOT EXISTS notes ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER NOT NULL UNIQUE REFERENCES users(id) ON DELETE CASCADE, + content TEXT NOT NULL DEFAULT '', + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP + ); + + -- Tool: 3D-Kalkulator + CREATE TABLE IF NOT EXISTS calculations ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, + name TEXT NOT NULL, + gramm REAL NOT NULL, + stunden REAL NOT NULL, + farben INTEGER NOT NULL DEFAULT 1, + materialpreis_pro_gramm REAL NOT NULL DEFAULT 0.01, + stromverbrauch_kw REAL NOT NULL DEFAULT 0.15, + strompreis_pro_kwh REAL NOT NULL DEFAULT 0.38, + druckerpreis REAL NOT NULL DEFAULT 550, + gesamtdruckstunden REAL NOT NULL DEFAULT 5000, + verschleiss_pro_stunde REAL NOT NULL DEFAULT 0.06, + preis_freundschaft REAL NOT NULL, + preis_normal REAL NOT NULL, + preis_auftrag REAL NOT NULL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME DEFAULT CURRENT_TIMESTAMP + ); +`); + +// Standard-Admin beim ersten Start anlegen +if (!db.prepare('SELECT id FROM users WHERE username = ?').get('admin')) { + db.prepare('INSERT INTO users (username, password_hash, role) VALUES (?, ?, ?)') + .run('admin', bcrypt.hashSync('admin123', 12), 'admin'); + console.log('βœ… Admin angelegt: admin / admin123 β†’ Bitte Passwort sofort Γ€ndern!'); +} + +// ── Migrationen (werden bei jedem Start geprΓΌft) ───────────────────────────── +const cols = db.prepare("PRAGMA table_info(calculations)").all().map(r => r.name); +if (!cols.includes('image')) db.exec("ALTER TABLE calculations ADD COLUMN image TEXT"); +if (!cols.includes('bemerkung')) db.exec("ALTER TABLE calculations ADD COLUMN bemerkung TEXT NOT NULL DEFAULT ''"); + +module.exports = db;