A simple and efficient pagination library for FastAPI applications
Project description
Paginate FastAPI
A simple and efficient pagination library for FastAPI applications.
Features
- Easy-to-use pagination with FastAPI
- Async support out of the box
- Flexible filtering options
- Customizable sorting
- Type-safe with full type hints
- Compatible with FastAPI
Installation
Using Poetry
poetry add paginate-fastapi
Using Pip
pip install paginate-fastapi
Quick Start
Usage as decorator
from pagination.decorator import paginate
@router.get("/", response_model=PageResponse[YourModel])
@paginate(YourModel, lambda: get_db)
async def get_items(
db: AsyncSession = Depends(get_db),
pagination: PaginationParams = Depends()
):
...
return { 'extra_data': 'data' }
Usage as middleware
from fastapi import FastAPI, Depends
from sqlmodel import SQLModel, Field
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from pagination import PaginationMiddleware, PaginationParams
app = FastAPI()
# Initialize your database
engine = create_async_engine("sqlite+aiosqlite:///database.db")
async def get_session() -> AsyncSession:
async with AsyncSession(engine) as session:
yield session
paginator = PaginationMiddleware(get_session)
# Define your model
class User(SQLModel, table=True):
id: int = Field(primary_key=True)
name: str
email: str
age: int
# Add pagination to your endpoint
@app.get("/users/")
async def get_users(
pagination: PaginationParams = Depends(),
paginator: PaginationMiddleware = Depends(lambda: paginator),
):
return await paginator.paginate(User, pagination)
Sample Request and Response
curl -X GET "http://localhost:8000/users/?page=1&page_size=10"
{
"items": [
{
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com",
"age": 30
}
],
"total": 100,
"page": 1,
"page_size": 10,
"pages": 10,
"has_next": true,
"has_previous": false
}
Sorting
# Sort by name ascending
users/?sort_by=name&sort_order=asc
# Sort by age descending
users/?sort_by=age&sort_order=desc
Filtering
# Filter users by age greater than 30
users/?filter_field=age&filter_operator=gt&filter_value=30
# Filter users by name containing "John"
users/?filter_field=name&filter_operator=like&filter_value=John
# Filter users by age in a list
users/?filter_field=age&filter_operator=in&filter_value=[25,30,35]
Available Filter Operators
eq: Equal tone: Not equal togt: Greater thanlt: Less thange: Greater than or equal tole: Less than or equal tolike: Contains (case-sensitive)ilike: Contains (case-insensitive)in: In list of valuesnot_in: Not in list of values
Development
Setup
# Clone the repository
git clone https://github.com/ritvikdayal/paginate-fastapi.git
cd paginate-fastapi
# Install dependencies
poetry install --with dev
# Setup pre-commit hooks (optional)
make setup-hooks
Running Tests
make test
Code Quality
# Run all code quality checks
make pre-commit
# Format code only
make format
# Run linters only
make lint
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file paginate_fastapi-1.0.2.tar.gz.
File metadata
- Download URL: paginate_fastapi-1.0.2.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
172836a5261766fbb3fabe4d61b0185a52c1cc01df601e2f41a81ef958d444e8
|
|
| MD5 |
c127441a2a74a9aa4c6aa907dcec9352
|
|
| BLAKE2b-256 |
0f7c10b8951ff4b853d60140a470f54086de19ae3824426aa29ff25e6113426e
|
Provenance
The following attestation bundles were made for paginate_fastapi-1.0.2.tar.gz:
Publisher:
publish.yml on RitvikDayal/paginate-fastapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
paginate_fastapi-1.0.2.tar.gz -
Subject digest:
172836a5261766fbb3fabe4d61b0185a52c1cc01df601e2f41a81ef958d444e8 - Sigstore transparency entry: 173561483
- Sigstore integration time:
-
Permalink:
RitvikDayal/paginate-fastapi@a7fba64e44ef6e6d3b69f4fad8ddbfe7c05f51e5 -
Branch / Tag:
refs/tags/v1.0.2 - Owner: https://github.com/RitvikDayal
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a7fba64e44ef6e6d3b69f4fad8ddbfe7c05f51e5 -
Trigger Event:
release
-
Statement type:
File details
Details for the file paginate_fastapi-1.0.2-py3-none-any.whl.
File metadata
- Download URL: paginate_fastapi-1.0.2-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27f9c6c2912ca2d34069ba72270571018b696b6f7e4867dac880917ced73ae23
|
|
| MD5 |
58767611096178ebc94528c5e34ef518
|
|
| BLAKE2b-256 |
6778f4b4b14a72ac44fd4bed7287d0fd8b2a134ac2d19faaaa710f5e2b35d554
|
Provenance
The following attestation bundles were made for paginate_fastapi-1.0.2-py3-none-any.whl:
Publisher:
publish.yml on RitvikDayal/paginate-fastapi
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
paginate_fastapi-1.0.2-py3-none-any.whl -
Subject digest:
27f9c6c2912ca2d34069ba72270571018b696b6f7e4867dac880917ced73ae23 - Sigstore transparency entry: 173561484
- Sigstore integration time:
-
Permalink:
RitvikDayal/paginate-fastapi@a7fba64e44ef6e6d3b69f4fad8ddbfe7c05f51e5 -
Branch / Tag:
refs/tags/v1.0.2 - Owner: https://github.com/RitvikDayal
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a7fba64e44ef6e6d3b69f4fad8ddbfe7c05f51e5 -
Trigger Event:
release
-
Statement type: