55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const Database = require('better-sqlite3');
|
|
const bcrypt = require('bcryptjs');
|
|
|
|
const dbPath = path.join(__dirname, 'data.db');
|
|
const db = new Database(dbPath);
|
|
|
|
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: admin / Kathi2022!
|
|
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));
|
|
}
|
|
|
|
module.exports = db;
|