Turns
const User = const query = User if requestinput'name' query = query if requestinput'city' query = query query
into
const User = User
Installation
npm i adonis-lucid-optional-queries --save
Registering provider
Make sure to register the provider inside start/app.js
const providers = 'adonis-lucid-optional-queries/providers/OptionalQueriesProvider'
Usage
First add the trait to the model.
const Model = static { super this }
Finally use the method as follows
const User = User
In the above query it will add active
and group
to the WHERE condition at all times.
For everything within the optional
closure it will only add the condition if the values are not falsy.
That means if name
and city
are filled in the request, but not zip
and birthday
, the WHERE condition would be for example:
select * from users where `active` = true and `group` = 1 and `name` = 'Lukas' and `city` = 'Tokyo';
What is considered falsy is: undefined, null, [], '', false
What is not considered falsy: 0, '0'
tests
Run tests using
npm test