Skip to content

Commit

Permalink
πŸŽ… Add beanie integration (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
uriyyo authored Sep 11, 2022
1 parent 70835ec commit c2263c9
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 643 deletions.
27 changes: 27 additions & 0 deletions fastapi_pagination/ext/beanie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Optional, TypeVar, Union

from beanie import Document
from beanie.odm.queries.find import FindMany

from ..api import create_page, resolve_params
from ..bases import AbstractPage, AbstractParams

TDocument = TypeVar("TDocument", bound=Document)


async def paginate(
query: Union[TDocument, FindMany[TDocument]],
params: Optional[AbstractParams] = None,
) -> AbstractPage[TDocument]:
params = resolve_params(params)
raw_params = params.to_raw_params()

items = await query.find_many(limit=raw_params.limit, skip=raw_params.offset).to_list()
total = await query.count()

return create_page(items, total, params)


__all__ = [
"paginate",
]
Loading

0 comments on commit c2263c9

Please sign in to comment.