Skip to content

Commit 027fadb

Browse files
committed
chore(enhance): pagination feature
1 parent ecefbeb commit 027fadb

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

docs/guide/pagination.md

+48-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,58 @@ const dataPagination = new DataPagination();
1212

1313
app.get('/users', async (req: Request, res: Response) => {
1414
try {
15-
const page = parseInt(req.query.page as string) || 1;
16-
const limit = parseInt(req.query.limit as string) || 10;
15+
const { page, limit, sort, select, populate } = req.query;
16+
const options = {
17+
page: parseInt(page as string, 10) || 1,
18+
limit: parseInt(limit as string, 10) || 10,
19+
sort: sort ? JSON.parse(sort as string) : { createdAt: -1 },
20+
select: select ? JSON.parse(select as string) : null,
21+
populate: populate ? JSON.parse(populate as string) : null,
22+
};
1723

18-
const result = await dataPagination.paginateResult(User, page, limit);
24+
const result = await dataPagination.paginateResult(User, {}, options);
25+
res.status(200).json(result);
26+
} catch (err) {
27+
res.status(500).json({ message: 'Failed to paginate users', error: err.message });
28+
}
29+
});
30+
31+
const PORT = process.env.PORT || 5000;
32+
app.listen(PORT, () => {
33+
console.log(`Server is running on http://localhost:${PORT}`);
34+
});
35+
```
36+
37+
## Example query to filter users
38+
39+
You can also filter users based on certain criteria. For example, if you want to filter users older than a certain age, you can modify the query as follows:
40+
41+
```typescript
42+
import express, { Request, Response } from 'express';
43+
import { DataPagination } from 'mongodb-atlas-sdk';
44+
import { User } from '../model/userModel';
45+
46+
const app = express();
47+
const dataPagination = new DataPagination();
48+
49+
app.get('/users', async (req: Request, res: Response) => {
50+
try {
51+
const { page, limit, sort, select, populate, age } = req.query;
52+
const options = {
53+
page: parseInt(page as string, 10) || 1,
54+
limit: parseInt(limit as string, 10) || 10,
55+
sort: sort ? JSON.parse(sort as string) : { createdAt: -1 },
56+
select: select ? JSON.parse(select as string) : null,
57+
populate: populate ? JSON.parse(populate as string) : null,
58+
};
59+
60+
// Example query to filter users older than a certain age
61+
const query = age ? { age: { $gt: parseInt(age as string, 10) } } : {};
1962

63+
const result = await dataPagination.paginateResult(User, query, options);
2064
res.status(200).json(result);
2165
} catch (err) {
22-
res.status(500).json({ message: 'Failed to paginate data', error: err.message });
66+
res.status(500).json({ message: 'Failed to paginate users', error: err.message });
2367
}
2468
});
2569

src/core/pagination.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
import mongoose from 'mongoose';
22
import { logger } from '../logger.ts';
33

4+
interface PaginationOptions {
5+
page: number;
6+
limit: number;
7+
sort?: any;
8+
select?: any;
9+
populate?: any;
10+
}
11+
412
export class DataPagination {
5-
async paginateResult(model: mongoose.Model<any>, page: number, limit: number, query: any = {}): Promise<any> {
13+
async paginateResult(model: mongoose.Model<any>, query: any = {}, options: PaginationOptions): Promise<any> {
14+
let { page = 1, limit = 10, sort, select, populate } = options;
15+
616
if (page < 1) {
717
page = 1;
818
}
19+
920
try {
1021
const skip = (page - 1) * limit;
11-
const result = await model.find(query).sort({ createdAt: -1 }).skip(skip).limit(limit).lean().exec();
22+
let queryBuilder = model.find(query).skip(skip).limit(limit).lean();
23+
24+
if (sort) {
25+
queryBuilder.sort(sort);
26+
} else {
27+
queryBuilder = queryBuilder.sort({ createdAt: -1 }); // Default sorting
28+
}
29+
30+
if (select) {
31+
queryBuilder = queryBuilder.select(select);
32+
}
33+
34+
if (populate) {
35+
queryBuilder = queryBuilder.populate(populate);
36+
}
37+
38+
const result = await queryBuilder.exec();
1239
const total = await model.countDocuments(query).exec();
1340

1441
return {

0 commit comments

Comments
 (0)