npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@openquantum/mongoose-cursor-pagination

v0.2.8

Published

Easy-to-use, scalable, cursor pagination plugin for mongoose

Downloads

12

Readme

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