1.0.2 • Published 3 years ago
graphql-mongoose-pagination v1.0.2
GraphQL Mongoose Pagination
Why This Plugin
There are many libraries can do the same function. However, still not support filter containing condition $or and multiple cursor
Install
npm install graphql-mongoose-paginationUsage
Please see the following examples support with Graphql and Simple
Sample Usage
Given the following query
Model User
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
sortFieldsonly support the schema typeID,Number,Datetherefore only one of the following 3 fields_id,dateOfBirth,activity
Example 1 : Get list User, don't used cursor
const Pagination = require('graphql-mongoose-pagination')
const paginated = new Pagination(
User,
{
criteria,
sort: { order: "desc"},
pagination: { limit:10 , skip: 100},
select,
},
);Example 2: Get list User, used cursor
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,
},
);Parameters in Pagination
[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
Return value
[getDocs]{Promise} - Array of documents[getCursor]{String} - The cursor to used query next page
Example
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()