Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knex JS "OR" Inside .where()?

Does anyone have any ideea how to write an OR logic like the ' || ' operator in JS I tried inside my knex to do it like that ->

knex('something')
  .where({ state: BOOKING_STATE.ACCEPTED } || { state: BOOKING_STATE.WAITING_LIST})

however it fails and i can't find anything about it in the docs

like image 928
TempAcct4Stack Avatar asked Dec 12 '18 21:12

TempAcct4Stack


2 Answers

If you check the documentation for where clauses with Knex, you'll see a grouped chain where, if you're trying to have multiple where clauses, but only want to or the two you listed, then something like this would work.

knex('something')
.where(function() {
  this.where('state', BOOKING_STATE.ACCEPTED ).orWhere('state', BOOKING_STATE.WAITING_LIST)
})
.andWhere('something', something_else)
...;
like image 130
technogeek1995 Avatar answered Sep 19 '22 04:09

technogeek1995


You should be able to use a combination of where() and orWhere():

knex('something')
  .where({ state: BOOKING_STATE.ACCEPTED })
  .orWhere({state: BOOKING_STATE.WAITING_LIST })
like image 44
Joel Brewer Avatar answered Sep 23 '22 04:09

Joel Brewer