https://github.com/digen21/dmxdev-mongoose-paginated-query
Pagination With Mongoose for NestJS
https://github.com/digen21/dmxdev-mongoose-paginated-query
mongoose pagination typescript
Last synced: 3 months ago
JSON representation
Pagination With Mongoose for NestJS
- Host: GitHub
- URL: https://github.com/digen21/dmxdev-mongoose-paginated-query
- Owner: digen21
- License: mit
- Created: 2024-12-21T20:53:49.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-01-19T11:05:34.000Z (6 months ago)
- Last Synced: 2025-03-25T21:35:08.087Z (3 months ago)
- Topics: mongoose, pagination, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@dmxdev/mongoose-paginated-query
- Size: 356 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Mongoose Paginate
A simple and type-safe pagination utility for Mongoose.
## Installation
```bash
npm install @dmxdev/mongoose-paginated-query
```## Usage
### Basic Usage
```typescript
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',
});
```### Types
```typescript
interface PaginationOptions {
page: number;
limit: number;
sort?: Record;
}interface MongoosePaginateParams {
schema: Model;
options: PaginationOptions;
query?: Record;
populate?: PopulateInput;
select?: string;
}interface PaginationResult {
docs: T[];
totalDocs: number;
limit: number;
page: number;
totalPages: number;
hasNextPage: boolean;
hasPrevPage: boolean;
}
```### Population Options
The `populate` parameter supports complex population scenarios:
```typescript
const populate = [
{
path: 'author',
select: 'name email',
populate: {
path: 'profile',
select: 'avatar',
},
},
];const result = await mongoosePaginate({
schema: YourModel,
options: { page: 1, limit: 10 },
populate,
});
```## Features
- Type-safe pagination
- Flexible population options
- Field selection support
- Sorting capabilities
- Automatic total pages calculation
- Previous/Next page indicators## License
MIT