Socket
Socket
Sign inDemoInstall

restful-filter

Package Overview
Dependencies
1
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    restful-filter

Simple library to parse restful filter based on http querystring


Version published
Weekly downloads
430
increased by55.23%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Restful filter

This library aim to convert querystring parameters into parsed json with related operators, so you would able to use the parsed values into another query action like filtering by using your model on SQL library (knex, sequelize, etc).

Features

  • Filter querystring parameters
  • Parse pagination by using page and count parameter
  • Parse ordering by using order parameter

Installation

# via npm
npm i restful-filter --save

# via yarn
yarn add rest

Usage

const restfulFilter = require('restful-filter')

const filter = restfulFilter({ 
    case_sensitive: false // false by default, this is just example
})

# FILTER
# /api/users?name__ilike=aditya&age__eq=25&password__ilike=%a%
.get('/users', (req, res, next) => {
  const queryParams = req.query
  const allowedColumn = ['name', 'age']
  const searchParams = filter.parse(queryParams, allowedColumn).filter

  # now searchParams contains
  # {
  #   name: {operator: '$iLike', operatorSQL: 'ILIKE', column: 'name', value: 'aditya'},
  #   age: {operator: '$eq', operatorSQL: '=', column: 'age', value: '25'}
  # }
  #
  # password filter will not processed because not listed in the allowedColumn
})

# PAGINATION
# /api/users?page=2
.get('/users', (req, res, next) => {
    const queryParams = req.query
    const paginationParams = filter.parse(queryParams).paginate

    # paginationParams contains
    # {
    #   offset: 20,
    #   limit: 20   
    # }
})

# ORDER
# api/users?order_by=-id,name
.get('/users', (req, res, next) => {
    const queryParams = req.query
    const orderParams = filter.parse(queryParams).order

    # orderParams contains
    # [
    #   ['id', 'DESC']
    #   ['name', 'ASC']
    # ]

    # You even can limit the order column value by using second parameter as allowedColumn to process
    # Like 
    
    const orderParams = filter.parse(queryParams, ['id']).order

    # Would return
    # [
    #   ['id', 'DESC']
    # ]
})

Configuration

KeyDefault ValueDescription
case_sensitivefalseUse case sensitive on query string parameter and parameter value
page_param_namepagePage parameter name to be used on querystring
limit_param_namecountLimit parameter name to be used on querystring
per_page20Default number count per request if not set
max_count_per_page100Maximum count for per_page parameter, so for example if count parameter value is higher than 100, the return value would stick to 100
order_param_nameorder_byOrder parameter name to be used on querystring

Operators

OperatorDescriptionExample
__eqFind column equal with value?name__eq=smith
__notNot same with given value?active__not=true
__neNegation, the opposite of equal?name__ne=smith
__ltLower than?age__lt=10
__gtGreater than?age__gt=15
__lteLower than and equal?age__lte=25
__gteGreater than and equal?age__gte=20
__likeLike with case sensitive?name__like=smith
__ilikeLike with case insensitive (Postgres)?name__ilike=smith
__notLikeOpposite of like with case sensitive?name__notLike=smith
__notILikeOpposite of like with case insensitive (Postgres)?name__notILike=smith
__inFind value which listed on given list?city__in=jakarta,bandung,bekasi
__notInFind value which not listed in given list?city__notIn=bogor,depok,tangerang
__containsFind value that contains in given list (Postgres)?name__contains=smith
__betweenFind value which between 2 given values?date__between=2015-06-18,2017-05-31
__notBetweenFind value which is not between 2 given values?date__notBetween=2015-06-18,2017-05-31

Test

npm run test

Note

Please open a pull request for further abilities and another issues

License

MIT

Keywords

FAQs

Last updated on 14 Mar 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc