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