nestjs-pageable-fn
v1.0.2
Published
NestJS pagination helper with sorting and search support.
Maintainers
Readme
nestjs-pageable-fn
A reusable pagination helper for NestJS (or any Node.js backend), providing support for:
- ✅ Pagination (
pageNumber,pageSize) - 🔍 Search (
searchTerm,searchBy) - 📊 Sorting (
sortBy,sorting) - ✅ Typed and extensible
- 📦 Ready for integration with databases like MongoDB, PostgreSQL, etc.
📦 Installation
npm install nestjs-pageable-fn🚀 Usage
1. Extend Pageable
if you extend Pageable<T> then you need to overide fetchData() according to your database schema as you needed.
import { Pageable, PaginationParams, PaginatedResponse } from 'nestjs-pageable-fn';
interface User {
id: string;
name: string;
email: string;
}
class UserPagination extends Pageable<User> {
async fetchData(params: PaginationParams): Promise<{ data: User[]; totalCount: number }> {
const { pageNumber, pageSize, searchTerm, searchBy, sortBy, sorting } = params;
// Example: Fetch from DB here
const data: User[] = []; // fetch paginated & filtered data
const totalCount = 0; // fetch total matching count
return { data, totalCount };
}
}2. Call paginate()
const userPagination = new UserPagination();
const result = await userPagination.paginate({
pageNumber: 1,
pageSize: 10,
searchTerm: 'john',
searchBy: ['name', 'email'],
sortBy: 'name',
sorting: 'asc'
});
console.log(result);
/*
{
"contents": [...],
"totalCount": 100,
"totalPage": 10,
"pageNumber": 1,
"pageSize": 10,
"isFirstPage": true,
"isLastPage": false
}
*/📘 Interfaces
PaginationParams
interface PaginationParams {
pageNumber: number;
pageSize: number;
searchTerm?: string;
searchBy?: string[];
sortBy?: string;
sorting?: 'asc' | 'desc';
}PaginatedResponse<T>
interface PaginatedResponse<T> {
contents: T[];
totalCount: number;
totalPage: number;
pageNumber: number;
pageSize: number;
isFirstPage: boolean;
isLastPage: boolean;
}🛠 Methods
paginate(params: PaginationParams): Promise<PaginatedResponse<T>>
- Calls the abstract fetchData() method you override
- Automatically packages pagination metadata into a unified response
fetchData(params: PaginationParams): Promise<{ data: T[]; totalCount: number }>
- Override this method in your subclass
- Use it to query your database or API
✅ Example Use Cases
- NestJS services/controllers that return paginated results
- MongoDB queries using skip() and limit()
- PostgreSQL with LIMIT, OFFSET, and ORDER BY
📄 License
MIT
👨💻 Author
- Made with ❤️ by
vicky705 (vicky kumar)
✅ Github Repository
https://github.com/vicky705/nestjs-pageable.git📚 Issues
Please report ypur issues here.
