mirror of
				https://gitlab.com/MrFry/mrfrys-node-server
				synced 2025-04-01 20:24:18 +02:00 
			
		
		
		
	Auth system
This commit is contained in:
		
							
								
								
									
										189
									
								
								utils/dbtools.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										189
									
								
								utils/dbtools.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,189 @@
 | 
			
		||||
// https://www.sqlitetutorial.net/sqlite-nodejs/
 | 
			
		||||
// https://github.com/JoshuaWise/better-sqlite3/blob/HEAD/docs/api.md
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  GetDB,
 | 
			
		||||
  AddColumn,
 | 
			
		||||
  TableInfo,
 | 
			
		||||
  UpdateRecord,
 | 
			
		||||
  Delete,
 | 
			
		||||
  CreateTable,
 | 
			
		||||
  SelectAll,
 | 
			
		||||
  Select,
 | 
			
		||||
  Insert,
 | 
			
		||||
  CloseDB
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const Sqlite = require('better-sqlite3')
 | 
			
		||||
const logger = require('../utils/logger.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) => {
 | 
			
		||||
    const item = conditions[key]
 | 
			
		||||
    if (typeof item === 'string') {
 | 
			
		||||
      acc.push(`${key} = '${conditions[key]}'`)
 | 
			
		||||
    } else {
 | 
			
		||||
      acc.push(`${key} = ${conditions[key]}`)
 | 
			
		||||
    }
 | 
			
		||||
    return acc
 | 
			
		||||
  }, []).join(', ')
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// -------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
function GetDB (path) {
 | 
			
		||||
  return new Sqlite(path)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function DebugLog (msg) {
 | 
			
		||||
  if (debugLog) {
 | 
			
		||||
    logger.DebugLog(msg, 'sql', 0)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function AddColumn (db, table, col) {
 | 
			
		||||
  try {
 | 
			
		||||
    const colName = Object.keys(col)[0]
 | 
			
		||||
    const colType = col.type
 | 
			
		||||
 | 
			
		||||
    const s = `ALTER TABLE ${table} ADD COLUMN ${colName} ${colType}`
 | 
			
		||||
    DebugLog(s)
 | 
			
		||||
    const stmt = db.prepare(s)
 | 
			
		||||
 | 
			
		||||
    return stmt.run()
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    console.error(e)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function TableInfo (db, table) {
 | 
			
		||||
  try {
 | 
			
		||||
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    console.error(e)
 | 
			
		||||
  }
 | 
			
		||||
  const s = `PRAGMA table_info(${table})`
 | 
			
		||||
  DebugLog(s)
 | 
			
		||||
  const stmt = db.prepare(s)
 | 
			
		||||
 | 
			
		||||
  const infoRes = stmt.all()
 | 
			
		||||
 | 
			
		||||
  const s2 = `SELECT COUNT(*) FROM ${table}`
 | 
			
		||||
  DebugLog(s2)
 | 
			
		||||
  const stmt2 = db.prepare(s2)
 | 
			
		||||
 | 
			
		||||
  const countRes = stmt2.get()
 | 
			
		||||
 | 
			
		||||
  return {
 | 
			
		||||
    columns: infoRes,
 | 
			
		||||
    dataCount: countRes[Object.keys(countRes)[0]]
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function UpdateRecord (db, table, newData, conditions) {
 | 
			
		||||
  try {
 | 
			
		||||
    const s = `UPDATE ${table} SET ${GetSqlQuerry(newData)} WHERE ${GetSqlQuerry(conditions)}`
 | 
			
		||||
    DebugLog(s)
 | 
			
		||||
    const stmt = db.prepare(s)
 | 
			
		||||
 | 
			
		||||
    return stmt.run()
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    console.error(e)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function Delete (db, table, conditions) {
 | 
			
		||||
  try {
 | 
			
		||||
    const s = `DELETE FROM ${table} WHERE ${GetSqlQuerry(conditions)}`
 | 
			
		||||
    DebugLog(s)
 | 
			
		||||
    const stmt = db.prepare(s)
 | 
			
		||||
 | 
			
		||||
    return stmt.run()
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    console.error(e)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function CreateTable (db, name, columns) {
 | 
			
		||||
  try {
 | 
			
		||||
    const cols = Object.keys(columns).reduce((acc, key) => {
 | 
			
		||||
      const item = columns[key]
 | 
			
		||||
      const isPrimary = item.primary ? ' PRIMARY KEY' : ''
 | 
			
		||||
      const isNotNull = item.notNull ? ' NOT NULL' : ''
 | 
			
		||||
 | 
			
		||||
      acc.push(`${key} ${item.type}${isPrimary}${isNotNull}`)
 | 
			
		||||
      return acc
 | 
			
		||||
    }, []).join(', ')
 | 
			
		||||
 | 
			
		||||
    const s = `CREATE TABLE IF NOT EXISTS ${name}(${cols})`
 | 
			
		||||
    DebugLog(s)
 | 
			
		||||
 | 
			
		||||
    const stmt = db.prepare(s)
 | 
			
		||||
    return stmt.run()
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    console.error(e)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function SelectAll (db, from) {
 | 
			
		||||
  try {
 | 
			
		||||
    const s = `SELECT * from ${from}`
 | 
			
		||||
    DebugLog(s)
 | 
			
		||||
 | 
			
		||||
    const stmt = db.prepare(s)
 | 
			
		||||
    return stmt.all()
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    console.error(e)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function Select (db, from, conditions) {
 | 
			
		||||
  try {
 | 
			
		||||
    const s = `SELECT * from ${from} WHERE ${GetSqlQuerry(conditions)}`
 | 
			
		||||
    DebugLog(s)
 | 
			
		||||
 | 
			
		||||
    const stmt = db.prepare(s)
 | 
			
		||||
    return stmt.all()
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    console.error(e)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function Insert (db, table, data) {
 | 
			
		||||
  try {
 | 
			
		||||
    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 s = `INSERT INTO ${table} (${cols}) VALUES (${values})`
 | 
			
		||||
    DebugLog(s)
 | 
			
		||||
    const stmt = db.prepare(s)
 | 
			
		||||
 | 
			
		||||
    return stmt.run()
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    console.error(e)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function CloseDB (db) {
 | 
			
		||||
  db.close((err) => {
 | 
			
		||||
    if (err) {
 | 
			
		||||
      return console.error(err.message)
 | 
			
		||||
    }
 | 
			
		||||
    DebugLog('Close the database connection.')
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
@@ -43,13 +43,6 @@ const statFile = 'stats/stats'
 | 
			
		||||
const vStatFile = 'stats/vstats'
 | 
			
		||||
const nologFile = './nolog'
 | 
			
		||||
 | 
			
		||||
const writeInterval = 10
 | 
			
		||||
 | 
			
		||||
let data = {}
 | 
			
		||||
let vData = {}
 | 
			
		||||
let writes = 0
 | 
			
		||||
let debugLevel = 0
 | 
			
		||||
 | 
			
		||||
const colors = [
 | 
			
		||||
  'green',
 | 
			
		||||
  'red',
 | 
			
		||||
@@ -59,6 +52,14 @@ const colors = [
 | 
			
		||||
  'cyan'
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
const writeInterval = 10
 | 
			
		||||
const debugLevel = parseInt(process.env.NS_LOGLEVEL) || 0
 | 
			
		||||
Log('Loglevel is: ' + debugLevel)
 | 
			
		||||
 | 
			
		||||
let data = {}
 | 
			
		||||
let vData = {}
 | 
			
		||||
let writes = 0
 | 
			
		||||
 | 
			
		||||
let noLogips = []
 | 
			
		||||
 | 
			
		||||
function GetDateString () {
 | 
			
		||||
@@ -158,10 +159,7 @@ function setNoLogReadInterval () {
 | 
			
		||||
  parseNoLogFile(utils.ReadFile(nologFile))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function Load (debugLvl) {
 | 
			
		||||
  if (debugLvl) {
 | 
			
		||||
    debugLevel = debugLvl
 | 
			
		||||
  }
 | 
			
		||||
function Load () {
 | 
			
		||||
  Log('Loading logger...')
 | 
			
		||||
  try {
 | 
			
		||||
    var prevData = utils.ReadFile(statFile)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
module.exports = {
 | 
			
		||||
  ReadFile: ReadFile,
 | 
			
		||||
  ReadJSON: ReadJSON,
 | 
			
		||||
  WriteFile: WriteFile,
 | 
			
		||||
  writeFileAsync: WriteFileAsync,
 | 
			
		||||
  AppendToFile: AppendToFile,
 | 
			
		||||
@@ -27,6 +28,15 @@ function ReadDir (path) {
 | 
			
		||||
  return fs.readdirSync(path)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function ReadJSON (name) {
 | 
			
		||||
  try {
 | 
			
		||||
    return JSON.parse(ReadFile(name))
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    console.log(e)
 | 
			
		||||
    throw new Error('Coulndt parse JSON in "ReadJSON", file: ' + name)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function ReadFile (name) {
 | 
			
		||||
  if (!FileExists(name)) { throw new Error('No such file: ' + name) }
 | 
			
		||||
  return fs.readFileSync(name, 'utf8')
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user