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

@kartikyathakur/nestjs-query-filter

v1.1.88

Published

Package providing decorators for NestJS to easily filter data thorugh query parameters

Downloads

126

Readme

@kartikyathakur/nestjs-query-filter

npm version License: MIT

Your savior from the trivial yet time consuming task of facilitating filteration on NestJS RESTful APIs. This package provides decorators for NestJS to easily filter data thorugh query parameters and currently supports:

Table of Contents

Installation

npm install --save @kartikyathakur/nestjs-query-filter

How to construct a filter

The idea is to have a standard way of filtering data in RESTful APIs, the convention is as follows:

/[route]?filter.[field].[type].[operator].[value]

field

This is the field that you want to filter on, for example name, age, email, etc.

type

This is the type of the field, currently supported types are:

| Type | Description | |------|-------------| | string | String | | number | Number | | boolean | Boolean | | date | Date |

operator

The following operators are supported for comparison:

| Operator | Description | String | Number | Boolean | Date | |----------|-------------|--------|--------|---------|------| | eq | Equal | ✅ | ✅ | ✅ | ✅ | | ne | Not equal | ✅ | ✅ | ✅ | ✅ | | gt | Greater than | ⛌ | ✅ | ⛌ | ✅ | | gte | Greater than or equal | ⛌ | ✅ | ⛌ | ✅ | | lt | Less than | ⛌ | ✅ | ⛌ | ✅ | | lte | Less than or equal | ⛌ | ✅ | ⛌ | ✅ | | in | In | ✅ | ✅ | ⛌ | ✅ | | nin | Not in | ✅ | ✅ | ⛌ | ✅ | | regex | Regular expression | ✅ | ⛌ | ⛌ | ⛌ |

value

The value is the value that you want to compare the field against.

@GenerateORMFilter

Let's look at a simple example, given we want to get all users with the name Ezio Auditore we will need to specify this requestUrl:

/users?filter.name=string.eq.Ezio%20Auditore

The resulting filter will be

{ "name": { "$eq": "Ezio Auditore" }}

For a more complex example, let’s say we only want to get users with the name like Connor and age between 21 and 30, our requestUrl will be:

/users?filter.name=string.regex.Connor&filter.age=number.gte.21
 &filter.age=number.lt.30

The resulting filter will be

{
  "name": { "$regex": "Connor" },
  "age": { "$gte": 21, "$lt": 30 }
}

Nested objects are also supported, let's say we want to get all users for games that were released after 2012, our requestUrl will be:

/users?filter.game.year=date.gte.2012

The resulting filter will be

{
  "game.year": { "$gte": "2012" }
}

Sample Usage

Add the decorator @GenerateORMFilter to the controller on which you want to apply the filters. Then make use of the ORMFilter by simply passing them to your ORM.

import { GenerateORMFilter, ORMFilter } from '@kartikyathakur/nestjs-query-filter';

@Get('/users')
async findAll(@GenerateORMFilter() ormFilter: ORMFilter) {
  // You can use the ormFilter to filter your data, directly passing it to Mongoose/TypeORM/Sequelize/Knex
  // For example:
  const users = await this.userService.findAll(ormFilter);
  return users;
}

@GenerateArrayFilter

There are cases where the processed data needs to be filtered in memory Unlike the @GenerateORMFilter decorator, the @GenerateArrayFilter decorator will return a function that can serve as predicate for the filter.

Let's look at a simple example, given we want to get all users with the name Ezio Auditore we will need to specify this requestUrl:

/users/profile?filter.name=string.eq.Ezio%20Auditore

The resulting predicate will be

(user: User) => user.name === 'Ezio Auditore'

For a more complex example, let’s say we only want to get users with the name like Connor and age between 21 and 30, our requestUrl will be:

/users?filter.name=string.regex.Connor&filter.age=number.gte.21
 &filter.age=number.lt.30

The resulting predicate will be

(user: User) => new RegExp('Connor').test(user.name) && user.age >= 21 && user.age < 30

Nested objects are also supported, let's say we want to get all users for games that were released after 2012, our requestUrl will be:

/users?filter.game.year=date.gte.2012

The resulting predicate will be

(user: User) => user.game.year >= 2012

Sample Usage

Add the decorator @GenerateArrayFilter to the controller on which you want to apply the filters. Then make use of the ArrayFilter as the filter predicate for the array.

import { GenerateArrayFilter, ArrayFilter } from '@kartikyathakur/nestjs-query-filter';

@Get('/users/comparison')
async findAll(@GenerateArrayFilter() arrayFilter: ArrayFilter) {
  // The array can be hard coded or fetched from a database
  const users = await this.userService.findAll();
  // ...
  // [Any business logic that is needed to be applied before filtering]
  // ...
  // Now you can the arrayFilter to filter the data in memory
  return users.filter(arrayFilter);
}