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

@lanrey/sails-pager

v2.1.1

Published

Pagination Service for SailsJS projects

Downloads

3

Readme

sails-pager

GitHub version npm version

Pagination Service for SailsJS 0.12.x projects

This module makes it easy to integrate pagination into your SailsJS applications.

Installation

  • Make sure that Node.js installed.
  • Install Node.js library with npm:
npm install sails-pager

Usage

paginate(options)

This function helps you paginate any model passed to the paginate function.

  • Create YourController.js file. Insert into following code.
var pager = require('sails-pager');

module.exports = {
    list: function(req, res) {
        var perPage = req.query.per_page;
        var currentPage = req.query.page;
        var conditions = {active: true};

        //Using Promises
        pager.paginate(SailsModelHere, conditions, currentPage, perPage, [{name: 'AssociatedModel', query: {isDeleted: false}}], 'createdAt DESC').then(function(records){
            console.log(records);
        }).catch(function(err) {
            console.log(err);
        });

        //Using a Callback
        pager.paginate(SailsModelHere, conditions, currentPage, perPage, [{name: 'AssociatedModel', query: {isDeleted: false}}], 'createdAt DESC', function(err, records){
            if(err){
                console.log(err);
            }
            console.log(records);
        });
  },
}

The pager.paginate() function takes the following options:

  1. model (required): Pass the sails model you want to query.
  2. conditions (required | object, pass {} if you have no conditions): This are the conditions for the query pass to the model in 1 above.
  3. currentPage (required | integer): This is the current page value for the dataset to return.
  4. perPage (required | integer): The number of results to return per page, you can pass false to ignore.
  5. populateData (required | array/collection): This is the associated sails model to populate. Multiple models can be populated eg: If main model=User then you can use ['pets', 'images'] or ['pets'] to populate only the User's associated pets. You can also pass populate queries, but you'll have to use a slightly different syntax by pass an object with the name & query properties. eg model=User, ['pets', {name: 'images', query: {isDeleted: false}}], this will populate the User's pets & all images that have not been deleted. You can pass false to ignore.
  6. sort (required | string): Pass the same sailsjs sort syntax. You can pass false to ignore.

Options order: pager.paginate(model, conditions, currentPage, perPage, populateData, sort);

Please note that the pagination module will return your records in a specific format.

paginatePupulate(options)

This function helps you paginate any populated data stripping the main model data eg: If a User has many pets and you would like to retrieve a paginated set of the users pets, You'll pass the User as the main model and pass the pets as the populateData, please note this does not support queries yet like the main paginate() function.

The pager.paginatePopulate() function takes the same options as the paginate() with the exception of the populateData: You can eaither pass a string to the pager.paginatePopulate() or an object not an array cos you can only paginate one set of pupolated data at once.

  1. populateData (required | string)
  2. populateData (required | object) e.g
//example object of 'populateData'
{name: 'AssociatedModel', query: {isDeleted: false}, select:['attribute1','attributed2', '...']}

Returned Records

Example returned records object;

{
    "message": "Data retrieved successfully",
    "data": [{data goes here}],
    "meta": {
        "page": 1,
        "perPage": 20,
        "previousPage": false,
        "nextPage": 2,
        "pageCount": 4,
        "total": 79
    }
}

Nice to have

  • populate inside a populate (Nested Populate).

Submit an issue, feedback or a feature request

  • Any issue topics are welcome.

CONTRIBUTING

  • Fork it!
  • Clone your fork
  • Create your feature branch: git checkout -b my-new-feature;
  • Commit your changes: git commit -am 'Add some feature/fix';
  • Push to the branch: git push origin my-new-feature;
  • Submit a pull request: