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

qs2mongo

v1.3.0

Published

qs2mongo

Downloads

483

Readme

qs2mongo

NPM version

Querystring to mongo mapper

Builds filters, projections and options for mongoose queries

  • In your querystring:
    • attributes=comma,separated,fields maps to projection { comma: 1, separated: 1, fields: 1 }
    • limit=1,offset=2 maps to options { limit: 1, offset: 2 }
    • Sorts
      • Ascending: sort=field maps to options { sort: { field: 1 } }
      • Descending: sort=-field maps to options { sort: { field: -1 } }
    • Any other key maps to a mongo filter. Available options are:
      • strict: key=value is { key: value }
      • not strict: key=value is { key: /value/gi }
      • $or: key,anotherKey=value is $or: [{key:value}, {anotherKey:value}]
      • $in : key__in=a,b,c is key: $in: ["a","b","c"]
      • unary operators: key__gt=value is {key: { $gt: "value"} }

Basic usage

Qs2Mongo = require ("qs2mongo")

#Bind types to mongoose schema
qs2mongo = Qs2Mongo.MongooseSchema YourMongooseSchema, {...otherOptions...}

#Or manually specify types
qs2mongo = Qs2Mongo.ManualSchema {
  filterableBooleans, 
  filterableNumbers,
  filterableDates,
  filterableObjectIds
}, {...otherOptions...}

#... 
anEndpoint: (req, res): =>
  { filters, projection, options } = qs2mongo.parse req

Usage as middleware

Qs2Mongo = require ("qs2mongo")
config = ...
qs2mongo = new Qs2Mongo config
#... 
router.use qs2mongo.middleware

#... 
anEndpoint: (req, res): =>
  { filters, projection, options } = req.mongo

Config

  {
    defaultSort, 
    idField = "id", 
    multigetIdField = "_id", 
    omitableProperties = Qs2Mongo.defaultOmitableProperties
  }

Advanced

Default driver is mongodb. If you'd like to use a custom mongodb driver, you must provide your own type conversion implementation as follows:

{
  toObjectId: (it) -> #your conversion

  toNumber: (it) -> #your conversion

  toBoolean: (it) -> #your conversion

  toDate: (it) -> #your conversion
}

Note: Your conversion must return the properly converted value if it is valid or undefined otherwise.

Specify the path to your implementation in process.env.QS_TRANSFORMER_DRIVER