Introduced help fnc for database

This commit is contained in:
Naida Vatric
2019-10-31 01:11:25 +01:00
parent 46d32196d9
commit 2193428609
5 changed files with 133 additions and 22 deletions

27
helpfunctions/addgroup.js Normal file
View File

@@ -0,0 +1,27 @@
//Checking if group existis in DB table groups
//if no inserts new group with no users (implicitly)
const handleAddGroup = (reqgroup, db) => {
db.transaction (trx => {
trx.select('groupname')
.from('groups')
.where('groupname', 'ilike', reqgroup)
.then ( data => {
if (data.length===0) {
return trx('groups')
.returning('*')
.insert( {
groupname: reqgroup,
username: ''
})
.then(group => {
console.log('Inserted new empty group implicitly.');
})
}
})
.then(trx.commit)
.catch(trx.rollback)
})
}
module.exports = {
handleAddGroup
}

View File

@@ -0,0 +1,26 @@
//Checking if object existis in DB table objects
//if no inserts new object (implicitly)
const handleAddObject = (reqobjname, db) => {
db.transaction (trx => {
trx.select('objname')
.from('objects')
.where('objname', 'ilike', reqobjname)
.then ( data => {
if (data.length===0) {
return trx('objects')
.returning('*')
.insert( {
objname: reqobjname
})
.then(obj => {
console.log('Inserted new object implicitly.');
})
}
})
.then(trx.commit)
.catch(trx.rollback)
})
}
module.exports = {
handleAddObject
}

26
helpfunctions/adduser.js Normal file
View File

@@ -0,0 +1,26 @@
//Checking if user existis in DB table users
//if no inserts new user (implicitly)
const handleAddUser = (requser, db) => {
db.transaction (trx => {
trx.select('username')
.from('users')
.where('username', 'ilike', requser)
.then ( data => {
if (data.length===0) {
return trx('users')
.returning('*')
.insert( {
username: requser
})
.then(user => {
console.log('Inserted new user implicitly.');
})
}
})
.then(trx.commit)
.catch(trx.rollback)
})
}
module.exports = {
handleAddUser
}