mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Authetication, logger middleware, db create tool
This commit is contained in:
parent
5f0b17a0db
commit
ebd27f93c1
11 changed files with 164 additions and 94 deletions
|
@ -109,19 +109,41 @@ function Delete (db, table, conditions) {
|
|||
}
|
||||
}
|
||||
|
||||
function CreateTable (db, name, columns) {
|
||||
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]
|
||||
const isPrimary = item.primary ? ' PRIMARY KEY' : ''
|
||||
const isNotNull = item.notNull ? ' NOT NULL' : ''
|
||||
// FIXME: array, and push stuff, then join()
|
||||
const flags = []
|
||||
const toCheck = {
|
||||
primary: 'PRIMARY KEY',
|
||||
notNull: 'NOT NULL',
|
||||
unique: 'UNIQUE',
|
||||
autoIncrement: 'AUTOINCREMENT'
|
||||
}
|
||||
Object.keys(toCheck).forEach((key) => {
|
||||
if (item[key]) {
|
||||
flags.push(toCheck[key])
|
||||
}
|
||||
})
|
||||
|
||||
acc.push(`${key} ${item.type}${isPrimary}${isNotNull}`)
|
||||
acc.push(`${key} ${item.type} ${flags.join(' ')}`)
|
||||
return acc
|
||||
}, []).join(', ')
|
||||
|
||||
// IF NOT EXISTS // TODO
|
||||
const s = `CREATE TABLE ${name}(${cols})`
|
||||
let fKeys = ''
|
||||
if (foreignKeys) {
|
||||
const { keysFrom, table, keysTo } = foreignKeys
|
||||
fKeys = `, FOREIGN KEY(${keysFrom.join(', ')}) REFERENCES ${table}(${keysTo.join(', ')})`
|
||||
}
|
||||
|
||||
// IF NOT EXISTS
|
||||
const s = `CREATE TABLE ${name}(${cols}${fKeys})`
|
||||
DebugLog(s)
|
||||
|
||||
const stmt = db.prepare(s)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue