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

mongoose-opt-paginate

v1.0.0

Published

Optimized pagination using indexes (no cursor.skip) with fallback. Used with Mongoose + Express.

Downloads

7

Readme

Mongoose Optimized Paginate (MOP)

Optimized pagination using indexes (no cursor.skip) with fallback. Used with Mongoose + Express, or Mongoose + Koa.

Getting Started

$ npm install mongoose-opt-paginate

The MOP plugin returns an object containing a property for accessing the api (paginate function) and a property for accessing the mongoose plugin.

The first step is to include the plugin in your mongoose instance e.g.

var mongoose = require('mongoose'),
	mongoosePlugin = require('mongoose-opt-paginate').plugin;
mongoose.plugin(mongoosePlugin);

Now you can start paginating!

Express Examples

var mongoose = require('mongoose'),
	// returns a function(model, [config])
	paginate = require('mongoose-opt-paginate').api;

// assuming a course model exists
var model = mongoose.model('Course');

// returns a function(req, res, next, search, options, cb)
paginate = paginate(model);

app.get('/courses', function(req, res, next) {
	// no callback will assume end of route (res.json called with results)
	paginate(req, res, next, {}, {});
	// with callback
	paginate(req, res, next, {}, {}, function(results) {
		/* results = {
			"page": 1,
			"hasMore": true,
			"links": {
				"first": "/courses?page=1&currentPage=1&pageSize=10",
				"next": "/courses?page=2&currentPage=1&pageSize=10&after=<encoded>",
				"last": "/courses?page=2&currentPage=1&pageSize=10&last=true"
			},
			"pageCount": 10,
			"total": 14,
			"before": "<encoded>",
			"after": "<encoded>",
			"data": [{<courses>}]
		} */

		// do something with results

		res.json(results);
	});
});

app.get('/other-courses', function(req, res, next) {
	var options = {};
	model.paginate(
		req.query.search,
		req.query.currentPage,
		req.query.page,
		req.query.pageSize,
		options
	)
	.then(function(pagingData) {
		/* pagingData = {
			newCurrentPage: 1,
			"before": "<encoded>",
			"after": "<encoded>",
			"pageCount": 10,
			"numPages": 10,
			"total": 100,
			"items": [{<courses}]
		}; */

		res.json(pagingData);
	});
});

Koa Examples

var mongoose = require('mongoose'),
	// returns a function(model, [config])
	paginate = require('mongoose-opt-paginate').koa;

// assuming a course model exists
var model = mongoose.model('Course');

// returns a function(ctx, search, options, cb)
paginate = paginate(model);

router.get('/courses', function* () {
	var ctx = this;
	// no callback will assume end of route (ctx.body called with results)
	paginate(ctx, {}, {});
	// with callback
	paginate(ctx, {}, {}, function(results) {
		/* results = {
			"page": 1,
			"hasMore": true,
			"links": {
				"first": "/courses?page=1&currentPage=1&pageSize=10",
				"next": "/courses?page=2&currentPage=1&pageSize=10&after=<encoded>",
				"last": "/courses?page=2&currentPage=1&pageSize=10&last=true"
			},
			"pageCount": 10,
			"total": 14,
			"before": "<encoded>",
			"after": "<encoded>",
			"data": [{<courses>}]
		} */

		// do something with results

		ctx.body = results;
	});
});

app.get('/other-courses', function* () {
	var ctx = this;
	var options = {};
	model.paginate(
		req.query.search,
		req.query.currentPage,
		req.query.page,
		req.query.pageSize,
		options
	)
	.then(function(pagingData) {
		/* pagingData = {
			newCurrentPage: 1,
			"before": "<encoded>",
			"after": "<encoded>",
			"pageCount": 10,
			"numPages": 10,
			"total": 100,
			"items": [{<courses}]
		}; */

		ctx.body = pagingData;
	});
});

Things to Note

before and after are encoded strings containing the necessary item information of the first and last item of the returned results for optimized pagination. If sorting, make sure a compound index (collection-level) exists for the sort field(s) and _id field in proper order

e.g. So if you're sorting by name, date, and _id (_id is always there by default and follows the same order as the first/primary sort key: name, in this case) and you want optimized pagination, the compound indexes {name: 1, date: 1, _id: 1} (for optimization when primary sort key in ascending) and {name: -1, date: 1, _id: -1} (for optimization when primary sort key in descending) should exist on the collection-level (not schema-level). Opposite compound indexes don't need to be created i.e. {name: -1, date: -1, _id: -1} and {name: 1, date: -1, _id: 1} wouldn't need to be added if the above indexes already exist.

If sorting (with more than by _id since it's always included) and a matching compound index is not found, pagination will fall back to a non-optimized state. If it's just by _id, no worries, there's a default index for _id that always exists.

Contributing

Looking to contribute? Awesome! Make sure you've got EditorConfig installed to maintain the existing code style. Send your PRs to the dev branch. Before opening a PR, test and lint your code using npm run ci.

Todo's

  • Write unit and integration tests
  • Documentation

Release History

1.0.0

  • Added support for koa pagination
  • The mongoose plugin now uses promises rather than callbacks

0.3.0

  • Added some unit tests to mongoosePaginateSpec.js.
  • Can now provide both options.before and options.after. The library handles the logic of which to use.

0.2.0

  • expressPaginate.js tests completed. Started writing mongoosePaginate.js tests.
  • FIX: sortBy params including non-alphabetical characters being replaced with empty space during splitting of param string.

0.1.0

  • Testing stack has been finalized. Linting has been included.
0.0.11
  • Returning results as opposed to ending route by calling res.json

License

MIT