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

mongoose-pagination-helper

v1.2.2

Published

A utility package to simplify and generalize pagination with Mongoose in Node.js applications.

Downloads

9

Readme

Mongoose Pagination Helper

Description

This package provides an easy way to implement pagination in MongoDB using either the find method or aggregation pipelines. It's designed to work with Mongoose models and offers a simple API to paginate data.

Installation

npm install mongoose-pagination-helper

Usage

The package exports two functions: findPagination and aggregatePagination. Both functions return paginated results and accept similar parameters.

Import CommonJS

const paginate = require('mongoose-pagination-helper');
const MyModel = require('./models/myModel');

findPagination

const query = { }; // Your Mongoose query
const options = {
  limit: 12, // Number of records per page
  page: 1, // Current page
  showPages: 3, // Number of pages to show before and after the current page
  sort: { field: -1}, // The default is { createdAt: -1}
  selectFields: 'field1 field2', // Fields to include in the result
  populateFields: 'field1 field2' // Fields to populate in the result
  // populateFields: [{path: 'field1', select: {_id: 0, field: 1}}] // Another way to use populate in the result
};

const paginatedData = await paginate.findPagination(MyModel, query, options);

Parameters

  • Model (Mongoose Model): The mongoose model for which you want to paginate data.
  • query (Object): Any valid mongoose query object.
  • options (Object): Optional settings for pagination.
    • limit (Number): Number of records to fetch per page.
    • page (Number): The current page, starts from 1.
    • pageRange (Number): Number of pages to show before and after the current one in the paginator.
    • sort (Object): field to be sorted by.
    • selectFields (String): The fields you want to include in the result.
    • populateFields (String or Array): The fields you want to populate in the result.

Returns

An object containing:

  • currentPage: The current page.
  • totalPages: Total number of pages.
  • previousPages: An array of previous page numbers.
  • nextPages: An array of upcoming page numbers.
  • items: The actual data records for the current page.
  • itemsCount: The total of items returned in the current page.

aggregatePagination

const aggregationPipeline = [ ]; // Your Mongoose query
const options = {
  limit: 10, // Number of records per page
  page: 1, // Current page
  pageRange: 3, // Number of pages to show before and after the current page
  sort: { field: -1}, // Sort bt field
};

const paginatedData = await paginate.aggregatePagination(MyModel, aggregatePagination, options);

Parameters

  • Model (Mongoose Model): The mongoose model for which you want to paginate data.
  • aggregatePagination (Array): Any valid aggregation pipeline satges.
  • options (Object): Optional settings for pagination.
    • limit (Number): Number of records to fetch per page.
    • page (Number): The current page, starts from 1.
    • pageRange (Number): Number of pages to show before and after the current one in the paginator.
    • sort (Object): field to be sorted by.

Returns

An object containing:

  • currentPage: The current page.
  • totalPages: Total number of pages.
  • previousPages: An array of previous page numbers.
  • nextPages: An array of upcoming page numbers.
  • items: The actual data records for the current page.
  • itemsCount: The total of items returned in the current page.

License

This package was created with assistance from ChatGPT and is licensed under the MIT license terms.