npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ordent/adonis-lucid-filter

v1.0.6

Published

Addon for filtering AdonisJS Lucid ORM

Downloads

4

Readme

Adonis Lucid Filter

Note: New realese for Adonis v5 work in progress

Greenkeeper badge Build Status Coverage Status

Works with @adonisjs/lucid ^6.1.*

This addon adds the functionality to filter Lucid Models

Inspired by EloquentFilter

Introduction

Example, we want to return a list of users filtered by multiple parameters. When we navigate to:

/users?name=er&last_name=&company_id=2&roles[]=1&roles[]=4&roles[]=7&industry=5

request.all() or request.get() will return:

{
  "name": "er",
  "last_name": "",
  "company_id": 2,
  "roles": [1, 4, 7],
  "industry": 5
}

To filter by all those parameters we would need to do something like:

'use strict'

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

class UserController {

  async index ({ request }) {
    const { company_id, last_name, name, roles, industry } = request.get()
  
    const query = User.query().where('company_id', +company_id)

    if (last_name) {
      query.where('last_name', 'LIKE', `%${last_name}%`)
    }
    if (name) {
      query.where(function () {
        this.where('first_name', 'LIKE', `%${name}%`)
          .orWhere('last_name', 'LIKE', `%${name}%`)
      })
    }

    query.whereHas('roles', (builder) => {
      builder.whereIn('id', roles)
    }).whereHas('industries', (builder) => {
      builder.where('industry_id', +industry)
    })

    return await query.fetch()
  }

}

To filter that same input with Lucid Filters:

'use strict'

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

class UserController {

  async index ({ request }) {
    return await User.query()
      .filter(request.all())
      .fetch()
  }

}

Installation

Make sure to install it using adonis-cli, npm or yarn.

# adonis
adonis install adonis-lucid-filter

# npm
npm i adonis-lucid-filter

# yarn
yarn add adonis-lucid-filter

Usage

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

const providers = [
  'adonis-lucid-filter/providers/LucidFilterProvider'
]

Generating The Filter

Only available if you have registered LucidFilterProvider in the providers array in your `start/app.js'

You can create a model filter with the following ace command:

adonis make:modelFilter User // or UserFilter

Where User is the Lucid Model you are creating the filter for. This will create app/ModelFilters/UserFilter.js

Defining The Filter Logic

Define the filter logic based on the camel cased input key passed to the filter() method.

  • Empty strings are ignored
  • setup() will be called regardless of input
  • _id is dropped from the end of the input to define the method so filtering user_id would use the user() method
  • Input without a corresponding filter method are ignored
  • The value of the key is injected into the method
  • All values are accessible through the this.input() method or a single value by key this.input(key)
  • All QueryBuilder methods are accessible in this context in the model filter class.

To define methods for the following input:

{
  "company_id": 5,
  "name": "Tuck",
  "mobile_phone": "888555"
}

You would use the following methods:

'use strict'

const ModelFilter = use('ModelFilter')

class UserFilter extends ModelFilter {

  // This will filter 'company_id' OR 'company'
  company (id) {
    return this.where('company_id', +id)
  }

  name (name) {
    return this.where(function () {
      this.where('first_name', 'LIKE', `%${name}%`)
        .orWhere('last_name', 'LIKE', `%${name}%`)
    })
  }

  mobilePhone (phone) {
    return this.where('mobile_phone', 'LIKE', `${phone}%`)
  }

  secretMethod (secretParameter) {
    return this.where('some_column', true)
  }
}

Note: In the above example if you do not want _id dropped from the end of the input you can set static get dropId () { return false } on your filter class. Doing this would allow you to have a company() filter method as well as a companyId() filter method.

Note: In the example above all methods inside setup() will be called every time filter() is called on the model

Blacklist

Any methods defined in the blackist array will not be called by the filter. Those methods are normally used for internal filter logic.

The whitelistMethod() methods can be used to dynamically blacklist methods.

In the example above secretMethod() will not be called, even if there is a secret_method key in the input array. In order to call this method it would need to be whitelisted dynamically:

Example:

setup ($query) {
  this.whitelistMethod('secretMethod')
  return this.where('is_admin', true)
}

setup() not may be async

Applying The Filter To A Model

Implement the @provider:Filterable trait on any Lucid model:

'use strict'

const UserFilter = use('App/ModelFilters/UserFilter')

class User extends Model {
  static boot () {
    super.boot()
    this.addTrait('@provider:Filterable', UserFilter)
  }

  // User Class
}

This gives you access to the filter() method that accepts an object of input:

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

class UserController {

  async index ({ request }) {
    return await User.query()
      .filter(request.all())
      .fetch()
  }

  // or with paginate method

  async index ({ request }) {
    const query = request.all()
    const page = query.page || 1

    return await User.query()
      .filter(query)
      .paginate(page, 15)
  }

}

Dynamic Filters

You can define the filter dynamically by passing the filter to use as the second parameter of the filter() method. Defining a filter dynamically will take precedent over any other filters defined for the model.

'use strict'

const AdminFilter = use('App/ModelFilters/AdminFilter')
const UserFilter = use('App/ModelFilters/UserFilter')

class UserController {

  async index ({ request, auth }) {
    const user = await auth.getUser()
    const Filter = user.isAdmin() ? AdminFilter : UserFilter
    
    return await User.query()
      .filter(request.all(), Filter)
      .fetch()
  }
  
}

Filtering By Relationships

A App/Models/User that belongsToMany App/Models/Industry

'use strict'

const Model = use('Model')
const UserFilter = use('App/ModelFilters/UserFilter')

class User extends Model {
  static boot () {
    super.boot()
    this.addTrait('@provider:Filterable', UserFilter)
  }
  industries () {
    return this.belongsToMany('App/Models/Industry')
  }
}

For filter by a relationship use related() method:

'use strict'

const ModelFilter = use('ModelFilter')

class UserFilter extends ModelFilter {
  industry (id) {
    return this.related('industries', 'industry_id', +id)
  }
  
  // or filter by revenue of industry
  
  revenue (revenue) {
    return this.related('industries', 'revenue', '>', revenue)
  }
}

A App/Models/Industry may not have a model filter