Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎅 Add beanie integration #372

Merged
merged 2 commits into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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