Skip to main content

Pagination for FastAPI

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fast_paginate-0.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fast_paginate-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file fast_paginate-0.1.0.tar.gz.

File metadata

  • Download URL: fast_paginate-0.1.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for fast_paginate-0.1.0.tar.gz
Algorithm Hash digest
SHA256 73dd3a39b7b2eca81a4d5bf30fb4b6d6d4150ce429190db9b6eea5a2a7a3b1a0
MD5 9b3861e91e5e8c81d225190c74623cca
BLAKE2b-256 cc1367949b5661056d771150b04bbeb9daeb10a096d3ac0e5511ba57de95243b

See more details on using hashes here.

File details

Details for the file fast_paginate-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fast_paginate-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for fast_paginate-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d554a76dee18c8a3556ff50aed0bdee23c6d6a1e0d2487dd83d06c8af846ad95
MD5 e234f2d14c8062e883582fdf13d88619
BLAKE2b-256 a4a1589bb51137175329e2d815cc334d76eaebb9602d73189d4c347e4e5d1f6b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page