Giter Site home page Giter Site logo

mongoose-cursor-pagination's Introduction

mongoose-cursor-pagination

Build Status

This small plugin makes it easy to use cursor-based pagination in your app, without changing the way you use queries in mongoose.

Installation

npm install @mother/mongoose-cursor-pagination --save

Node.js 10.x or higher is required.

Usage

In your schema:

import mongoose from 'mongoose'
import paginationPlugin from '@mother/mongoose-cursor-pagination'

const CommentSchema = new mongoose.Schema({
  date: { type: Date },
  body: { type: String },
  author: {
     firstName: { type: String },
     lastName: { type: String }
  }
})

CommentSchema.plugin(paginationPlugin)

In your application code:

const { results, pageInfo } = await Comment
   .find({})                 // Whatever filter you want
   .limit(30)                // Use limit and other Query options as you normally would
   .sort('author.lastName')  // Use sort as you would normally do
   .paginate(startCursor)    // startCursor optional
   .exec()                   // Required

results will be the results that you would expect from a normal mongoose find query

pageInfo will have two properties: hasNext and nextCursor

Be sure to index correctly. Note that this plugin always add _id to the sort key to ensure cursors are unique, so include that in your index as well. For example, say you are using the date field for pagination, then you would want to setup the index:

commentSchema.index({ date: -1, _id: -1 })

Plugin Options

You can optionally pass an options object to this plugin:

CommentSchema.plugin(paginationPlugin, {
   // If no limit is specified, and paginate() is being used,
   // what should the default limit be.
   defaultLimit: 100
})

Tests

Numerous tests are included in the tests directory, and can be run using the command npm test.

To Do

  • Test against older versions of mongoose
  • Support for search with pagination
  • Support for aggregation with pagination
  • Support for hasPrev and prevCursor
  • Support exec calls that use callbacks instead of promises

License

MIT

mongoose-cursor-pagination's People

Contributors

asabhaney avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mongoose-cursor-pagination's Issues

nextToken/prevToken for future records

Hey, I was looking into this library and was wondering if the following could be supported:

nextToken should be a valid token even if there is no next page. Assume a webpage got a list of messages, but then via websocket it is notified of new data to fetch.

It should be possible to use the cursor to get results that were added in the mean time.

It should be possible to have both a nextToken and a prevToken.

So, as an example, a chat app would work like this:

  1. get 50 latest chat messages (sorted by date: -1)
  2. there should be a way to get "future" messages, once an event comes via websocket
  3. should be also possible to get previous messages, when the user scrolls back and wants to see the chat room history.

In this case, if sorted by date: -1, the nextToken would actually be for old messages, and prevToken would be for future messages.

nextCursor when there are no next pages

Hey, I was looking into this library and was wondering if this was supported, or if it's implemented already.

Assume a collection of chat messages. I'd like to get the last page, but have both a prevCursor and nextCursor available, even is there are no further results at the time of the query.

The prevCursor would work to get previous chat message pages, such as when the user scrolls up to browse through the history.

Assume the client is notified of new messages. It should be able to query with the nextCursor token and get new messages.

It might also work by sorting descending, but a cursor token to get fresh messages would still be needed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.