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

@lperanni/query-parser

v1.0.3

Published

Query params to operation mapper for Express API-s

Downloads

6

Readme

Query Parser

Travis License Size

This Express query-parser/mapper provides middleware functions that parse the request query written wth a specific syntax and enable easier filtering of resources on the backend. The operations are modeled after Sequelize's operators, but mappers for other ORM's or custom solutions can be easily added as the base parser is library-agnostic

Installation

npm install @lperanni/query-parser

Usage

Let's assume a request query targeting the 'User' resource with the following query object

{
  limit: '6',
  offset: '0',
  name: 'ts.UserOne',
  randomParam: 'thisisnotpartofthelang',
  notSupportedOperation: 'op.Test',
  id: 'in.9,8,7,6',
  available: 'eq.false',
  created_at: 'lte.20/07/2020'
};

In this case we'll assume an Express Rest Api with Sequelize as it's ORM library

const { sequelizeMapper } = require('@lperanni/query-parser')

router.get('/user', sequelizeMapper , (req, res, next) => {
  const { operations, limit, offset } = req.query;
  User.findAll({ 
    limit,
    offset,
    where: operations
  })
}));

What happens in the background is that the sequelizeMapper takes the object and parses all keys with operations supported by this package and adds them to req.query.operations transformed into the appropriate operators. So the above query object turns into this:

{
  limit: '6',
  offset: '0',
  randomParam: 'thisisnotpartofthelang',
  notSupportedOperation: 'op.Test',
  operations: {
    name: { [Op.iLike]: '%Text%' },
    id: [9,8,7,6],
    available: { [Op.eq]: false },
    startDate: { [Op.lte]: '20/07/2020' }
  }
}

The following is a list of supported operations

const supportedOperations = [
  'ts',   //text search
  'eq',   // equals
  'ne',   // not equal
  'lt',   // less than
  'in',   
  'lte',  // less than or equal
  'gt',   // greater than 
  'gte',  // greater than or equal
  'between',
  'like',
  'notLike',
  'iLike',
  'notILike',
  'is',
  'not',
  'or',
  'notBetween',
  'startsWith',
  'endsWith',
  'substring',
  'regexp',
  'notRegexp',
  'iRegexp',
  'notIRegexp'
];

If you want/need to use this package to map custom operations to the functions all you need to do is write a custom mapper and pass it as a param to the baseMapper function

const { base } = require('@lperanni/query-parser');
const { customMapper } = require('../lib')

const customMiddleware = base(customMapper);

router.get('/user', customMiddleware , (req, res, next) => { ...

Contributions

Any contributions are welcome. If you want to support this package by contributing fixes and changes then fork it, create a new branch and submit a Pull Request and i will try to check it as soon as possible

License

ISC