|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -__all__ = ["paginate"] |
| 3 | +__all__ = [ |
| 4 | + "apaginate", |
| 5 | + "paginate", |
| 6 | +] |
| 7 | + |
4 | 8 |
|
5 | 9 | from collections.abc import Mapping, Sequence |
6 | 10 | from typing import Any, Optional, TypeVar |
7 | 11 |
|
| 12 | +from pymongo.asynchronous.collection import AsyncCollection |
8 | 13 | from pymongo.collection import Collection |
9 | 14 |
|
10 | 15 | from fastapi_pagination.bases import AbstractParams |
11 | 16 | from fastapi_pagination.config import Config |
12 | | -from fastapi_pagination.flow import flow_expr, run_sync_flow |
| 17 | +from fastapi_pagination.flow import flow_expr, run_async_flow, run_sync_flow |
13 | 18 | from fastapi_pagination.flows import generic_flow |
14 | | -from fastapi_pagination.types import AdditionalData, SyncItemsTransformer |
| 19 | +from fastapi_pagination.types import AdditionalData, ItemsTransformer, SyncItemsTransformer |
15 | 20 |
|
16 | 21 | T = TypeVar("T", bound=Mapping[str, Any]) |
17 | 22 |
|
@@ -49,3 +54,38 @@ def paginate( |
49 | 54 | config=config, |
50 | 55 | ) |
51 | 56 | ) |
| 57 | + |
| 58 | + |
| 59 | +async def apaginate( |
| 60 | + collection: AsyncCollection[T], |
| 61 | + query_filter: Optional[dict[Any, Any]] = None, |
| 62 | + filter_fields: Optional[dict[Any, Any]] = None, |
| 63 | + params: Optional[AbstractParams] = None, |
| 64 | + sort: Optional[Sequence[Any]] = None, |
| 65 | + *, |
| 66 | + transformer: Optional[ItemsTransformer] = None, |
| 67 | + additional_data: Optional[AdditionalData] = None, |
| 68 | + config: Optional[Config] = None, |
| 69 | + **kwargs: Any, |
| 70 | +) -> Any: |
| 71 | + query_filter = query_filter or {} |
| 72 | + |
| 73 | + return await run_async_flow( |
| 74 | + generic_flow( |
| 75 | + total_flow=flow_expr(lambda: collection.count_documents(query_filter)), |
| 76 | + limit_offset_flow=flow_expr( |
| 77 | + lambda raw_params: collection.find( |
| 78 | + query_filter, |
| 79 | + filter_fields, |
| 80 | + skip=raw_params.offset, |
| 81 | + limit=raw_params.limit, |
| 82 | + sort=sort, |
| 83 | + **kwargs, |
| 84 | + ).to_list() |
| 85 | + ), |
| 86 | + params=params, |
| 87 | + transformer=transformer, |
| 88 | + additional_data=additional_data, |
| 89 | + config=config, |
| 90 | + ) |
| 91 | + ) |
0 commit comments