Exit cleanup functions, authentication polish, db polish

This commit is contained in:
MrFry
2020-04-07 14:09:34 +02:00
parent fb8e12f8d2
commit c764c4f402
10 changed files with 314 additions and 229 deletions

View File

@@ -270,7 +270,6 @@ class Question {
return SUtils.CompareString(this.A, qObj.A)
}
// TODO: return q / a / data match for debuging
Compare (q2, data) {
assert(q2)
let qObj

View File

@@ -11,9 +11,22 @@ let authDB
console.clear()
CreateDB()
// authDB.backup(usersDBPath)
// .then(() => {
// logger.Log('backup complete!')
// })
// .catch((err) => {
// logger.Log('backup failed!', logger.GetColor('redbg'))
// console.log(err)
// })
authDB.close()
function CreateDB () {
const dbStruct = utils.ReadJSON(dbStructPath)
// authDB = dbtools.GetDB(':memory:')
authDB = dbtools.GetDB(usersDBPath)
authDB.pragma('synchronous = OFF')
Object.keys(dbStruct).forEach((tableName) => {
const tableData = dbStruct[tableName]
@@ -24,11 +37,14 @@ function CreateDB () {
const uids = utils.ReadFile('../dbUsers/keys').split('\n')
uids.forEach((cid, i) => {
if (!cid) { return }
logger.Log(`[ ${i} / ${uids.length} ]`)
try {
dbtools.Insert(authDB, 'users', {
pw: uuidv4(),
oldCID: cid
oldCID: cid,
avaiblePWRequests: 4,
created: new Date().toString()
})
} catch (e) {
logger.Log('Error during inserting', logger.GetColor('redbg'))

View File

@@ -21,8 +21,8 @@ const utils = require('../utils/utils.js')
const debugLog = process.env.NS_SQL_DEBUG_LOG
// { asd: 'asd', basd: 4 } => asd = 'asd', basd = 4
function GetSqlQuerry (conditions) {
return Object.keys(conditions).reduce((acc, key) => {
function GetSqlQuerry (conditions, type) {
const res = Object.keys(conditions).reduce((acc, key) => {
const item = conditions[key]
if (typeof item === 'string') {
acc.push(`${key} = '${conditions[key]}'`)
@@ -30,14 +30,21 @@ function GetSqlQuerry (conditions) {
acc.push(`${key} = ${conditions[key]}`)
}
return acc
}, []).join(', ')
}, [])
if (type === 'where') {
return res.join(' AND ')
} else {
return res.join(', ')
}
}
// -------------------------------------------------------------------------
function GetDB (path) {
utils.CreatePath(path)
return new Sqlite(path)
const res = new Sqlite(path)
res.pragma('synchronous = OFF')
return res
}
function DebugLog (msg) {
@@ -87,7 +94,7 @@ function TableInfo (db, table) {
function Update (db, table, newData, conditions) {
try {
const s = `UPDATE ${table} SET ${GetSqlQuerry(newData)} WHERE ${GetSqlQuerry(conditions)}`
const s = `UPDATE ${table} SET ${GetSqlQuerry(newData, 'set')} WHERE ${GetSqlQuerry(conditions, 'where')}`
DebugLog(s)
const stmt = db.prepare(s)
@@ -99,7 +106,7 @@ function Update (db, table, newData, conditions) {
function Delete (db, table, conditions) {
try {
const s = `DELETE FROM ${table} WHERE ${GetSqlQuerry(conditions)}`
const s = `DELETE FROM ${table} WHERE ${GetSqlQuerry(conditions, 'where')}`
DebugLog(s)
const stmt = db.prepare(s)
@@ -137,14 +144,16 @@ function CreateTable (db, name, columns, foreignKeys) {
return acc
}, []).join(', ')
let fKeys = ''
let fKeys = []
if (foreignKeys) {
const { keysFrom, table, keysTo } = foreignKeys
fKeys = `, FOREIGN KEY(${keysFrom.join(', ')}) REFERENCES ${table}(${keysTo.join(', ')})`
foreignKeys.forEach((f) => {
const { keysFrom, table, keysTo } = f
fKeys.push(`, FOREIGN KEY(${keysFrom.join(', ')}) REFERENCES ${table}(${keysTo.join(', ')})`)
})
}
// IF NOT EXISTS
const s = `CREATE TABLE ${name}(${cols}${fKeys})`
const s = `CREATE TABLE ${name}(${cols}${fKeys.join(', ')})`
DebugLog(s)
const stmt = db.prepare(s)
@@ -168,7 +177,7 @@ function SelectAll (db, from) {
function Select (db, from, conditions) {
try {
const s = `SELECT * from ${from} WHERE ${GetSqlQuerry(conditions)}`
const s = `SELECT * from ${from} WHERE ${GetSqlQuerry(conditions, 'where')}`
DebugLog(s)
const stmt = db.prepare(s)