mongoose-cursor-pagination
Mongoose cursor-based pagination
Installation
npm install mongoose-cursor-pagination --save
Usage
The plugin utilises cursor-based pagination via the startingAfter
and endingBefore
parameters.
Both take an existing value (see below) and return objects in reverse chronological order.
The endingBefore
parameter returns objects listed before the named object.
The startingAfter
parameter returns objects listed after the named object.
If both parameters are provided, only endingBefore
is used.
Moreover, an optional limit
parameter can be passed to limit the amount of objects returned.
Add the plugin to a schema:
const AccountSchema = username: type: Number unique: true index: true AccountSchema mongoose
and then use the model paginate method using a promise:
mongoose
or using a callback:
mongoose
A possible value for results
is:
items: /* ... */ hasMore: true
where items
is an array containing the elements, and hasMore
is true
if there are more elements available after this set. Or false
otherwise.
The default plugin values can be overwritten, here we show the default values:
AccountSchema
The key
specified is assumed to be unique and should have an index associated.
Moreover, when paginating the key
should be sorted ascending order and the values of startingAfter
and endingBefore
should contained values for that key
.
Tests
npm installnpm test