simple-mongoose-pagination
v0.0.3
Published
simple mongoose pagination
Downloads
93
Maintainers
Readme
Simple Mongoose Pagination - Pagination Helper for Mongoose
A lightweight utility for managing pagination in Mongoose-based applications. This helper provides functions to calculate pagination details such as limit
, skip
, currentPage
, and totalPages
and totalItemsCount
.
Installation
To install the package, use npm or yarn:
$ npm install simple-mongoose-pagination
or
$ yarn add simple-mongoose-pagination
Usage
import { getPaginationQueryDetails } from 'simple-mongoose-pagination';
Example Usage
import { getPaginationQueryDetails } from 'simple-mongoose-pagination';
// Define your pagination query parameters
const paginationQuery = {
pageNumber: 4, // Page number (1-based index, 0 will default to 1)
itemsPerPage: 2 // Number of items per page (0 will default to 1)
};
// Total number of items in your dataset
const totalItemsCount = 100;
// Get pagination details
const paginationDetails = getPaginationQueryDetails(paginationQuery, totalItemsCount);
console.log(paginationDetails);
Functions
getPaginationQueryDetails(paginationQuery: PaginationQueryInterface, totalItemsCount: number)
Calculates pagination details based on the provided query parameters and total items count.
Parameters
paginationQuery
(object): Contains pageNumber and itemsPerPage.totalItemsCount
(number): Total number of items available.
Returns:
currentPage
(number): The current page number.limit
(number): The number of items per page.skip
(number): The number of items to skip for the current page.totalPages
(number): The total number of pages.
{
currentPage: 1,
limit: 1,
skip: 0,
totalPages: 1
}
API Details
currentPage
: The current page number (1-based index). Defaults to1
ifpageNumber
is less than or equal to0
.limit
: The number of items per page. Defaults to1
ifitemsPerPage
is less than or equal to0
.skip
: The number of items to skip.totalPages
: The total number of pages. Calculated based on the total number of items and items per page. Defaults to1
ifitemsPerPage
is less than or equal to0
.
Technologies Used
- JavaScript - The core language used for implementing the pagination logic.
- TypeScript Type annotations are used to ensure type safety and enhance the development experience by providing clear definitions for the parameters and return types.
- Jest Jest is a JavaScript testing framework maintained by Facebook, Inc. designed and built by Christoph Nakazawa with a focus on simplicity and support for large web applications.
Tests
Run the command below
$ npm run watch
or
$ npm run test