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

mongoose-jsonapi-filter

v0.6.0

Published

easily using map json api filter in query string to mongoose schema

Downloads

3

Readme

Mongoose JSON API Filter

Install

run npm i mongoose-jsonapi-filter --save or yarn add mongoose-jsonapi-filter

Basic Usage

given we have url like this api/user?filter[email][is][email protected] you can use this module in your body like this

import parseFilter from 'mongoose-jsonapi-filter';
import UserModel from './modelPath/user';

// express router / controller
app.get('/api/users', async (req, res) => {
  const { filter } = req.query;

  // then you can do this
  const parsedFilter = parseFilter(filter);

  /* it will actually transform like the above url this
    {
      email : {
        '$eq': '[email protected]'
      }
    }
  */
  // so you can pass it directly to mongoose
  const users = await User.find(filter);
  res.json({ data: users });
});

Sanitize field

you might want to sanitize filter. for example you might want to disable filter on password field. you can do it by passing array of field to the second parameter

import parseFilter from 'mongoose-jsonapi-filter';
import UserModel from './modelPath/user';

// express router / controller
app.get('/api/users', async (req, res) => {
  const { filter } = req.query;
  const sanitizedField = ['password'];
  // then you can do this
  const parsedFilter = parseFilter(filter, sanitizedField);

  /* it will still actually transform like the above url this
    {
      email : {
        '$eq': '[email protected]'
      }
    }
  */

  // so you can pass it directly to mongoose
  const users = await User.find(filter);
  res.json({ data: users });
});

Default mapping

By default this module will map keyword 'is' to 'equal' and 'like' to 'regex' and will also transform the string to regex

Extending Default Mapping

You can extends default mapping by passing it as the third argument

for example you want url to be formated like api/user?filter[email][damnnotequal][email protected] you can see available format in mongodb here (https://docs.mongodb.com/manual/reference/operator/query/#comparison)[https://docs.mongodb.com/manual/reference/operator/query/#comparison]

you can have

import parseFilter from 'mongoose-jsonapi-filter';
import UserModel from './modelPath/user';

// express router / controller
app.get('/api/users', async (req, res) => {
  const { filter } = req.query;
  const sanitizedField = ['password'];
  const customMapping = {
    // without the $ sign, this module will automatically add it for you
    damnnotequal: 'ne',
  };
  // then you can do this
  const parsedFilter = parseFilter(filter, sanitizedField, customMapping);

  /* it will still actually transform like the above url this
    {
      email : {
        '$ne': '[email protected]'
      }
    }
  */

  // so you can pass it directly to mongoose
  const users = await User.find(filter);
  res.json({ data: users });
});

Filter Mapping Value to Regex

by default this module will map the value of filter with key 'like' into 'regex'

given we have url like this api/user?filter[email][like][email protected] you can use this module in your body like this

import parseFilter from 'mongoose-jsonapi-filter';
import UserModel from './modelPath/user';

// express router / controller
app.get('/api/users', async (req, res) => {
  const { filter } = req.query;
  const sanitizedField = ['password'];
  // then you can do this
  const parsedFilter = parseFilter(filter, sanitizedField);

  /* it will still actually transform like the above url this
    {
      email : {
        '$regex': /[email protected]/gmi
      }
    }
  */

  // so you can pass it directly to mongoose
  const users = await User.find(filter);
  res.json({ data: users });
});

extend filter mapping value that transformed to regex

given we have url like this api/user?filter[email][helllike][email protected] you can use this module in your body like this and you want to transform hellike into regex you will need to use a regex for value in mapping like this

import parseFilter from 'mongoose-jsonapi-filter';
import UserModel from './modelPath/user';

// express router / controller
app.get('/api/users', async (req, res) => {
  const { filter } = req.query;
  const sanitizedField = ['password'];
  const customMapping = {
    // without the $ sign, this module will automatically add it for you
    hellike: 'regex',
  };
  // then you can do this
  const parsedFilter = parseFilter(filter, sanitizedField, customMapping);

  /* it will still actually transform like the above url this
    {
      email : {
        '$regex': /[email protected]/gmi
      }
    }
  */

  // so you can pass it directly to mongoose
  const users = await User.find(filter);
  res.json({ data: users });
});