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

generic-query-parser

v0.1.5

Published

Parse request query object to conditions, projection, and options (sort, skip, limit) to use for DB queries

Downloads

3,343

Readme

Generic Query Parser

Convert a query string object to a generic db query.

Generates conditions, projection, and options (sort, skip, limit).

Works for generic usage or even better in combination with an ORM. You can map REST APIs to mongodb driver, mongoose or sequelize queries. Express and Koa middlewares are already provided.

Installation

npm install --save generic-query-parser

Usage

You can manually parse a query string or use one of the middlewares already provided to easily consume the parsed object in your server code.

Manual parse

// define your query
var query = `
    filter[age]=32
    &operator[age]=gte
    &type[age]=number
    &filter[name]=^A.*
    &type[name]=regexp
    &limit=3
    &skip=0
`;

// require query string module (nested structures support)
var qs = require('qs');

// parse query to a nested object object
var requestQuery = qs.parse(query);

// require generic query parser
var Parser = require('generic-query-parser');

// parse request query to a db query
var dbQuery = Parser.parse(requestQuery)

// use parsed object
dbQuery.conditions;
dbQuery.projection;
dbQuery.options;

Middleware

Express

Simply use expressMiddleware provided with the module and req.custom in each subsequent middleware will be populated with conditions, projection and options.

// create new express app
var express = require('express'),
    app = express();

// require generic query parser
var Parser = require('generic-query-parser');

// use middleware
app.use(Parser.expressMiddleware);

// consume parsed results
app.use(function(req,res,next){
    var dbQuery = req.custom;

    dbQuery.conditions;
    dbQuery.projection;
    dbQuery.options;
})

Koa

Simply use koaMiddleware provided with the module and this.custom in each subsequent middleware will be populated with conditions, projection and options.

NOTE: Koa native query string module doesn't support nested structures. You need to use koa-qs module in order to use filter conditions.

// create new koa app
var koa = require('koa'),
    app = koa();

// use koa-qs module to parse query string to nested object
require('koa-qs')(app)

// require generic query parser
var Parser = require('generic-query-parser');

// use middleware
app.use(Parser.koaMiddleware);

// consume parsed results
app.use(function*(next){
    var dbQuery = this.custom;

    dbQuery.conditions;
    dbQuery.projection;
    dbQuery.options;
})

Documentation

Methods

parse(queryObject)

Parse a query string object to a db query

params

  • Object queryObject: query string object

return

  • Object: parsed query composed by: conditions, projection, options

expressMiddleware(req, res, next)

Use as a middleware for Express

koaMiddleware(next)

Use as a middleware for Koa

Syntax

Filters

Convert filter, operator and type from query to conditions object

Example:

// age greather than or equal to 32 (converted to a number)
'filter[age]=32&operation[age]=gte&type[age]=number'
Filter values

Filter attributes by given value. Adds a condition for each attribute name received.

  • Object filter: filter values
  • String filter[attribute]: value to filter for given attribute

Example:

// age 32
'filter[age]=32'

// color red
'filter[color]=red'

// name starts with 'A'
'filter[name]=^A.*'
Filter operators

Apply an operator to a filter value. Adds the given operator to the conditions.

  • Object operator: filter operators
  • String operator[attribute]: filter operator for the attribute, one of eq, gt, gte, lt, lte or ne

Example:

// age greather or equal than
'operation[age]=gte'
Filter types

Apply an type to a filter value. Converts the value to the given type.

Some ORMs (or DBs) might do an implicit conversion and directly accept a string as parameter even if the db type is different. If this is not your case just use the type parameter.

  • Object type: filter types
  • String type[attribute]: filter type for attribute, one of string, number, date, bool, regexp or null

Example:

// given age value is a number
'type[age]=number'

// given name value is a regex
'type[name]=regex'
Projection
  • String fields: list of desired fields in the projection, comma or space separated
Sort
  • String sort: list of sorting attributes, comma or space separated
Range
  • Number limit: results amount, default 0
  • Number skip: results offset, default 0

Example:

// get the first 10
'skip=0&limit=10'

// get the next 10
'skip=10&limit=10'

LICENSE: MIT