Skip to content

Commit fe538aa

Browse files
committed
Added some documentation for release.
1 parent 097a60c commit fe538aa

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# mongoose-cursor-pagination
22
[![Build Status](https://travis-ci.org/enkidevs/mongoose-cursor-pagination.svg?branch=master)](https://travis-ci.org/enkidevs/mongoose-cursor-pagination.svg?branch=master)
3+
[![npm version](https://img.shields.io/npm/v/mongoose-cursor-pagination.svg?style=flat-square)](https://www.npmjs.com/package/mongoose-cursor-pagination)
34
[![Dependency Status](https://david-dm.org/enkidevs/mongoose-cursor-pagination.svg)](https://david-dm.org/enkidevs/mongoose-cursor-pagination)
45
[![devDependency Status](https://david-dm.org/enkidevs/mongoose-cursor-pagination/dev-status.svg)](https://david-dm.org/enkidevs/mongoose-cursor-pagination#info=devDependencies)
56
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/enkidevs/mongoose-cursor-pagination/issues)
@@ -12,3 +13,85 @@
1213
```bash
1314
npm install mongoose-cursor-pagination --save
1415
```
16+
17+
## Usage
18+
19+
The plugin utilises cursor-based pagination via the `startingAfter` and `endingBefore` parameters.
20+
Both take an existing value (see below) and return objects in reverse chronological order.
21+
The `endingBefore` parameter returns objects listed before the named object.
22+
The `startingAfter` parameter returns objects listed after the named object.
23+
If both parameters are provided, only `endingBefore` is used.
24+
Moreover, an optional `limit` parameter can be passed to limit the amount of objects returned.
25+
26+
Add the plugin to a schema:
27+
28+
```javascript
29+
import mongoose from 'mongoose'
30+
import paginationPlugin from 'mongoose-cursor-pagination'
31+
32+
const AccountSchema = new mongoose.Schema({
33+
username: { type: Number, unique: true, index: true }
34+
})
35+
36+
AccountSchema.plugin(paginationPlugin)
37+
38+
mongoose.model('Account', AccountSchema)
39+
```
40+
41+
and then use the model paginate method using a promise:
42+
43+
```javascript
44+
mongoose.model('Account').paginate({}, {
45+
sort: { '_id': 1 },
46+
startingAfter: '59b1f7fd41cfc303859ea1c9',
47+
limit: 20
48+
})
49+
.then(results => { /* ... */ })
50+
.catch(error => { /* ... */ })
51+
```
52+
53+
or using a callback:
54+
55+
```javascript
56+
mongoose.model('Account').paginate({}, {
57+
sort: { '_id': 1 }
58+
}, (error, results) => {
59+
/* ... */
60+
})
61+
```
62+
63+
A possible value for `results` is:
64+
65+
```javascript
66+
{
67+
items: [ /* ... */ ],
68+
hasMore: true
69+
}
70+
```
71+
72+
where `items` is an array containing the elements, and `hasMore` is `true` if there are more elements available after this set. Or `false` otherwise.
73+
74+
The default plugin values can be overwritten, here we show the default values:
75+
76+
```javascript
77+
AccountSchema.plugin(paginationPlugin, {
78+
key: '_id',
79+
limit: 20,
80+
maxLimit: 100,
81+
minLimit: 1
82+
})
83+
```
84+
85+
The `key` specified is assumed to be unique and should have an index associated.
86+
Moreover, when paginating the `key` should be sorted ascending order and the values of `startingAfter` and `endingBefore` should contained values for that `key`.
87+
88+
## Tests
89+
90+
```bash
91+
npm install
92+
npm test
93+
```
94+
95+
## License
96+
97+
[MIT](LICENSE)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mongoose-cursor-pagination",
3-
"version": "0.1.1",
3+
"version": "1.0.0",
44
"description": "Mongoose cursor-based pagination.",
55
"main": "lib/index.js",
66
"files": [

0 commit comments

Comments
 (0)