populate db with default user
This commit is contained in:
42
src/init/init_db.js
Normal file
42
src/init/init_db.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import sqlite3 from 'sqlite3';
|
||||
import jsSHA from 'jssha';
|
||||
|
||||
const db = new sqlite3.Database('./src/lib/data/tatort.db');
|
||||
|
||||
db.serialize(() => {
|
||||
// users table
|
||||
|
||||
let create_stmt = `CREATE TABLE IF NOT EXISTS users
|
||||
(id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
pw TEXT NOT NULL)`;
|
||||
db.run(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.run(check_ins_stmt);
|
||||
|
||||
let users_stmt = `SELECT * FROM USERS`;
|
||||
db.each(users_stmt, (err, row) => {
|
||||
console.log(`xxx ${row.name} + ${row.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,
|
||||
created_by INTEGER NOT NULL,
|
||||
FOREIGN KEY(created_by) REFERENCES users(id))`;
|
||||
|
||||
db.run(create_stmt);
|
||||
});
|
||||
|
||||
db.close();
|
||||
BIN
src/lib/data/tatort.db
Normal file
BIN
src/lib/data/tatort.db
Normal file
Binary file not shown.
Reference in New Issue
Block a user