|
2 | 2 |
|
3 | 3 | __all__ = [ |
4 | 4 | "apaginate", |
| 5 | + "apaginate_aggregate", |
5 | 6 | "paginate", |
| 7 | + "paginate_aggregate", |
6 | 8 | ] |
7 | 9 |
|
8 | 10 |
|
9 | 11 | from collections.abc import Mapping, Sequence |
10 | | -from typing import Any, Optional, TypeVar |
| 12 | +from typing import Any, Literal, Optional, TypeVar, Union |
11 | 13 |
|
12 | 14 | from pymongo.asynchronous.collection import AsyncCollection |
13 | 15 | from pymongo.collection import Collection |
14 | 16 |
|
15 | 17 | from fastapi_pagination.bases import AbstractParams |
16 | 18 | from fastapi_pagination.config import Config |
17 | | -from fastapi_pagination.flow import flow_expr, run_async_flow, run_sync_flow |
18 | | -from fastapi_pagination.flows import generic_flow |
| 19 | +from fastapi_pagination.ext.utils import get_mongo_pipeline_filter_end |
| 20 | +from fastapi_pagination.flow import flow, flow_expr, run_async_flow, run_sync_flow |
| 21 | +from fastapi_pagination.flows import create_page_flow, generic_flow |
19 | 22 | from fastapi_pagination.types import AdditionalData, ItemsTransformer, SyncItemsTransformer |
| 23 | +from fastapi_pagination.utils import verify_params |
20 | 24 |
|
21 | 25 | T = TypeVar("T", bound=Mapping[str, Any]) |
22 | 26 |
|
@@ -89,3 +93,112 @@ async def apaginate( |
89 | 93 | config=config, |
90 | 94 | ) |
91 | 95 | ) |
| 96 | + |
| 97 | + |
| 98 | +@flow |
| 99 | +def _aggregate_flow( |
| 100 | + is_async: bool, |
| 101 | + collection: Union[Collection[T], AsyncCollection[T]], |
| 102 | + aggregate_pipeline: Optional[list[dict[Any, Any]]] = None, |
| 103 | + params: Optional[AbstractParams] = None, |
| 104 | + *, |
| 105 | + transformer: Optional[ItemsTransformer] = None, |
| 106 | + additional_data: Optional[AdditionalData] = None, |
| 107 | + aggregation_filter_end: Optional[Union[int, Literal["auto"]]] = None, |
| 108 | + config: Optional[Config] = None, |
| 109 | +) -> Any: |
| 110 | + params, raw_params = verify_params(params, "limit-offset") |
| 111 | + aggregate_pipeline = aggregate_pipeline or [] |
| 112 | + |
| 113 | + paginate_data = [] |
| 114 | + if raw_params.limit is not None: |
| 115 | + paginate_data.append({"$limit": raw_params.limit + (raw_params.offset or 0)}) |
| 116 | + if raw_params.offset is not None: |
| 117 | + paginate_data.append({"$skip": raw_params.offset}) |
| 118 | + |
| 119 | + if aggregation_filter_end is not None: |
| 120 | + if aggregation_filter_end == "auto": |
| 121 | + aggregation_filter_end = get_mongo_pipeline_filter_end(aggregate_pipeline) |
| 122 | + transform_part = aggregate_pipeline[:aggregation_filter_end] |
| 123 | + aggregate_pipeline = aggregate_pipeline[aggregation_filter_end:] |
| 124 | + paginate_data.extend(transform_part) |
| 125 | + |
| 126 | + cursor = yield collection.aggregate( |
| 127 | + [ |
| 128 | + *aggregate_pipeline, |
| 129 | + { |
| 130 | + "$facet": { |
| 131 | + "metadata": [{"$count": "total"}], |
| 132 | + "data": paginate_data, |
| 133 | + }, |
| 134 | + }, |
| 135 | + ], |
| 136 | + ) |
| 137 | + |
| 138 | + data, *_ = yield cursor.to_list(length=None) |
| 139 | + |
| 140 | + items = data["data"] |
| 141 | + try: |
| 142 | + total = data["metadata"][0]["total"] |
| 143 | + except IndexError: |
| 144 | + total = 0 |
| 145 | + |
| 146 | + page = yield from create_page_flow( |
| 147 | + items, |
| 148 | + params, |
| 149 | + total=total, |
| 150 | + transformer=transformer, |
| 151 | + additional_data=additional_data, |
| 152 | + config=config, |
| 153 | + async_=is_async, |
| 154 | + ) |
| 155 | + |
| 156 | + return page |
| 157 | + |
| 158 | + |
| 159 | +async def apaginate_aggregate( |
| 160 | + collection: AsyncCollection[T], |
| 161 | + aggregate_pipeline: Optional[list[dict[Any, Any]]] = None, |
| 162 | + params: Optional[AbstractParams] = None, |
| 163 | + *, |
| 164 | + transformer: Optional[ItemsTransformer] = None, |
| 165 | + additional_data: Optional[AdditionalData] = None, |
| 166 | + aggregation_filter_end: Optional[Union[int, Literal["auto"]]] = None, |
| 167 | + config: Optional[Config] = None, |
| 168 | +) -> Any: |
| 169 | + return await run_async_flow( |
| 170 | + _aggregate_flow( |
| 171 | + is_async=True, |
| 172 | + collection=collection, |
| 173 | + aggregate_pipeline=aggregate_pipeline or [], |
| 174 | + params=params, |
| 175 | + transformer=transformer, |
| 176 | + additional_data=additional_data, |
| 177 | + aggregation_filter_end=aggregation_filter_end, |
| 178 | + config=config, |
| 179 | + ) |
| 180 | + ) |
| 181 | + |
| 182 | + |
| 183 | +def paginate_aggregate( |
| 184 | + collection: Collection[T], |
| 185 | + aggregate_pipeline: Optional[list[dict[Any, Any]]] = None, |
| 186 | + params: Optional[AbstractParams] = None, |
| 187 | + *, |
| 188 | + transformer: Optional[SyncItemsTransformer] = None, |
| 189 | + additional_data: Optional[AdditionalData] = None, |
| 190 | + aggregation_filter_end: Optional[Union[int, Literal["auto"]]] = None, |
| 191 | + config: Optional[Config] = None, |
| 192 | +) -> Any: |
| 193 | + return run_sync_flow( |
| 194 | + _aggregate_flow( |
| 195 | + is_async=False, |
| 196 | + collection=collection, |
| 197 | + aggregate_pipeline=aggregate_pipeline or [], |
| 198 | + params=params, |
| 199 | + transformer=transformer, |
| 200 | + additional_data=additional_data, |
| 201 | + aggregation_filter_end=aggregation_filter_end, |
| 202 | + config=config, |
| 203 | + ) |
| 204 | + ) |
0 commit comments