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

sluicebox

v0.4.0

Published

Quickly and elegantly add search, sort and pagination to your plural resource endpoints.

Downloads

4

Readme

SluiceBox

Add search, sort and pagination to your plural resource endpoints.

Getting Started

Install Sluice Box:

npm install sluicebox --save

Overview

SluiceBox is a bundle of helpers for creating robust and feature-rich plural resource endpoints. It has helpers for pagination, searching and sorting. It is currently focused on resources stored in MongoDB, but support for other persistent stores may be added in the future. Sluicbox pagination works particularly well with the express-lane enhanced routing library. Sluicebox searching, sorting and pagination works particularly well with the meerkat (and/or timon) wrapper for the mongo-native driver.

Searching

Searches are expressed as a collection of clauses joined by conjunctions: ;(and) to further refine a search, |(or) to broaden the search. Clauses may be either simple predicates or groups of subordinate clauses joined together by conjunctions and wrapped in parenthesis (...). Predicates are composed of a field name and a value separated by a comparison. Comparisons are either a lone comparator or a !(not) followed by a comparator. The currently supported comparators are: :(equal), *(contains), ^(starts with), $(ends_with), <(less than) and >(greater than).

A simple search to find all users that have a gmail email address could be expressed as:

/api/versions/1/users?q=email$gmail.com

To refine our search to only active users we can simply add another predicate:

/api/versions/1/users?q=email$gmail.com;state:active

A complex search to find all active users with either gmail or yahoo addresses could be expressed as:

/api/versions/1/users?q=state:active;(email$gmail.com|email$yahoo.com)

The Code

This example contains constructs from express-lane and timon for simplicity.

sluicebox = require('sluicebox')()
{ Account } = require '../models'
module.exports =
  get: (req, res, next) ->
    sluicebox.search req.query.q,
      (query) ->
        Account(req).find(query).all (accounts) ->
          res.json { accounts }
      (error) ->
        res.status(422).json error

Sorting

Sorting is specified as a comma separated list of one or more sort descriptors. A sort descriptor is a field name optionally followed by a direction. A direction is one of asc or desc. When a direction is not specified the descriptor defaults to asc.

A simple sort that orders the results lexicographically by last name could be expressed as:

/api/versions/1/users?sort=name.last asc

or

/api/versions/1/users?sort=name.last

A simple sort that orders the most recently created accounts first could be expressed as:

/api/verisons/1/users?sort=created_at desc

A compound sort that orders the results lexicographically by last name and orders people who have the last name lexicographically by first name could be expressed as:

/api/versions/1/users?sort=name.last,name.first

The Code

Lets add sorting to our previous example.

sluicebox = require('sluicebox')()
{ Account } = require '../models'
module.exports =
  get: (req, res, next) ->
    sluicebox.search req.query.q,
      (query) ->
        sort = sluicebox.sort req.query.sort
        Account(req).find(query).sort(sort).all (accounts) ->
          res.json { accounts }
      (error) ->
        res.status(422).json error

Pagination

The pagination complete with hypermedia can be easily added to result sets. This is particularly easy when paired with meerkat or timon.

The third page of a result set with 20 results per page could be requested as:

/api/versions/1/users?page=3&per=20

The Code

Lets add pagination to our previous example.

sluicebox = require('sluicebox')()
{ Account } = require '../models'
module.exports =
  get: (req, res, next) ->
    sluicebox.search req.query.q,
      (query) ->
        sort = sluicebox.sort req.query.sort
        page = req.query.page ? 1
        per = req.query.per ? 100
        Account(req).find(query).sort(sort).paginate page, per, (accounts, pagination) ->
          pagination = sluicebox.paginate 'accounts', req, res, pagination
          res.json { accounts, pagination }
      (error) ->
        res.status(422).json error

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.

Release History

(Nothing yet)