import Database from 'better-sqlite3'; import jsSHA from 'jssha'; const db = new Database('./src/lib/data/tatort.db'); let create_stmt = `CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, pw TEXT NOT NULL)`; db.exec(create_stmt); // check if there are any users; if not add one default admin one let pw = 'pass-123'; let hashed_pw = new jsSHA('SHA-512', 'TEXT').update(pw).getHash('HEX'); let check_ins_stmt = `INSERT INTO users (name, pw) SELECT 'admin', '${hashed_pw}' WHERE NOT EXISTS (SELECT * FROM users);`; db.exec(check_ins_stmt); let users_stmt = `SELECT * FROM USERS`; let stmt = db.prepare(users_stmt); console.log(`\n`, `*** Users table`); for (const usr of stmt.iterate()) { console.log(`[r] ${usr.name} + ${usr.pw}`); } // cases table create_stmt = `CREATE TABLE IF NOT EXISTS cases (id INTEGER PRIMARY KEY AUTOINCREMENT, token TEXT NOT NULL UNIQUE, name TEXT NOT NULL, pw TEXT NOT NULL)`; db.exec(create_stmt); let cases_stmt = `SELECT * FROM cases`; stmt = db.prepare(cases_stmt); console.log(`\n`, `*** Cases table`); for (const usr of stmt.iterate()) { console.log(`[r] ${usr.name} + ${usr.token} + ${usr.pw}`); } db.close();