Skip to main content

Fast Paginate

A lightweight pagination library for FastAPI with SQLAlchemy 2.x.

Simple, dependency-friendly page number pagination without unnecessary complexity.


Features

  • FastAPI dependency injection support
  • Async SQLAlchemy 2.x
  • Page number pagination
  • Automatic pagination metadata
  • Next & Previous page URLs
  • Generic Pydantic v2 response models
  • Lightweight and easy to integrate

Requirements

  • Python 3.12+
  • FastAPI
  • Pydantic v2
  • SQLAlchemy 2.x

Installation

pip install fast-paginate

Quick Start

from typing import Annotated

from fastapi import APIRouter, Depends, Request
from sqlalchemy.ext.asyncio import AsyncSession

from fast_paginate import (
    Paginate,
    ApiPaginateResponse,
    paginate,
)

router = APIRouter()


@router.get(
    "/users",
    response_model=ApiPaginateResponse[list[UserOut]],
)
async def list_users(
    request: Request,
    pagination: Annotated[Paginate, Depends()],
    session: AsyncSession = Depends(get_async_db),
):

    total, users = await paginate(
        session=session,
        model=User,
        limit=pagination.limit,
        offset=pagination.offset,
    )

    return pagination.get_paginated_response(
        total=total,
        data=users,
        request=request,
    )

Example Response

{
  "status": "success",
  "message": "All Records are Retrieved Successfully",
  "data": [
    {
      "id": 1,
      "name": "John"
    }
  ],
  "meta": {
    "total_pages": 10,
    "total_records": 196,
    "current_page": 2,
    "page_size": 20,
    "next": "http://localhost:8000/users?page=3&page_size=20",
    "previous": "http://localhost:8000/users?page=1&page_size=20"
  }
}

Pagination Parameters

Parameter Type Default Description
page int 1
page_size int 20 Records per page

Example:

GET /users?page=2&page_size=20

API Reference

Paginate

Dependency class responsible for calculating pagination values.

pagination = Annotated[Paginate, Depends()]

Available attributes:

pagination.limit
pagination.offset

paginate()

Fetches paginated records from SQLAlchemy.

total, items = await paginate(
    session=session,
    model=User,
    limit=20,
    offset=40,
)

Returns

(total_records, items)

ApiPaginateResponse

Generic response model.

ApiPaginateResponse[list[UserOut]]

Produces

{
    "status": "...",
    "message": "...",
    "data": [...],
    "meta": {...}
}

get_paginated_response()

Builds the final API response.

return pagination.get_paginated_response(
    total=total,
    data=users,
    request=request,
)

Roadmap

Future plans include:

  • Cursor Pagination
  • Offset Pagination
  • Sync SQLAlchemy support
  • Custom response messages
  • Custom page parameter names

Dependencies

  • FastAPI
  • Pydantic v2
  • SQLAlchemy 2.x

License

MIT License

Release files for fast-paginate 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fast-paginate 0.1.0
File Size Uploaded
fast_paginate-0.1.0.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fast-paginate 0.1.0
File Interpreter ABI Platform
fast_paginate-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.9 kB

Release files / fast_paginate-0.1.0.tar.gz

Download URL fast_paginate-0.1.0.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
73dd3a39b7b2eca81a4d5bf30fb4b6d6d4150ce429190db9b6eea5a2a7a3b1a0
BLAKE2b-256 checksum
How to use checksums
cc1367949b5661056d771150b04bbeb9daeb10a096d3ac0e5511ba57de95243b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / fast_paginate-0.1.0-py3-none-any.whl

Download URL fast_paginate-0.1.0-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d554a76dee18c8a3556ff50aed0bdee23c6d6a1e0d2487dd83d06c8af846ad95
BLAKE2b-256 checksum
How to use checksums
a4a1589bb51137175329e2d815cc334d76eaebb9602d73189d4c347e4e5d1f6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page