Skip to content
This repository was archived by the owner on Jun 10, 2025. It is now read-only.

wunderflats/goosepage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Super-easy pagination utility for mongoose cursors.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •