Files
getrankekarte/db.js
DanielS 7f1e17db2b
All checks were successful
Build and Deploy / build (push) Successful in 1m11s
fix(persistence): Robustes /data Volume für SQLite data.db & WAL-Dateien
2026-09-22 23:18:39 +02:00

82 lines
2.5 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const Database = require('better-sqlite3');
const bcrypt = require('bcryptjs');
// Verzeichnis für Persistenz sicherstellen (/data im Docker Container oder local dir)
const dataDir = process.env.DATA_DIR || (fs.existsSync('/data') ? '/data' : __dirname);
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
const dbPath = path.join(dataDir, 'data.db');
const db = new Database(dbPath);
// Standard Journal Mode
db.pragma('journal_mode = WAL');
// Tabellen anlegen
db.exec(`
CREATE TABLE IF NOT EXISTS config (
key TEXT PRIMARY KEY,
value TEXT
);
CREATE TABLE IF NOT EXISTS product_groups (
id TEXT PRIMARY KEY,
name TEXT,
sort_order INTEGER DEFAULT 0
);
CREATE TABLE IF NOT EXISTS products (
id TEXT PRIMARY KEY,
text1 TEXT NOT NULL,
text2 TEXT,
group_id TEXT,
preis1 REAL DEFAULT 0,
unit TEXT,
active INTEGER DEFAULT 1,
sort_order INTEGER DEFAULT 0,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS sync_logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
status TEXT,
message TEXT,
items_count INTEGER DEFAULT 0
);
`);
// Standard Admin Initialisieren
const adminUser = db.prepare('SELECT value FROM config WHERE key = ?').get('admin_user');
if (!adminUser) {
const hash = bcrypt.hashSync('Kathi2022!', 10);
db.prepare('INSERT INTO config (key, value) VALUES (?, ?)').run('admin_user', 'admin');
db.prepare('INSERT INTO config (key, value) VALUES (?, ?)').run('admin_password_hash', hash);
db.prepare('INSERT INTO config (key, value) VALUES (?, ?)').run('jwt_secret', 'secret_' + Math.random().toString(36).substring(2));
}
// Standard Theme & Texte initialisieren
const initConfig = (k, v) => {
const existing = db.prepare('SELECT value FROM config WHERE key = ?').get(k);
if (!existing) {
db.prepare('INSERT INTO config (key, value) VALUES (?, ?)').run(k, v);
}
};
initConfig('site_title', 'GETRÄNKEKARTE');
initConfig('site_subtitle', 'Frisch zubereitete Drinks, Spezialitäten & Erfrischungen');
initConfig('site_badge', '✦ EXKLUSIVE AUSWAHL ✦');
initConfig('site_footer', '© Bar & Lounge • Live POS Sync');
initConfig('theme_bg', '#090b10');
initConfig('theme_card_bg', 'rgba(22, 27, 34, 0.75)');
initConfig('theme_primary', '#f59e0b');
initConfig('theme_accent', '#ec4899');
initConfig('theme_text', '#f8fafc');
initConfig('theme_text_muted', '#94a3b8');
module.exports = db;