/graphql-mongoose-pagination

Primary LanguageJavaScriptApache License 2.0Apache-2.0

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-pagination

Usage

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 sortFields only support the schema type ID,Number,Date therefore 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 of model
  • [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 support asc|desc. Documentation
    • [fields][String] : Sort field with schema type ID,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()