There are many libraries can do the same function. However, still not support filter containing condition $or
and multiple cursor
npm install graphql-mongoose-pagination
Please see the following examples support with Graphql
and Simple
Given the following query
const mongoose = require('mongoose');
const { Schema } = mongoose;
const UserSchema = new Schema({
email: String,
firstName: String,
lastName: String,
photo: String,
dateOfBirth: { type: Date, index: -1 },
activity: { type: Number, index: -1 },
}, {
timestamps: true,
});
module.exports = mongoose.model('user', UserSchema);
Because
sortFields
only support the schema typeID,Number,Date
therefore only one of the following 3 fields_id
,dateOfBirth
,activity
const Pagination = require('graphql-mongoose-pagination')
const paginated = new Pagination(
User,
{
criteria,
sort: { order: "desc"},
pagination: { limit:10 , skip: 100},
select,
},
);
const Pagination = require('graphql-mongoose-pagination')
const {cursor } = input
const paginated = new Pagination(
User,
{
criteria,
sort: { field: "dateOfBirth", order: "desc"},
pagination: { limit:10, cursor },
select,
},
);
[criteria]
{Object} - The filter ofmodel
[pagination]
{Object}[limit]
{ Number}: Limit that was used[cursor]
{ String}: The cursor used compare with record[skip]
{ Number}: The number skip record
[sort]
{Object}:[order]
[Object] : Sort order only supportasc|desc
. Documentation[fields]
[String] : Sort field with schema typeID,Number,Date
[select]
{String || Array} : Fields to return (by default returns all fields). Documentation
[getDocs]
{Promise} - Array of documents[getCursor]
{String} - The cursor to used query next page
const Pagination = require('graphql-mongoose-pagination')
const {cursor } = input
const paginated = new Pagination(
User,
{
criteria,
sort: { field: "_id", order: "asc"},
pagination: { limit:10, cursor },
select,
},
);
// get list data
await paginated.getDocs();
// Get cursor
paginated.getCursor()