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

@mutabazia/mongoose-pagination-express

v1.0.5

Published

A pagination library for use with Express.js and Mongoose, written in TypeScript.

Downloads

65

Readme

@mutabazia/mongoose-pagination-express

A pagination library for use with Express.js and Mongoose, written in TypeScript.

Installation

npm install @mutabazia/mongoose-pagination-express

Usage

Step 1 - Import the pagination middleware and use it in your Express.js app:

import { Aggregate, Query } from "mongoose";
import { pagination } from "@mutabazia/mongoose-pagination-express";

const app = express();

// ...
app.use(pagination(Query, Aggregate));
// ...

Step 2 - Use the paginate() method on your Mongoose Query or Aggregate objects:

import { Request, Response } from 'express';
import { UserModel } from './models/user.model';

export const listUsers = async (req: Request, res: Response) => {
  // ...

  // 1. With Query
  const query = UserModel.find(...);
  query.where(...);
  const data = await query.paginate();

  // Or
  const data = await UserModel.find(...).where(...).paginate()

  // 2. With Aggregate
  const aggregate = UserModel.aggregate(...);
  aggregate.match(...);
  const data = await aggregate.paginate();

  // Or
  const data = await UserModel.aggregate(...).match(...).paginate();

  // ...
};

The paginate() method returns a Promise that resolves to a Pagination object containing the paginated data and metadata.

Step 3 - You can customize the pagination behavior by passing query parameters in the request URL:

GET /api/items?page=2&per_page=10&pagination=off

The following query parameters are supported:

  • page: The page number to retrieve. Defaults to 1.
  • per_page: The number of items to retrieve per page. Defaults to 15.
  • pagination: The parameter to turn off pagination. You can set the value to off, false, or 0 to turn off pagination. By default, pagination is turned on.

Note: pagination=off query parameter is a way to turn off pagination and retrieve all the results in a single query.

This can be useful when you know that you don't want to paginate the results on an endpoint that has pagination implemeted, or if you are retrieving a small number of results and want to avoid the overhead of pagination.

When pagination=off is used, the function will return a Promise that resolves to an array of results instead of a Pagination object. This array will contain all the results without any pagination information.

It's worth noting that when pagination is turned off, the per_page and page parameters will be ignored.

API

pagination

The pagination middleware is a function that adds the paginate() method to the Query and Aggregate prototypes.

paginate<Doc>(per_page?: number): Promise<Pagination<Doc>>

The paginate() function takes a Query or Aggregate object, and a per_page number, and returns a Promise that resolves to a Pagination object.

Pagination<Doc>

The Pagination interface represents the result of a paginated query. It has the following properties:

  • from: The index of the first item in the current page.
  • to: The index of the last item in the current page.
  • per_page: The number of items per page.
  • total: The total number of items in the query.
  • current_page: The current page number.
  • prev_page: The previous page number, or null if the current page is the first page.
  • next_page: The next page number, or null if the current page is the last page.
  • last_page: The last page number, or null if there is only one page.
  • data: An array of items in the current page.

Disclaimer

This package was inspired by the typeorm-pagination package, and some parts of the code were adapted from it. We thank the typeorm-pagination team for their work and contribution to the community.

License

This library is licensed under the MIT License