adonis-lucid-when

Implements Laravel's when method as a trait in AdonisJs

Usage no npm install needed!

<script type="module">
  import adonisLucidWhen from 'https://cdn.skypack.dev/adonis-lucid-when';
</script>

README

Turns

const User = use('App/Models/User')

const query = User.query().where('active', true)

if (request.input('name')) {
  query = query.where('name', request.input('name'))
}

if (request.input('city')) {
  query = query.where('city', request.input('city'))
}

query.fetch()

into

const User = use('App/Models/User')

User.query()
    .where('active', true)
    .when(request.input('name'), (q, value) => q.where('name', value))
    .when(request.input('city'), (q, value) => q.where('city', value))
    .fetch()

Installation

npm i adonis-lucid-when --save

Registering provider

Make sure to register the provider inside start/app.js

const providers = [
  'adonis-lucid-when/providers/WhenProvider'
]

Usage

First add the trait to the model.

const Model = use('Model')

class User extends Model {
  static boot() {
    super.boot()

    this.addTrait('@provider:Lucid/When')
  }
}

Finally use the method as in the example above.

Apply default value

const User = use('App/Models/User')

User.query()
    .where('active', true)
    .when(request.input('status'), (q, value) => q.where('status', value), 1)
    .when(request.input('is_deleted'), (q, value) => q.where('is_deleted', value), false)
    .fetch()

The third parameter deals as a default value in case the first variable is falsy.

0 and '0' are not considered falsy

empty array / object is considered falsy

tests

Run tests using

npm test