This small plugin makes it easy to use cursor-based pagination in your app, without changing the way you use queries in mongoose.
npm install @mother/mongoose-cursor-pagination --save
Node.js 8.x or higher is required.
In your schema:
import mongoose from 'mongoose'
import paginationPlugin from '@mother/mongoose-cursor-pagination'
const CommentSchema = new mongoose.Schema({
date: { type: Date },
body: { type: String },
author: {
firstName: { type: String },
lastName: { type: String }
}
})
CommentSchema.plugin(paginationPlugin, { defaultLimit: 50 })
In your application code:
const { results, pageInfo } = await Comment
.find({}) // Whatever filter you want
.limit(30) // Use limit and other Query options as you normally would
.sort('author.lastName') // Use sort as you would normally do
.paginate(startCursor) // startCursor optional
.exec() // Required
results
will be the results that you would expect from a normal mongoose find
query
pageInfo
will have two properties: hasNext
and nextCursor
- Support for search with pagination
- Support for aggregation with pagination
- Support for
hasPrev
andprevCursor
- Support
exec
calls that use callbacks instead of promises - Ensure
lean
query modifier works - More tests