Skip to content

Commit dcf2930

Browse files
authored
Add pagination support (#42)
1 parent 6e01418 commit dcf2930

File tree

12 files changed

+1688
-70
lines changed

12 files changed

+1688
-70
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.3.0] - 2025-06-11
11+
1012
### Added
1113
- Context support
1214
- Lifespan support
15+
- Pagination
1316

1417
## [0.2.0] - 2025-01-15
1518

README.md

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,54 @@ async def get_customer(customer_id: int) -> Customer:
220220
return customer
221221
```
222222

223-
### Pagination (Coming Soon)
223+
### Pagination
224224

225-
Handle large datasets efficiently:
225+
Handle large datasets efficiently with built-in pagination support:
226226

227227
```python
228+
from enrichmcp import PageResult, CursorResult
229+
230+
231+
# Page-based pagination (ideal for UIs)
232+
@app.resource
233+
async def list_customers(page: int = 1, page_size: int = 50) -> PageResult[Customer]:
234+
"""List customers with page-based pagination."""
235+
customers, total = await db.get_customers_page(page, page_size)
236+
return PageResult.create(
237+
items=customers,
238+
page=page,
239+
page_size=page_size,
240+
has_next=page * page_size < total,
241+
total_items=total,
242+
)
243+
244+
245+
# Cursor-based pagination (ideal for real-time feeds)
246+
@app.resource
247+
async def stream_orders(cursor: str | None = None, limit: int = 20) -> CursorResult[Order]:
248+
"""Stream orders with cursor-based pagination."""
249+
orders, next_cursor = await db.get_orders_cursor(cursor, limit)
250+
return CursorResult.create(items=orders, next_cursor=next_cursor, page_size=limit)
251+
252+
253+
# Paginated relationships
228254
@Customer.orders.resolver
229-
async def get_orders(customer_id: int, limit: int = 10, offset: int = 0) -> list[Order]:
230-
"""Paginated order retrieval."""
231-
return await db.get_orders(customer_id, limit=limit, offset=offset)
255+
async def get_customer_orders(
256+
customer_id: int, page: int = 1, page_size: int = 10
257+
) -> PageResult[Order]:
258+
"""Get customer orders with pagination."""
259+
orders, total = await db.get_customer_orders_page(customer_id, page, page_size)
260+
return PageResult.create(
261+
items=orders,
262+
page=page,
263+
page_size=page_size,
264+
has_next=page * page_size < total,
265+
total_items=total,
266+
)
232267
```
233268

269+
See the [Pagination Guide](docs/pagination.md) for comprehensive examples and best practices.
270+
234271
## Development
235272

236273
```bash

0 commit comments

Comments
 (0)