From 4422e9e4242188fdaf9e5d5916937a22fbd02591 Mon Sep 17 00:00:00 2001 From: Jose Varela Date: Fri, 30 Nov 2018 09:34:27 +0000 Subject: [PATCH] Handle conditional arrays --- index.js | 11 +++++++++-- test.js | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 029fc1d..d19f546 100644 --- a/index.js +++ b/index.js @@ -62,8 +62,15 @@ function addCondition (q, field, val) { // SQL operator val = [ val[0], field ].concat(val.slice(1)); } else { - // other cases like ( '>', '10' ) Greater than 10 - val = [ 'AND', field ].concat(val); + // Cases when we have something like 'OR_ILIKE' or 'AND_@>' + var operators = /(\w+)_(\w+)/.exec(val[0]) + var operatorsExist = operators && operators.constructor === Array && operators.length >= 3 + if (operatorsExist) { + val = [operators[1], field].concat([operators[2]], val.slice(1)); + } else { + // other cases like ( '>', '10' ) Greater than 10 + val = [ 'AND', field ].concat(val); + } } } var args = val[0].includes('RAW') ? [ '"'+val[1]+'" ' + val[2] ] : val.slice(1) diff --git a/test.js b/test.js index 2d33f3d..2e9cfd7 100644 --- a/test.js +++ b/test.js @@ -138,6 +138,11 @@ var conditions= [ input: [ { f1: { $raw: '@> ANY(ARRAY(1,2,3))' } }, { f2: 20 } ], output: 'select * where (("f1" @> ANY(ARRAY(1,2,3))) or ("f2" = 20))' }, + { + name: 'handle simple or condition with conditional array', + input: { f1: [['ILIKE', 'awesome'], ['OR_ILIKE', '%super%'] ] }, + output: 'select * where (("f1" ILIKE \'awesome\' or "f1" ILIKE \'%super%\'))' + } ];