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

sails-count-middleware

v1.0.6

Published

While paginating, find the total count by criteria, via an added `X-Pagination` header to all blueprints `find` requests

Downloads

339

Readme

sails-count-middleware

While paginating, find the total count by criteria, via an added X-Total-Count header to all blueprints find requests. Or even an X-Pagination-JSON header, or both.

screen shot 2017-12-21 at 7 15 37 pm

Sails version support

It only supports Sails 1.x+

Basic Usage

npm install sails-count-middleware --save

Then in your config/http.js

middleware: {
// ....

    // require it with whatever name you want
    addCount: require('sails-count-middleware'),

    // then add it to the order[] array
    order: [
      // ...
      'addCount',
      // ...
     ]

Advanced Usage

You can create a policy, say we called it api/policies/addCount.js

 module.exports = require('sails-count-middleware').generate({
    // enable both for demo
    totalCountHeader: true
    paginationJsonHeader: true
 });

Then in config/policies.js you can specify which find call will get augmented with the count header.

UserController: {
    'find': ['isLoggedIn', 'addCount'],
}

Extra Options

There are options that you can change, just call the generate() function

    require('sails-count-middleware').generate({
        // if you want to add/remove an action i.e. 'user/search' or whatever
        // the array can contain a string for exact match or a regular expression for a pattern
        // if you use this options, the array you use will override the default, it will NOT concat
        // this is the default,
        // it will match all the usual :model/find, :model/:id/:association/populate
        actions: ['find', 'populate', /^.*\/(find|populate)$/]

        // you can rename the headers by passing a string
        // or disable them by setting them to false

        // default option value is true
        // default header name is 'X-Total-Count'
        totalCountHeader:  'X-Count', // this will enable and rename the header
        totalCountHeader: false, // this disable this header
        totalCountHeader: true, // this enable it with the default name

        // default option value is false
        // default header name is 'X-Pagination-JSON'
        paginationJsonHeader: 'X-PJSON', // this will enable and rename the header
        paginationJsonHeader: false, // this disable this header
        paginationJsonHeader: true, // this enable it with the default name

        // if the .count() calls fails, to throw an error or not
        silentError: false // default
    }),

Result

Then make a GET call to a find blueprint action, with any criteria, even with limit

Request

curl -sSL -D - localhost:1337/user?name=jane&limit=1

Response

HTTP/1.1 200 OK
X-Powered-By: Sails <sailsjs.com>
Content-Type: application/json; charset=utf-8
X-Total-Count: 4                          <---------
X-Pagination-JSON: {"count":4,"limit":1}  <---------
Content-Length: 614
ETag: W/"266-K6bjdxLpUMTlwLOyDQFTFJYi/pQ"
Date: Thu, 21 Dec 2017 17:18:22 GMT
Connection: keep-alive

[
  {..}  // Only 1 result though, since limit=1
]

Client side node

You can use this header however you like, but I recommend the following if you do not want to change the response.body that comes back from the sails server.

Just augment that array with a non-enumerable property, say we call it, __pagination__, this way when you iterate over or enumerate that array, that property won't be part of that iteration (in most cases), but you can still access it response.body.__pagination__.count

    // say you're using jQuery

    // .... somewhere in your app, call this once
    // this will intercept all the responses from the server
    // then augment the data response with a __pagination__, even if data is an array.
    $.ajaxSetup({
        success: (data, status, $xhr) => {
            let json = $xhr.getResponseHeader('x-pagination-json');
            if (json) {
                Object.defineProperty(data, '__pagination__', {
                    enumerable: false,
                    value: JSON.parse(json)
                });
            }
        }
    });

    // so when you make a GET call to find a list of things
    $.get('/api/users')
        .then((users) => {
            let {count, limit, skip, sort} = users.__pagination__;
            console.log(`users returned: ${users.length}, total users: ${count}`);
            users.forEach((user, i) => {
                console.log(`[index:${i}] user: ${user.name}`);
            });
        });