SkipUndefined in objection.js

Ok today I will create a function to get all the tickets. This function will take one parameter called quantity.
This quantity will include only the tickets whose number is greater than the quantity parameter, but the parameter is optional. This means that if the function does not have the parameter it will get all the tickets without any conditional.
Easy I think, I can use and if and call it a day.

1
2
3
4
5
6
7
function greaterThan(quantity) {
const query = Ticket.query().select('id', 'number');
if (quantity) {
query.where('number', '>', quantity);
}
return query;
}

Great, let’s take a pisco sour at the Bolivar Hotel 🍹. Well not so fast…

SkipUndefined

Objection.js have a built in method to avoid the ugly thing I did above: skipUndefined.
My code must be rewritten like this:

1
2
3
4
5
6
function greaterThan(quantity) {
return Ticket.query()
.select('id', 'number')
.skipUndefined()
.where('number', '>', quantity);
}

Looks cleaner and can be really powerful if you have many optional parameters 👊