Mongoose KeySet Pagination Plugin
npm install mongoose-keyset-paginateKey Set pagination mongoose plugin developed for data pagination

Use the package manager npm to install:
``bash`
npm i mongoose-keyset-paginate
javascript
const {Schema, model} = require("mongoose");const UserSchema = new Schema({
email: {
type: Schema.Types.String,
required: true,
unique: true,
},
password: {
type: Schema.Types.String,
required: true,
}
}, { timestamps: true });
const User = model('user', UserSchema)
module.exports = User
`
You two option to use this plugin:
* Applying plugin globally for all models
* Applying plugin only for specific model$3
`javascript
const mongoose = require("mongoose");
const {paginate} = require("mongoose-keyset-paginate");
mongoose.plugin(paginate);
`
$3
`javascript
const {Schema, model} = require("mongoose");
const {paginate} = require("mongoose-keyset-paginate");const UserSchema = new Schema({
email: {
type: Schema.Types.String,
required: true,
unique: true,
},
password: {
type: Schema.Types.String,
required: true,
}
}, { timestamps: true });
UserSchema.plugin(paginate)
const User = model('user', UserSchema)
module.exports = User
`
$3
Getting First Page
#### Code:
`javascript
const results = User.cursorPaginate({}, {});
console.log(results)
`
#### Output:
`json
{
"currentCursorResultsCount": 0,
"results": [],
"hasNext": false,
"next": null,
"hasPrevious": false,
"previous": null,
"limit": 10
}
`
* currentCursorResultsCount: field contains count of docs present in current page
* results: field contains docs in current page
* hasNext: field is "true" if next page is present
* next: field contains next page token if its available otherwise null
* hasPrevious: field is "true" if previous page is present
* previous: field contains previous page token if its available otherwise null$3
Getting Next Page
#### Code:
`javascript
const nextResults = User.cursorPaginate({}, {
next: "token retirved from last results inside next field"
});
console.log(nextResults)
`$3
Getting Previous Page
#### Code:
`javascript
const previousResults = User.cursorPaginate({}, {
previous: "token retirved from last results inside previous field"
});
console.log(previousResults)
`$3
Let's see above example
`
Error: [{"field":"name","error":"name field doesn't exist inside model."}]
at /home/universe/WebstormProjects/test-socket/node_modules/mongoose-keyset-paginate/dist/pagination/functions/paginateByKeySet.js:62:19
at Generator.next ()
at /home/universe/WebstormProjects/test-socket/node_modules/mongoose-keyset-paginate/dist/pagination/functions/paginateByKeySet.js:8:71
at new Promise ()
at __awaiter (/home/universe/WebstormProjects/test-socket/node_modules/mongoose-keyset-paginate/dist/pagination/functions/paginateByKeySet.js:4:12)
at paginateByKeySet (/home/universe/WebstormProjects/test-socket/node_modules/mongoose-keyset-paginate/dist/pagination/functions/paginateByKeySet.js:22:79)
at Function.schema.statics.cursorPaginate (/home/universe/WebstormProjects/test-socket/node_modules/mongoose-keyset-paginate/dist/pagination/functions/paginate.js:10:54)
at Object.findAll (/home/universe/WebstormProjects/test-socket/services/user.service.js:11:15)
at /home/universe/WebstormProjects/test-socket/controllers/auth.controller.js:8:37
at /home/universe/WebstormProjects/test-socket/utils/catchAsync.js:2:19
``Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate (if applicable).