uncledent / mongoose-cursor-pagination-plugin

Cursor pagination plugin for mongoose
8 stars 1 forks source link

Usage of paginate #1

Closed arvi closed 5 years ago

arvi commented 6 years ago

Hello @uncledent ,

I am trying to follow the instructions on the README, however, I am stuck on figuring out how to implement the paginate on the exported model:

// book.schema.js
import mongoose from 'mongoose';
import MongoPaging from 'mongoose-cursor-pagination-plugin';
const BookSchema = new mongoose.Schema(
  {
    title: String,
    categoryId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Category',
    },
    author: {
      id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Author',
      },
      name: String,
    },
    bookLended: Date,
  },
  { timestamps: true },
);
BookSchema.plugin(MongoPaging.mongoosePlugin);
export default mongoose.model('Book', BookSchema);
// book.helper.js
import Book from 'models/book.schema';

export const getBooks = async (categoryId, value) => {
  const filterAuthor =    {
    $or: [{ 'author.name': { $in: value } }],
  };     

  const books = await Book.find({
    $and: [
      categoryId: mongoose.Types.ObjectId(categoryId),
      filterAuthor,
    ],
  }).sort({ bookLended: 1 }); 

  // where should I insert the `.paginate` in the documentation?
  // default function is "paginate"
  counter.paginate({ limit: 10 }).then(result => {
    console.log(result);
  });
};

Thanks for your advice.

uncledent commented 6 years ago

What exactly does not work? You can use readme of https://github.com/mixmaxhq/mongo-cursor-pagination as it does exactly the same.

chrischen commented 5 years ago

You would use Book.paginate() instead of Book.find(), but I can't seem to get paginate() to work as it's hanging.

sangdth commented 4 years ago

My typescript always yells ".paginate() does not exist", I don't know why. In both mongo-cursor-pagination and here.