And and Or statements in Where Statements in Knex

Viewed 622

I've been struggling with this problem for hours now... And I can not seem to figure out how to execute the following query using knex query builder...

select * from persons where first_name = "John" and (id_card_number = "1234" or id_card_number_2 = "5678")

Any help would be appreciated.

Thanks

Best Rick

1 Answers

You can pass a function to where, which Knex will wrap all internal changes with parenthesis.

It will look like this:


knex('persons')
  .where('first_name', 'John')
  .where((whereBuilder) =>
    whereBuilder.where('id_card_number', '1234').orWhere('id_card_number_2', '5678')
  );
Related