mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Even more type fixes
This commit is contained in:
parent
37fbe56294
commit
d9175c1fc2
2 changed files with 96 additions and 42 deletions
|
@ -3,7 +3,12 @@ import type { Database } from 'better-sqlite3'
|
||||||
|
|
||||||
import logger from '../../../utils/logger'
|
import logger from '../../../utils/logger'
|
||||||
import utils from '../../../utils/utils'
|
import utils from '../../../utils/utils'
|
||||||
import { Request, SubmoduleData, User } from '../../../types/basicTypes'
|
import {
|
||||||
|
Request,
|
||||||
|
SubmoduleData,
|
||||||
|
User,
|
||||||
|
Submodule,
|
||||||
|
} from '../../../types/basicTypes'
|
||||||
import dbtools from '../../../utils/dbtools'
|
import dbtools from '../../../utils/dbtools'
|
||||||
|
|
||||||
const minimumAlowwedSessions = 2 // how many sessions are allowed for a user
|
const minimumAlowwedSessions = 2 // how many sessions are allowed for a user
|
||||||
|
@ -38,7 +43,7 @@ function BackupDB(usersDbBackupPath: string, userDB: Database) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function setup(data: SubmoduleData): any {
|
function setup(data: SubmoduleData): Submodule {
|
||||||
const { app, userDB, url /* publicdirs, moduleSpecificData */ } = data
|
const { app, userDB, url /* publicdirs, moduleSpecificData */ } = data
|
||||||
let domain: any = url.split('.') // [ "https://api", "frylabs", "net" ]
|
let domain: any = url.split('.') // [ "https://api", "frylabs", "net" ]
|
||||||
domain.shift() // [ "frylabs", "net" ]
|
domain.shift() // [ "frylabs", "net" ]
|
||||||
|
|
|
@ -15,14 +15,18 @@ export default {
|
||||||
runStatement: runStatement,
|
runStatement: runStatement,
|
||||||
}
|
}
|
||||||
|
|
||||||
import Sqlite, { Database } from 'better-sqlite3'
|
import Sqlite, { Database, RunResult } from 'better-sqlite3'
|
||||||
import logger from '../utils/logger'
|
import logger from '../utils/logger'
|
||||||
import utils from '../utils/utils'
|
import utils from '../utils/utils'
|
||||||
|
|
||||||
const debugLog = process.env.NS_SQL_DEBUG_LOG
|
const debugLog = process.env.NS_SQL_DEBUG_LOG
|
||||||
|
|
||||||
// { asd: 'asd', basd: 4 } => asd = 'asd', basd = 4
|
// { asd: 'asd', basd: 4 } => "asd = 'asd', basd = 4"
|
||||||
function GetSqlQuerry(conditions: any, type: string, joiner?: string) {
|
function GetSqlQuerry(
|
||||||
|
conditions: { [key: string]: string | number },
|
||||||
|
type?: string,
|
||||||
|
joiner?: string
|
||||||
|
) {
|
||||||
const res = Object.keys(conditions).reduce((acc, key) => {
|
const res = Object.keys(conditions).reduce((acc, key) => {
|
||||||
const item = conditions[key]
|
const item = conditions[key]
|
||||||
if (typeof item === 'string') {
|
if (typeof item === 'string') {
|
||||||
|
@ -45,7 +49,7 @@ function GetSqlQuerry(conditions: any, type: string, joiner?: string) {
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
function GetDB(path: string): any {
|
function GetDB(path: string): Database {
|
||||||
utils.CreatePath(path)
|
utils.CreatePath(path)
|
||||||
const res = new Sqlite(path)
|
const res = new Sqlite(path)
|
||||||
res.pragma('synchronous = OFF')
|
res.pragma('synchronous = OFF')
|
||||||
|
@ -58,7 +62,12 @@ function DebugLog(msg: string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function AddColumn(db: any, table: any, col: any): any {
|
// FIXME: this might not work: what is col exactly, and how we use AddColumn?
|
||||||
|
function AddColumn(
|
||||||
|
db: Database,
|
||||||
|
table: string,
|
||||||
|
col: { [key: string]: string | number }
|
||||||
|
): RunResult {
|
||||||
try {
|
try {
|
||||||
const colName = Object.keys(col)[0]
|
const colName = Object.keys(col)[0]
|
||||||
const colType = col.type
|
const colType = col.type
|
||||||
|
@ -69,10 +78,17 @@ function AddColumn(db: any, table: any, col: any): any {
|
||||||
return stmt.run()
|
return stmt.run()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function TableInfo(db: any, table: any): any {
|
function TableInfo(
|
||||||
|
db: Database,
|
||||||
|
table: string
|
||||||
|
): {
|
||||||
|
columns: any[]
|
||||||
|
dataCount: number
|
||||||
|
} {
|
||||||
try {
|
try {
|
||||||
const command = `PRAGMA table_info(${table})`
|
const command = `PRAGMA table_info(${table})`
|
||||||
const stmt = PrepareStatement(db, command)
|
const stmt = PrepareStatement(db, command)
|
||||||
|
@ -90,10 +106,16 @@ function TableInfo(db: any, table: any): any {
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Update(db: any, table: any, newData: any, conditions: any): any {
|
function Update(
|
||||||
|
db: Database,
|
||||||
|
table: string,
|
||||||
|
newData: { [key: string]: string | number },
|
||||||
|
conditions: { [key: string]: string | number }
|
||||||
|
): RunResult {
|
||||||
try {
|
try {
|
||||||
const command = `UPDATE ${table} SET ${GetSqlQuerry(
|
const command = `UPDATE ${table} SET ${GetSqlQuerry(
|
||||||
newData,
|
newData,
|
||||||
|
@ -104,10 +126,15 @@ function Update(db: any, table: any, newData: any, conditions: any): any {
|
||||||
return stmt.run()
|
return stmt.run()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Delete(db: any, table: any, conditions: any): any {
|
function Delete(
|
||||||
|
db: Database,
|
||||||
|
table: string,
|
||||||
|
conditions: { [key: string]: string | number }
|
||||||
|
): RunResult {
|
||||||
try {
|
try {
|
||||||
const command = `DELETE FROM ${table} WHERE ${GetSqlQuerry(
|
const command = `DELETE FROM ${table} WHERE ${GetSqlQuerry(
|
||||||
conditions,
|
conditions,
|
||||||
|
@ -118,10 +145,31 @@ function Delete(db: any, table: any, conditions: any): any {
|
||||||
return stmt.run()
|
return stmt.run()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function CreateTable(db: any, name: any, columns: any, foreignKeys: any): any {
|
interface DbColumnDescription {
|
||||||
|
[key: string]: {
|
||||||
|
type: string
|
||||||
|
primary?: boolean
|
||||||
|
autoIncrement?: boolean
|
||||||
|
notNull?: boolean
|
||||||
|
defaultZero?: boolean
|
||||||
|
[key: string]: any
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function CreateTable(
|
||||||
|
db: Database,
|
||||||
|
name: string,
|
||||||
|
columns: DbColumnDescription,
|
||||||
|
foreignKeys: {
|
||||||
|
keysFrom: string[]
|
||||||
|
table: string
|
||||||
|
keysTo: string[]
|
||||||
|
}[]
|
||||||
|
): RunResult {
|
||||||
// CREATE TABLE users(pw text PRIMARY KEY NOT NULL, id number, lastIP text, notes text, loginCount
|
// CREATE TABLE users(pw text PRIMARY KEY NOT NULL, id number, lastIP text, notes text, loginCount
|
||||||
// number, lastLogin text, lastAccess text
|
// number, lastLogin text, lastAccess text
|
||||||
//
|
//
|
||||||
|
@ -152,20 +200,14 @@ function CreateTable(db: any, name: any, columns: any, foreignKeys: any): any {
|
||||||
|
|
||||||
const fKeys: string[] = []
|
const fKeys: string[] = []
|
||||||
if (foreignKeys) {
|
if (foreignKeys) {
|
||||||
foreignKeys.forEach(
|
foreignKeys.forEach((foreignKey) => {
|
||||||
(foreignKey: {
|
|
||||||
keysFrom: string[]
|
|
||||||
table: string
|
|
||||||
keysTo: string[]
|
|
||||||
}) => {
|
|
||||||
const { keysFrom, table, keysTo } = foreignKey
|
const { keysFrom, table, keysTo } = foreignKey
|
||||||
fKeys.push(
|
fKeys.push(
|
||||||
`, FOREIGN KEY(${keysFrom.join(
|
`, FOREIGN KEY(${keysFrom.join(
|
||||||
', '
|
', '
|
||||||
)}) REFERENCES ${table}(${keysTo.join(', ')})`
|
)}) REFERENCES ${table}(${keysTo.join(', ')})`
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IF NOT EXISTS
|
// IF NOT EXISTS
|
||||||
|
@ -174,10 +216,11 @@ function CreateTable(db: any, name: any, columns: any, foreignKeys: any): any {
|
||||||
return stmt.run()
|
return stmt.run()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function SelectAll(db: any, from: any): any {
|
function SelectAll(db: Database, from: string): any[] {
|
||||||
try {
|
try {
|
||||||
const command = `SELECT * from ${from}`
|
const command = `SELECT * from ${from}`
|
||||||
|
|
||||||
|
@ -185,11 +228,17 @@ function SelectAll(db: any, from: any): any {
|
||||||
return stmt.all()
|
return stmt.all()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SELECT * FROM MyTable WHERE SomeColumn > LastValue ORDER BY SomeColumn LIMIT 100;
|
// SELECT * FROM MyTable WHERE SomeColumn > LastValue ORDER BY SomeColumn LIMIT 100;
|
||||||
function Select(db: any, from: any, conditions: any, options: any = {}): any {
|
function Select(
|
||||||
|
db: Database,
|
||||||
|
from: string,
|
||||||
|
conditions: { [key: string]: string | number },
|
||||||
|
options: { joiner?: string; limit?: number } = {}
|
||||||
|
): any[] {
|
||||||
const { joiner, limit } = options
|
const { joiner, limit } = options
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -207,10 +256,15 @@ function Select(db: any, from: any, conditions: any, options: any = {}): any {
|
||||||
return stmt.all()
|
return stmt.all()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Insert(db: any, table: any, data: any): any {
|
function Insert(
|
||||||
|
db: Database,
|
||||||
|
table: string,
|
||||||
|
data: { [key: string]: number | string }
|
||||||
|
): RunResult {
|
||||||
try {
|
try {
|
||||||
const cols = Object.keys(data)
|
const cols = Object.keys(data)
|
||||||
.reduce((acc, key) => {
|
.reduce((acc, key) => {
|
||||||
|
@ -220,15 +274,13 @@ function Insert(db: any, table: any, data: any): any {
|
||||||
.join(', ')
|
.join(', ')
|
||||||
|
|
||||||
const values = Object.keys(data)
|
const values = Object.keys(data)
|
||||||
.reduce((acc, key) => {
|
.map((item) => {
|
||||||
const item = data[key]
|
|
||||||
if (typeof item === 'string') {
|
if (typeof item === 'string') {
|
||||||
acc.push(`'${item}'`)
|
return `'${item}'`
|
||||||
} else {
|
} else {
|
||||||
acc.push(`${item}`)
|
return `${item}`
|
||||||
}
|
}
|
||||||
return acc
|
})
|
||||||
}, [])
|
|
||||||
.join(', ')
|
.join(', ')
|
||||||
|
|
||||||
const command = `INSERT INTO ${table} (${cols}) VALUES (${values})`
|
const command = `INSERT INTO ${table} (${cols}) VALUES (${values})`
|
||||||
|
@ -237,25 +289,22 @@ function Insert(db: any, table: any, data: any): any {
|
||||||
return stmt.run()
|
return stmt.run()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function runStatement(db: any, command: string, runType?: string): any {
|
function runStatement(db: Database, command: string, runType?: string): any {
|
||||||
const stmt = PrepareStatement(db, command)
|
const stmt = PrepareStatement(db, command)
|
||||||
if (!runType) {
|
if (!runType) {
|
||||||
return stmt.all()
|
return stmt.all()
|
||||||
} else if (runType === 'run') {
|
} else if (runType === 'run') {
|
||||||
return stmt.run()
|
return stmt.run()
|
||||||
}
|
}
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
function CloseDB(db: any): void {
|
function CloseDB(db: Database): void {
|
||||||
db.close((err: Error) => {
|
db.close()
|
||||||
if (err) {
|
|
||||||
return console.error(err.message)
|
|
||||||
}
|
|
||||||
DebugLog('Close the database connection.')
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue