Skip to content

Commit 69022d4

Browse files
committed
feat: pagination functionality
1 parent bafc477 commit 69022d4

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

docs/guide/pagination.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## How to handle pagination ?
2+
3+
Pagination is a common requirement in applications that deal with large datasets. The `mongodb-atlas-sdk` provides a simple way to implement pagination using the `DataPagination` class. let's explore how to use it effectively. It will return most recent data first.
4+
5+
```typescript
6+
import express, { Request, Response } from 'express';
7+
import { DataPagination } from 'mongodb-atlas-sdk';
8+
import { User } from '../model/userModel';
9+
10+
const app = express();
11+
const dataPagination = new DataPagination();
12+
13+
app.get('/users', async (req: Request, res: Response) => {
14+
try {
15+
const page = parseInt(req.query.page as string) || 1;
16+
const limit = parseInt(req.query.limit as string) || 10;
17+
18+
const result = await dataPagination.paginateResults(User, page, limit);
19+
20+
res.status(200).json(result);
21+
} catch (err) {
22+
res.status(500).json({ message: 'Failed to paginate data', error: err.message });
23+
}
24+
});
25+
26+
const PORT = process.env.PORT || 5000;
27+
app.listen(PORT, () => {
28+
console.log(`Server is running on http://localhost:${PORT}`);
29+
});
30+
```
31+
32+
## Example Response
33+
34+
```json
35+
{
36+
"results": [
37+
{
38+
"_id": "679a1343c406f835121d0f9d",
39+
"name": "John Doe",
40+
"email": "[email protected]",
41+
"age": 31,
42+
"__v": 0
43+
},
44+
{
45+
"_id": "67a98564855d0233c02db952",
46+
"name": "Jane Doe",
47+
"email": "[email protected]",
48+
"age": 28,
49+
"__v": 0
50+
}
51+
],
52+
"total": 2,
53+
"page": 1,
54+
"pages": 1
55+
}
56+
```

docs/summary.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
- [Installation](./guide/installation.md)
1212
- [Connect to MongoDB Atlas](./guide/connection.md)
1313
- [Create Schema and Model](./guide/schema.md)
14+
- [Backup and Restore](./guide/backup.md)
1415
- [Connection Pooling](./guide/connection-pool.md)
16+
- [Data Pagination](./guide/pagination.md)
1517
- [Query Caching](./guide/queryCaching.md)
1618
- [Audit Logging](./guide/auditLogging.md)
17-
- [Backup and Restore](./guide/backup.md)
1819

1920
</details>
2021

src/core/pagination.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import mongoose from 'mongoose';
2+
3+
export class DataPagination {
4+
async paginateResult(model: mongoose.Model<any>, page: number, limit: number, query: any = {}): Promise<any> {
5+
if (page < 1) {
6+
page = 1;
7+
}
8+
const skip = (page - 1) * limit;
9+
const result = await model.find(query).sort({ createdAt: -1 }).skip(skip).limit(limit).lean().exec();
10+
const total = await model.countDocuments(query).exec();
11+
12+
return {
13+
result,
14+
total,
15+
page,
16+
pages: Math.ceil(total / limit),
17+
};
18+
}
19+
}

0 commit comments

Comments
 (0)