Update Knex Query in cron.js

install-with-shinobicctv-user-instead-of-root
Moe 2020-08-20 09:31:21 -07:00
parent 6c577f7691
commit 9b87f5255b
1 changed files with 41 additions and 29 deletions

70
cron.js
View File

@ -135,29 +135,15 @@ s.sqlQuery = function(query,values,onMoveOn){
} }
}) })
} }
const processSimpleWhereCondition = (dbQuery,where,didOne) => {
const processWhereCondition = (dbQuery,where,didOne) => {
var whereIsArray = where instanceof Array; var whereIsArray = where instanceof Array;
if(!where[0])return; if(where[0] === 'or' || where.__separator === 'or'){
if(where[0] && where[0] instanceof Array){ if(whereIsArray){
// didOne = true where.shift()
dbQuery.where(function() { dbQuery.orWhere(...where)
var _this = this }else{
var didOneInsideGroup = false where = cleanSqlWhereObject(where)
where.forEach((whereInsideGroup) => { dbQuery.orWhere(where)
processWhereCondition(_this,whereInsideGroup,didOneInsideGroup)
})
})
}else if(where.length === 4){
const separator = where[0] + ''
where.shift()
switch(separator){
case'and':
whereIsArray ? dbQuery.andWhere(...where) : dbQuery.andWhere(where)
break;
case'or':
whereIsArray ? dbQuery.orWhere(...where) : dbQuery.orWhere(where)
break;
} }
}else if(!didOne){ }else if(!didOne){
didOne = true didOne = true
@ -165,17 +151,39 @@ const processWhereCondition = (dbQuery,where,didOne) => {
}else{ }else{
whereIsArray ? dbQuery.andWhere(...where) : dbQuery.andWhere(where) whereIsArray ? dbQuery.andWhere(...where) : dbQuery.andWhere(where)
} }
}
const processWhereCondition = (dbQuery,where,didOne) => {
var whereIsArray = where instanceof Array;
if(!where[0])return;
if(where[0] && where[0] instanceof Array){
dbQuery.where(function() {
var _this = this
var didOneInsideGroup = false
where.forEach((whereInsideGroup) => {
processWhereCondition(_this,whereInsideGroup,didOneInsideGroup)
})
})
}else if(where[0] && where[0] instanceof Object){
dbQuery.where(function() {
var _this = this
var didOneInsideGroup = false
where.forEach((whereInsideGroup) => {
processSimpleWhereCondition(_this,whereInsideGroup,didOneInsideGroup)
})
})
}else{
processSimpleWhereCondition(dbQuery,where,didOne)
}
} }
const knexError = (dbQuery,options,err) => { const knexError = (dbQuery,options,err) => {
console.error('knexError CRON----------------------------------- START') console.error('knexError----------------------------------- START')
if(config.debugLogVerbose && config.debugLog === true){ if(config.debugLogVerbose && config.debugLog === true){
s.debugLog('s.knexQuery QUERY',JSON.stringify(options,null,3)) s.debugLog('s.knexQuery QUERY',JSON.stringify(options,null,3))
s.debugLog('STACK TRACE, NOT AN ',new Error()) s.debugLog('STACK TRACE, NOT AN ',new Error())
} }
console.error(err) console.error(err)
console.error(dbQuery.toString()) console.error(dbQuery.toString())
console.error('knexError CRON----------------------------------- END') console.error('knexError----------------------------------- END')
} }
const knexQuery = (options,callback) => { const knexQuery = (options,callback) => {
try{ try{
@ -200,17 +208,22 @@ const knexQuery = (options,callback) => {
dbQuery = s.databaseEngine(options.table).update(options.update) dbQuery = s.databaseEngine(options.table).update(options.update)
break; break;
case'delete': case'delete':
dbQuery = s.databaseEngine(options.table).del() dbQuery = s.databaseEngine(options.table)
break; break;
case'insert': case'insert':
dbQuery = s.databaseEngine(options.table).insert(options.insert) dbQuery = s.databaseEngine(options.table).insert(options.insert)
break; break;
} }
if(options.where){ if(options.where instanceof Array){
var didOne = false; var didOne = false;
options.where.forEach((where) => { options.where.forEach((where) => {
processWhereCondition(dbQuery,where,didOne) processWhereCondition(dbQuery,where,didOne)
}) })
}else if(options.where instanceof Object){
dbQuery.where(options.where)
}
if(options.action === 'delete'){
dbQuery.del()
} }
if(options.orderBy){ if(options.orderBy){
dbQuery.orderBy(...options.orderBy) dbQuery.orderBy(...options.orderBy)
@ -227,10 +240,9 @@ const knexQuery = (options,callback) => {
} }
} }
if(config.debugLog === true){ if(config.debugLog === true){
console.log(options)
console.log(dbQuery.toString()) console.log(dbQuery.toString())
} }
if(callback || options.update || options.insert){ if(callback || options.update || options.insert || options.action === 'delete'){
dbQuery.asCallback(function(err,r) { dbQuery.asCallback(function(err,r) {
if(err){ if(err){
knexError(dbQuery,options,err) knexError(dbQuery,options,err)