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

get-query-modifier

v1.0.0

Published

Helper for parsing mongoose mongodb query operators.

Downloads

9

Readme

get-query-modifier

Build Status Coverage Status devDependency Status npm downloads per month


Helper for parsing mongoose mongodb query operators.

What is this?

get-query-modifier is a module designed to help with mongoose query parsing. Let's say you have a GET /api/comments endpoint, in your API like the following:

app.get('/api/comments', function(req, res) {
  Comment
    .find()
    .lean()
    .exec(function(err, comments) {
      if(err) res.json(err.status || 500, { error: err.message });
      else res.json({ comments: comments });
    });
});

Suddenly, you decided to add pagination to this route. You could easily implement calls with mongoose's Query.prototype.limit and Query.prototype.skip, as follows:

app.get('/api/comments', function(req, res) {
  Comment
    .find()
    .skip(req.query.page * req.query.items_per_page)
    .limit(req.query.items_per_page)
    .exec(/**/);
});

Or something like that.

You'd be, however, recreating your own query syntax.

You can get a lot more power by using mongodb's default query syntax, extending it where mongoose/mongodb expose a slightly different API, such as .limit, .skip etc.

get-query-modifier solves this solution by extracting the operators $sort, $limit, $skip, $page and $select (plus any operator you want to support by setting options.allow), and translating them into a set of method calls to the query object (calls to Query.prototype.sort, Query.prototype.select etc.). This allows maximum flexibility for using both the req.query object for further querying in the mongoose API and supporting a known standard set of operators, which come out-of-the-box in mongodb.

It does it by taking the query object as its first argument and returning a modifier function, which handles calling each of these deferred Query method calls once your Query instance is available.

app.get('/api/comments', function(req, res) {
  var modifier = getQueryModifier(req.query);
  // `req.query` had its `$limit`, `$sort`, `$page`, `$skip` and `$select`
  // fields striped from it and translated into a set of method calls in
  // `modifier`

  var query = Comment
    .find(req.query) // we can create an arbitrary Query, re-using `req.query`
    .lean();

  modifier(query) // modifier applies the extracted operators to the Query
    .exec(/**/);
});

getQueryModifier(query, [options])

Manipulates an object representation of a querystring, extracting its mongoose query operators and returning a function to attach them to a mongoose.Query object.

Note this function isn't pure and does delete the operators from the query parameter after reading them.

Params:

  • Object query The querystring representation to manipulate
  • Object [options] An options object
  • Object [options.defaults] An object with defaults for the valid operators
  • Object [options.ignore] An object with the properties corresponding to the ignored operators set to true
  • Boolean [options.deleteIgnored=false] Whether to delete the ignored operators from the query object
  • Array. [options.allow] An array of operators to parse in addition to the plugin's default VALID_OPERATORS.

Return:

  • Function queryModifier The mongoose.Query modifier function, which attaches the operators to the search

Example:

var app = require('./app'); // some express app
var mongoose = require('mongoose');
var User = mongoose.model('User'); // some mongoose model

app.get('/api/users', function(req, res) {
  var modifier = getQueryModifier(req.query);
  var query = modifier(User.find(req.query).lean());

  query.exec(function(err, results) {
    // use the results...
  });
});

See the tests for more elaborate examples.

getQueryModifier.middleware([options])

A connect middleware for parsing and using mongodb query operators with mongoose given an object representation of a query.

Params:

  • Object [options] An options object which will simply be passed onto getQueryModifier(req.query, options)

Return:

  • Function mdw The middleware function

Example:

var app = require('./app'); // some express app
app.use(getQueryModifier.middleware());

License

This code is licensed under the MIT License. See LICENSE for more information.