I have a built a query with Knex as follows:
knex('discounts')
.where((builder) => {
builder
.where('product_code', productCode)
.where((builder1) => {
builder1
.andWhere('account_code', customer)
.orWhere('account_code', null);
});
})
.select('*');
Everything works fine, but I felt like the .where statement is too long so tried to use the .whereIn function, and that's when I realized it doesn't work:
knex('discounts')
.where((builder) => {
builder
.where('product_code', productCode)
.whereIn('account_code', [customer, null]);
})
.select('*');
I am aware that we cannot use null with an IN in raw SQL and it should be done like so:
WHERE
(columnName IN ('value1', 'value2', 'value3') OR columnName IS NULL)
My question: is my initial query the only way to achieve this or is there any alternative way to use .whereIn with a null when using Knex?