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

api-mongoose

v0.1.2-pre

Published

Given a Mongoose model and an array of URI params, construct a query for use in a search API.

Downloads

5

Readme

Overview

If you use Mongoose to help serve results for API calls, you might be used to handling calls like:

/monsters?color=purple&eats_humans=true

mongoose-api-query handles some of that busywork for you. Pass in a vanilla object (e.g. req.query) and query conditions will be cast to their appropriate types according to your Mongoose schema. For example, if you have a boolean defined in your schema, we'll convert the eats_humans=true to a boolean for searching.

It also adds a ton of additional search operators, like less than, greater than, not equal, near (for geosearch), in, and all. You can find a full list below.

When searching strings, by default it does a partial, case-insensitive match. (Which is not the default in MongoDB.)

Usage

Apply the plugin to any schema in the usual Mongoose fashion:

monsterSchema.plugin(mongooseApiQuery);

Then call it like you would using Model.find. This returns a Mongoose.Query:

Monster.apiQuery(req.query).exec(...

Or pass a callback in and it will run .exec for you:

Monster.apiQuery(req.query, function(err, monsters){...

Examples

t, y, and 1 are all aliases for true:

/monsters?eats_humans=y&scary=1

Match on a nested property:

/monsters?foods.name=kale

Use exact matching:

/monsters?foods.name={exact}KALE

Matches either kale or beets:

/monsters?foods.name=kale,beets

Matches only where kale and beets are both present:

/monsters?foods.name={all}kale,beets

Numeric operators:

/monsters?monster_id={gte}30&age={lt}50

Combine operators:

/monsters?monster_id={gte}30{lt}50

geo near, with (optional) radius in miles:

/monsters?latlon={near}38.8977,-77.0366
/monsters?latlon={near}38.8977,-77.0366,10
Pagination
/monsters?page=2
/monsters?page=4&per_page=25 		// per_page defaults to 10
Sorting results
/monsters?sort_by=name
/monsters?sort_by=name,desc
Schemaless search

Do you have a property defined in your schema like data: {}, that can have anything inside it? You can search that, too, and it will be treated as a string.

Search Operators

This is a list of the optional search operators you can use for each SchemaType.

Number

  • number={all}123,456 - Both 123 and 456 must be present
  • number={nin}123,456 - Neither 123 nor 456
  • number={in}123,456 - Either 123 or 456
  • number={gt}123 - > 123
  • number={gte}123 - >= 123
  • number={lt}123 - < 123
  • number={lte}123 - <=123
  • number={ne}123 - Not 123
  • number={mod}10,2 - Where (number / 10) has remainder 2

String

  • string={all}match,batch - Both match and batch must be present
  • string={nin}match,batch - Neither match nor batch
  • string={in}match,batch - Either match or batch
  • string={not}coffee - Not coffee
  • string={exact}CoFeEe - Case-sensitive exact match of "CoFeEe"

Latlon

  • latlon={near}37,-122,5 Near 37,-122, with a 5 mile max radius
  • latlon={near}37,-122 Near 37,-122, no radius limit. Automatically sorts by distance

To run tests

node load_fixtures.js
node app.js
mocha

License

MIT http://mit-license.org/