A simple and type-safe pagination utility for Mongoose.
npm install @dmxdev/mongoose-paginated-query
import mongoosePaginate from '@dmxdev/mongoose-paginated-query';
const result = await mongoosePaginate({
schema: YourModel,
options: {
page: 1,
limit: 10,
sort: { createdAt: -1 },
},
query: { status: 'active' },
populate: [{ path: 'author', select: 'name' }],
select: 'title content',
});
interface PaginationOptions {
page: number;
limit: number;
sort?: Record<string, any>;
}
interface MongoosePaginateParams<T> {
schema: Model<T>;
options: PaginationOptions;
query?: Record<string, any>;
populate?: PopulateInput;
select?: string;
}
interface PaginationResult<T> {
docs: T[];
totalDocs: number;
limit: number;
page: number;
totalPages: number;
hasNextPage: boolean;
hasPrevPage: boolean;
}
The populate
parameter supports complex population scenarios:
const populate = [
{
path: 'author',
select: 'name email',
populate: {
path: 'profile',
select: 'avatar',
},
},
];
const result = await mongoosePaginate({
schema: YourModel,
options: { page: 1, limit: 10 },
populate,
});
- Type-safe pagination
- Flexible population options
- Field selection support
- Sorting capabilities
- Automatic total pages calculation
- Previous/Next page indicators
MIT