A simple and efficient pagination library for FastAPI applications.
- 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
poetry add paginate-fastapi
pip install paginate-fastapi
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' }
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)
curl -X GET "http://localhost:8000/users/?page=1&page_size=10"
{
"items": [
{
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"age": 30
}
],
"total": 100,
"page": 1,
"page_size": 10,
"pages": 10,
"has_next": true,
"has_previous": false
}
# Sort by name ascending
users/?sort_by=name&sort_order=asc
# Sort by age descending
users/?sort_by=age&sort_order=desc
# 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]
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
# 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
make test
# Run all code quality checks
make pre-commit
# Format code only
make format
# Run linters only
make lint
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
This project is licensed under the MIT License - see the LICENSE file for details.