0.2.0 • Published 7 years ago
mongul v0.2.0
mongul
Paginates a MongoDB collection and add links to the next/prev page.
Install
npm install mongul --save
Examples
const express = require('express');
const app = express();
const paginate = require('mongul');
// 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
function index(req, res, next) {
req.cursor = collection.find({ owner: req.user })
next();
}
app.get('/items', index, paginate(), (req, res) => {
res.status(200).json(req.page)
});
// {
// "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