mongo-paginate
v1.1.0
Published
Express middleware to paginate mongo collections
Downloads
4
Maintainers
Readme
pagina
Paginates a MongoDB collection and add links to the next/prev page.
Install
npm install mongo-paginate --save
Examples
const express = require('express');
const app = express();
const paginate = require('mongo-paginate');
// without pagination
// GET http://localhost/items
app.get('/items', (req, res, next) => {
collection
.find({ owner: req.user }).toArray()
.then(items => res.status(200).json(items))
});
// [{a:1, b:1}, {a:2, b:2}, {a:3, b:3}, {a:4, b:4}]
// with pagination
// GET http://localhost/items?start=0&limit=3
paginate.extend(app);
app.get('/items', (req, res, next) => {
const cursor = collection.find({ owner: req.user })
res.status(200).paginate(cursor, { limit: 100 })
});
// will return 400 error if query limit > 100
// {
// "items": [{a:1, b:1}, {a:2, b:2}, {a:3, b:3}],
// "prev": null,
// "next": "http://localhost/items?start=3&limit=3"
// }
License
MIT
TODO
http://tools.ietf.org/html/rfc5988#page-6