Files
tatort/src/init/init_db.ts
Chi Cong Tran 76c2e26e8c
Some checks failed
InnoHub Processor/tatort/pipeline/head There was a failure building this commit
test persistent storage
2025-10-13 12:44:48 +02:00

47 lines
1.3 KiB
TypeScript

import Database from 'better-sqlite3';
import fs from 'fs';
import path from 'path';
import { DB_FULLPATH } from '../routes';
const fullPath = DB_FULLPATH;
const dir = path.dirname(fullPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const db = new Database(fullPath);
let createSQLStmt = `CREATE TABLE IF NOT EXISTS users
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
pw TEXT NOT NULL)`;
db.exec(createSQLStmt);
// check if there are any users; if not add one default admin one
// const saltRounds = 12;
// const hashedUserPassword = bcrypt.hashSync(userPasswordHashed, saltRounds);
const hashedUserPassword = '$2b$12$d6bDzoDluXeCTuoxmWSVtOp5Cpian3mZm8qxzox6B37BIf6qtOnnG';
const checkInsertSQLStmt = `INSERT INTO users (name, pw) SELECT 'admin', '${hashedUserPassword}'
WHERE NOT EXISTS (SELECT * FROM users);`;
db.exec(checkInsertSQLStmt);
const usersSQLStmt = `SELECT * FROM USERS`;
let SQLStatement = db.prepare(usersSQLStmt);
// cases table
createSQLStmt = `CREATE TABLE IF NOT EXISTS cases
(id INTEGER PRIMARY KEY AUTOINCREMENT,
token TEXT NOT NULL UNIQUE,
name TEXT NOT NULL UNIQUE,
pin TEXT NOT NULL)`;
db.exec(createSQLStmt);
const vorgangSQLStmt = `SELECT * FROM cases`;
SQLStatement = db.prepare(vorgangSQLStmt);
db.close();