mongoose-pagination-middleware
v1.0.7
Published
A simple pagination middleware for Express and Mongoose
Maintainers
Readme
📄 mongoose-pagination-middleware README.md
# 📦 mongoose-pagination-middleware
A simple, TypeScript-friendly Express middleware to handle pagination parameters for Mongoose queries.
Cleanly parses pagination and sorting options from URL queary parameters and attaches them to the request object for easy use.
---
## 🚀 Features
✅ Zero-dependency middleware
✅ TypeScript type definitions included
✅ Customizable via query parameters
✅ Works seamlessly with Mongoose `.find()` queries
✅ Easy integration with existing Express apps
---
## 📦 Installation
```bash
npm install mongoose-pagination-middleware
```📖 Usage
📌 Register the middleware in your Express app
import express from "express";
import paginationMiddleware from "mongoose-pagination-middleware";
const app = express();
app.use(paginationMiddleware);📌 Example Mongoose route with pagination
import { Request, Response } from "express";
import Item from "./models/Item"; // Replace with your actual model
app.get("/items", async (req: Request, res: Response) => {
const { page, limit, sort } = req.pagination!;
const items = await Item.find()
.sort(sort)
.skip((page - 1) * limit)
.limit(limit);
res.json({ items, pagination: { page, limit } });
});🔍 Query Parameters Supported
| Parameter | Type | Description | Default |
| ----------- | ------ | --------------------------------------------------------- | ----------- |
| page | number | Current page number | 1 |
| limit | number | Number of items per page | 10 |
| sortBy | string | Field name to sort by | createdAt |
| sortOrder | string | Sorting order: asc for ascending, desc for descending | desc |
📊 Example API Call
GET /items?page=2&limit=5&sortBy=name&sortOrder=asc📃 License
MIT License © 2025 Ankit Kumar
📞 Contact
For issues, questions, or suggestions, feel free to open an issue on the GitHub repository or reach out via email at [email protected]
