Knex: Conditional queries

Created on 23 Nov 2015  路  3Comments  路  Source: knex/knex

Hi,
I don't know if this request exists. But I search a way to build a select query with conditional arguments.
Sample:

knex('mytable')
.select('champ1','champ2','champ3')
.where ('champ1','=',val1)
.andWhere ('champ2','=',val2)
.then{function(rows,err){
})

etc...

But if champ2 is empty or don't fill the condition
I must duplicate the code without the andWhere

knex('mytable')
.select('champ1','champ2','champ3')
.where ('champ1','=',val1)
.then{function(rows,err){
etc...
})

If i give as value2 :'*', the query turn off.
If you a reference, thanks.
Franck

question

Most helpful comment

QueryBuilder#modify

knex('table').select('col_a').modify(function (qb) {
  if (bValue) {
    qb.where('col_b', bValue);
  }
}).then(...

All 3 comments

QueryBuilder#modify

knex('table').select('col_a').modify(function (qb) {
  if (bValue) {
    qb.where('col_b', bValue);
  }
}).then(...

it's perfect for me. Is there a forum ?
Thanks,
Franck

As far as I know github issues is the only forum for knex.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hyperh picture hyperh  路  3Comments

mattgrande picture mattgrande  路  3Comments

aj0strow picture aj0strow  路  3Comments

mtom55 picture mtom55  路  3Comments

legomind picture legomind  路  3Comments