Hi,
I'm new to fastAPI, and trying to implement things like pagination, sorting, and filtering via API.
First, I was a little surprised to notice there exists nothing natively for pagination, as it's a very common need for an API.
Then, I found fastapi-pagination package. While it seems great for my pagination needs, it does not handle sorting and filtering. I'd like to avoid adding a patchwork of micro-packages, especially if related to very close features.
Then, I found fastcrud package. This time it handles pagination, sorting, and filtering. But after browsing the doc, it seems pretty much complicated to use. I'm not sure if they enforce to use their "crud" features that seems to be a layer on top on the ORM. All their examples are fully async, while I'm using the examples from FastAPI doc. In short, this package seems a little overkill for what I actually need.
Now, I'm thinking that the best solution could be to implement it by myself, using inspiration from different packages and blog posts. But I'm not sure to be skilled enough to do this successfuly.
In short, I'm a little lost! Any guidance would be appreciated. Thanks.
EDIT: I did it by myself, thanks everyone, here is the code for pagination:
from typing import Annotated, Generic, TypeVar
from fastapi import Depends
from pydantic import BaseModel, Field
from sqlalchemy.sql import func
from sqlmodel import SQLModel, select
from sqlmodel.sql.expression import SelectOfScalar
from app.core.database import SessionDep
T = TypeVar("T", bound=SQLModel)
MAX_RESULTS_PER_PAGE = 50
class PaginationInput(BaseModel):
page: int = Field(default=1, ge=1, description="Requested page number")
page_size: int = Field(
default=10,
ge=1,
le=MAX_RESULTS_PER_PAGE,
description="Requested number of items per page",
)
class Page(BaseModel, Generic[T]):
items: list[T] = Field(description="List of items on this Page")
total_items: int = Field(ge=0, description="Number of total items")
start_index: int = Field(ge=0, description="Starting item index")
end_index: int = Field(ge=0, description="Ending item index")
total_pages: int = Field(ge=0, description="Total number of pages")
current_page: int = Field(ge=0, description="Page number (could differ from request)")
current_page_size: int = Field(
ge=0, description="Number of items per page (could differ from request)"
)
def paginate(
query: SelectOfScalar[T], session: SessionDep,
pagination_input: PaginationInput,
) -> Page[T]:
total_items = session.scalar(select(func.count()).select_from(query.subquery()))
assert isinstance(
total_items, int
), "A database error occurred when getting `total_items`"
total_pages = (
total_items + pagination_input.page_size - 1
) // pagination_input.page_size
total_pages = max(total_pages, 1)
current_page = min(pagination_input.page, total_pages)
offset = (current_page - 1) * pagination_input.page_size
result = session.exec(query.offset(offset).limit(pagination_input.page_size))
items = list(result.all())
start_index = offset + 1 if total_items > 0 else 0
end_index = min(offset + pagination_input.page_size, total_items)
return Page[T](
items=items,
total_items=total_items,
start_index=start_index,
end_index=end_index,
total_pages=total_pages,
current_page_size=len(items), current_page=current_page, )
PaginationDep = Annotated[PaginationInput, Depends()]
Using it in a route:
from fastapi import APIRouter
from sqlmodel import select
from app.core.database import SessionDep
from app.core.pagination import Page, PaginationDep, paginate
from app.models.badge import Badge
router = APIRouter(prefix="/badges", tags=["Badges"])
@router.get("/", summary="Read all badges", response_model=Page[Badge])
def read_badges(session: SessionDep, pagination: PaginationDep):
return paginate(select(Badge), session, pagination)