Prettied all js in src/

This commit is contained in:
mrfry 2020-10-01 14:44:24 +02:00
parent 3f081d8dff
commit ee0f0a9f3b
17 changed files with 1012 additions and 688 deletions
src/utils

View file

@ -11,7 +11,7 @@ module.exports = {
SelectAll,
Select,
Insert,
CloseDB
CloseDB,
}
const Sqlite = require('better-sqlite3')
@ -21,7 +21,7 @@ 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, type) {
function GetSqlQuerry(conditions, type) {
const res = Object.keys(conditions).reduce((acc, key) => {
const item = conditions[key]
if (typeof item === 'string') {
@ -40,20 +40,20 @@ function GetSqlQuerry (conditions, type) {
// -------------------------------------------------------------------------
function GetDB (path) {
function GetDB(path) {
utils.CreatePath(path)
const res = new Sqlite(path)
res.pragma('synchronous = OFF')
return res
}
function DebugLog (msg) {
function DebugLog(msg) {
if (debugLog) {
logger.DebugLog(msg, 'sql', 0)
}
}
function AddColumn (db, table, col) {
function AddColumn(db, table, col) {
try {
const colName = Object.keys(col)[0]
const colType = col.type
@ -67,9 +67,8 @@ function AddColumn (db, table, col) {
}
}
function TableInfo (db, table) {
function TableInfo(db, table) {
try {
} catch (e) {
console.error(e)
}
@ -85,13 +84,16 @@ function TableInfo (db, table) {
return {
columns: infoRes,
dataCount: countRes[Object.keys(countRes)[0]]
dataCount: countRes[Object.keys(countRes)[0]],
}
}
function Update (db, table, newData, conditions) {
function Update(db, table, newData, conditions) {
try {
const s = `UPDATE ${table} SET ${GetSqlQuerry(newData, 'set')} WHERE ${GetSqlQuerry(conditions, 'where')}`
const s = `UPDATE ${table} SET ${GetSqlQuerry(
newData,
'set'
)} WHERE ${GetSqlQuerry(conditions, 'where')}`
const stmt = PrepareStatement(db, s)
return stmt.run()
@ -100,7 +102,7 @@ function Update (db, table, newData, conditions) {
}
}
function Delete (db, table, conditions) {
function Delete(db, table, conditions) {
try {
const s = `DELETE FROM ${table} WHERE ${GetSqlQuerry(conditions, 'where')}`
const stmt = PrepareStatement(db, s)
@ -111,39 +113,45 @@ function Delete (db, table, conditions) {
}
}
function CreateTable (db, name, columns, foreignKeys) {
function CreateTable(db, name, columns, foreignKeys) {
// CREATE TABLE users(pw text PRIMARY KEY NOT NULL, id number, lastIP text, notes text, loginCount
// number, lastLogin text, lastAccess text
//
// FOREIGN KEY(songartist, songalbum) REFERENCES album(albumartist, albumname) )
try {
const cols = Object.keys(columns).reduce((acc, key) => {
const item = columns[key]
// FIXME: array, and push stuff, then join()
const flags = []
const toCheck = {
primary: 'PRIMARY KEY',
notNull: 'NOT NULL',
unique: 'UNIQUE',
autoIncrement: 'AUTOINCREMENT',
defaultZero: 'DEFAULT 0'
}
Object.keys(toCheck).forEach((key) => {
if (item[key]) {
flags.push(toCheck[key])
const cols = Object.keys(columns)
.reduce((acc, key) => {
const item = columns[key]
// FIXME: array, and push stuff, then join()
const flags = []
const toCheck = {
primary: 'PRIMARY KEY',
notNull: 'NOT NULL',
unique: 'UNIQUE',
autoIncrement: 'AUTOINCREMENT',
defaultZero: 'DEFAULT 0',
}
})
Object.keys(toCheck).forEach((key) => {
if (item[key]) {
flags.push(toCheck[key])
}
})
acc.push(`${key} ${item.type} ${flags.join(' ')}`)
return acc
}, []).join(', ')
acc.push(`${key} ${item.type} ${flags.join(' ')}`)
return acc
}, [])
.join(', ')
let fKeys = []
if (foreignKeys) {
foreignKeys.forEach((f) => {
const { keysFrom, table, keysTo } = f
fKeys.push(`, FOREIGN KEY(${keysFrom.join(', ')}) REFERENCES ${table}(${keysTo.join(', ')})`)
fKeys.push(
`, FOREIGN KEY(${keysFrom.join(
', '
)}) REFERENCES ${table}(${keysTo.join(', ')})`
)
})
}
@ -156,7 +164,7 @@ function CreateTable (db, name, columns, foreignKeys) {
}
}
function SelectAll (db, from) {
function SelectAll(db, from) {
try {
const s = `SELECT * from ${from}`
@ -167,7 +175,7 @@ function SelectAll (db, from) {
}
}
function Select (db, from, conditions) {
function Select(db, from, conditions) {
try {
const s = `SELECT * from ${from} WHERE ${GetSqlQuerry(conditions, 'where')}`
@ -178,22 +186,26 @@ function Select (db, from, conditions) {
}
}
function Insert (db, table, data) {
function Insert(db, table, data) {
try {
const cols = Object.keys(data).reduce((acc, key) => {
acc.push(`${key}`)
return acc
}, []).join(', ')
const cols = Object.keys(data)
.reduce((acc, key) => {
acc.push(`${key}`)
return acc
}, [])
.join(', ')
const values = Object.keys(data).reduce((acc, key) => {
const item = data[key]
if (typeof item === 'string') {
acc.push(`'${item}'`)
} else {
acc.push(`${item}`)
}
return acc
}, []).join(', ')
const values = Object.keys(data)
.reduce((acc, key) => {
const item = data[key]
if (typeof item === 'string') {
acc.push(`'${item}'`)
} else {
acc.push(`${item}`)
}
return acc
}, [])
.join(', ')
const s = `INSERT INTO ${table} (${cols}) VALUES (${values})`
const stmt = PrepareStatement(db, s)
@ -204,7 +216,7 @@ function Insert (db, table, data) {
}
}
function CloseDB (db) {
function CloseDB(db) {
db.close((err) => {
if (err) {
return console.error(err.message)
@ -215,9 +227,11 @@ function CloseDB (db) {
// -------------------------------------------------------------------------
function PrepareStatement (db, s) {
function PrepareStatement(db, s) {
if (!db) {
throw new Error('DB is undefined in prepare statement! DB action called with undefined db')
throw new Error(
'DB is undefined in prepare statement! DB action called with undefined db'
)
}
DebugLog(s)
return db.prepare(s)