|
1 | 1 | # mongoose-cursor-pagination
|
2 | 2 | [](https://travis-ci.org/enkidevs/mongoose-cursor-pagination.svg?branch=master)
|
| 3 | +[](https://www.npmjs.com/package/mongoose-cursor-pagination) |
3 | 4 | [](https://david-dm.org/enkidevs/mongoose-cursor-pagination)
|
4 | 5 | [](https://david-dm.org/enkidevs/mongoose-cursor-pagination#info=devDependencies)
|
5 | 6 | [](https://github.com/enkidevs/mongoose-cursor-pagination/issues)
|
|
12 | 13 | ```bash
|
13 | 14 | npm install mongoose-cursor-pagination --save
|
14 | 15 | ```
|
| 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) |
0 commit comments