I'm trying to make a where clause with custom functions applied to the columns using Knex.js.
Suppose I have a table named tableName
with columns named col1, col2, col3
and a function f
that receives as parameter something that is the same type of the things that are in col1
and col2
.
I also have two variables named var1
and var2
(defined beforehand) that are the same type of the thing returned by f
. I tried some ways.
Example 1:
let rows = knexClient("tableName").whereRaw('f(?) <= ${var1} AND f(?) >= ${var2}', [col1, col2]).then((rows) => {
for (row of rows) {
console.log('${row["col1"]} ${row["col2"]} ${row["col3"]}');
}
}).catch((err) => {
console.log(err);
throw err;
});
This gives the following error:
ReferenceError: col1 is not defined.
Example 2:
let rows = knexClient("tableName").whereRaw("f(col1) <= ? AND f(col2) >= ?", [var1, var2]).then((rows) => {
for (row of rows) {
console.log('${row["col1"]} ${row["col2"]} ${row["col3"]}');
}
}).catch((err) => {
console.log(err);
throw err;
});
This gives the following error:
SQLITE_ERROR: no such column: col1] {
errno: 1,
code: 'SQLITE_ERROR'
}
What is the right way to do it? I have searched around and saw some people doing things similar to my first try here. But it didn't work for me.