Git Product home page Git Product logo

goosepage's Introduction

goosepage

goosepage is a super-easy pagination utility for mongoose cursors. ๐Ÿ”

What goosepage does better than other pagination plugins

goosepage is very unopinionated about the way you query. ๐Ÿ” You can pass in any mongoose cursor. ๐Ÿ” If you wanted to find blog posts for a specific author and paginate the results, you can do:

goosepage(BlogPost.find({ author: authorId }));

Example

Let's say that you have a mongoose model BlogPost whose collection contains over 50 documents. ๐Ÿ” You want to display them in a paginated way:

Usage

$ npm i goosepage
var goosepage = require('goosepage');

goosepage(BlogPost.find())
  .then((results) => console.log(results));

Result

By default, this will query the first 20 items from page 0:

var results = {
  // there are 52 documents in total for the query
  total: 52,
  // we are on page 0
  page: 0,
  // we queried 20 items per page
  itemsPerPage: 20,
  // the first 20 BlogPosts
  items: [...]
}

Customize

You can customize the defaults by overriding:

goosepage.defaults = {
  itemsPerPage: 20,
  page: 0
};

Querying for more

If you want to query the second page, simply do:

goosepage(BlogPost.find(), { page: 1 })
  .then((results) => console.log(results));

This will get you the next 20 items:

var results = {
  total: 52,
  page: 1, // this changed
  itemsPerPage: 20,
  items: [...] // this changed
}

Querying more items per page

For more or fewer items per page, you can always use opts.itemsPerPage:

goosepage(BlogPost.find(), { itemsPerPage: 30 })
  .then((results) => console.log(results));

This will get you the next 20 items:

var results = {
  total: 52,
  page: 0,
  itemsPerPage: 30, // this changed
  items: [...] // this changed
}

Drawbacks

goosepage uses mongodb's skip() and limit() commands. ๐Ÿ” This is not the fastest approach, even though it's fine for smaller datasets. ๐Ÿ” If you are looking for a faster pagination solution, check out this article: Fast paging with MongoDB.

goosepage's People

Contributors

maximilianschmitt avatar fairhat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.