Socket
Socket
Sign inDemoInstall

cagination

Package Overview
Dependencies
18
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cagination

A Mongoose helper to simplify the pagination process.


Version published
Weekly downloads
0
Maintainers
1
Created
Weekly downloads
 

Readme

Source

NPM version NPM downloads MIT License Build Status

Cagination

A Mongoose helper to simplify the pagination process.

If you are looking for a nice front-end component to interface with pagination, check out our AngularJS project Cagination Angular. These two projects integrate nicely for seamless, full stack pagination.

The Old, Painful Way

var currentPage = data.currentPage;
var perPage = data.perPage;
var options = {
    actorId: nicCage._id
};

Film.find(options).select('name actorId').populate({
    path: 'actorId',
    select: 'lastName'
}).sort({
    name: -1
}).skip((currentPage - 1) * perPage).limit(perPage).exec(function(err, films) {
    Film.count(options).exec(function(err, count) {
        var totalPages = Math.ceil(count / perPage);
        // return films and totalPages
    });
});

NOT THE PAGES! NOT THE PAGES! AGWHGHAHGHAA THEY'RE CLOGGING MY CODE!

Of course this is quite a mess, and painful to rewrite for each query you need paginated. Although the .count() procedure is pretty quick, to make this really efficient, you would need to also utilize async to perform that operation in parallel with .find(). But then you have a real disaster on your hands for such a simple task.

The Cage Rage Way

Caginate aims to be robust but reproducible. Like your standard find() query, Caginate can accept the same options for select, populate, and sort. However, Caginate handles all of the bothersome math and manual skipping/limiting needed to get a paginated return. Additionally, it asynchronously grabs the document count in order to provide you with the total number of pages.

var currentPage = data.currentPage;
var perPage = data.perPage;
var options = {
    firstName: 'Nicolas'
};

caginate(Film, {
    options: options,
    select: 'name actorId',
    populate: {
        path: 'actorId',
        select: 'lastName'
    },
    sort: {
        name: -1
    },
    maxTime: 500,
    perPage: perPage,
    currentPage: currentPage
}).exec(function(err, films, count, totalPages) {
    // return films and totalPages
});

Setup and Options

Get the source from GitHub or install via NPM

npm install cagination --save

Make sure to add:

var caginate = require('cagination').find;

and you should be set. At any time you want paginated results, replace your Mongoose Model.find(query, callback) with caginate(Model, query, callback).

The Caginate query is an object consisting of:

  • options (optional)
  • meta (optional) - this field allows us to support an additional parameter on the Mongoose find, such as {score : {$meta : 'textScore'}} for text searches
  • select (optional)
  • populate (optional)
  • sort (optional)
  • lean (boolean, optional - defaults to false)
  • maxTime (optional) - sets maxTimeMS for cursor
  • perPage (optional - defaults to 25 - how many documents to get back per page)
  • currentPage (required - the page to retrieve documents for)

Callback

The Caginate callback will look something like:

caginate(Model, {
    perPage: 10,
    currentPage: 1
}, function(err, documents, count, totalPages) {
    if(err) {
        // handle the Mongoose error
    }
    // documents - documents matching query but limited by perPage
    // count - the total number of documents matching your query
    // totalPages - pages needed to fit all of the documents at 10 per page
});

Version History

  • 0.1.10: Add maxTime option
  • 0.1.9: currentPage parse fix.
  • 0.1.8: Syntax bugfix.
  • 0.1.7: Dependency updates. Parse integers for limits and skips.
  • 0.1.6: Dependency updates
  • 0.1.5: Supports additional options on find, in a new field called meta
  • 0.1.4: Remove mongoose dependency. Update modules.
  • 0.1.3: Module updates and tests
  • 0.1.2:: Added lean option
  • 0.1.1: Documentation
  • 0.1.0: Initial release

Keywords

FAQs

Last updated on 17 Jul 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc