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

koskimas picture koskimas  ·  3Comments

zettam picture zettam  ·  3Comments

ghost picture ghost  ·  3Comments

tjwebb picture tjwebb  ·  3Comments

nklhrstv picture nklhrstv  ·  3Comments