Introduced help fnc for database
This commit is contained in:
26
helpfunctions/adduser.js
Normal file
26
helpfunctions/adduser.js
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user