INNER CODE UNIT · Python
default_query_count
dialoguemd/fastapi-sqla · fastapi_sqla/async_pagination.py:21
async def default_query_count(session: SqlaAsyncSession, query: Select) -> int:
result = await session.execute(select(func.count()).select_from(query.subquery()))
return cast(int, result.scalar())
async def paginate_query(
query: Select,
session: SqlaAsyncSession,
total_items: int,
offset: int,
limit: int,
*,
scalars: bool = True,
) -> Page:
total_pages = math.ceil(total_items / limit)
page_number = math.floor(offset / limit + 1)
query = query.offset(offset).limit(limit)
result = await session.execute(query)