A powerful CRUD toolkit for FastAPI with SQLAlchemy, featuring query building, filtering, sorting, and field selection with async/sync support.
Project description
FastAPI CRUD Kit
A powerful CRUD toolkit for FastAPI with SQLAlchemy, featuring query building, filtering, sorting, and field selection with async/sync support.
Why FastAPI CRUD Kit?
Building REST APIs with FastAPI and SQLAlchemy often requires writing repetitive CRUD code, handling query parameters, managing database sessions, and implementing common patterns like pagination, filtering, and soft deletes. FastAPI CRUD Kit eliminates this boilerplate by providing:
- Ready-to-use CRUD operations that work with any SQLAlchemy model
- Advanced query building with automatic parsing and validation of query parameters
- Production-ready features like transactions, retries, timeouts, and read-only operations
- Type-safe operations with full type hints support
- Flexible architecture supporting both async and sync SQLAlchemy sessions
Features
- 🚀 Full CRUD Operations: Create, Read, Update, Delete with minimal boilerplate
- 🔍 Advanced Query Building: Filtering, sorting, field selection, and relationship loading
- ⚡ Async & Sync Support: Works seamlessly with both async and sync SQLAlchemy sessions
- 🛡️ Filter Validation: Configurable filter validation with custom callbacks
- 🔒 Type Safe: Full type hints support throughout
- 📦 Production Ready: Context managers for transactions, retries, and timeouts
- 🗑️ Soft Delete: Built-in support for soft delete functionality
- 📊 Pagination: Built-in pagination support with complete metadata
Installation
pip install fastapi-crud-kit
Or using poetry:
poetry add fastapi-crud-kit
Quick Example
from fastapi_crud_kit.crud.base import CRUDBase
from fastapi_crud_kit.models import BaseModel
from fastapi_crud_kit.query import AllowedFilters, QueryBuilderConfig, parse_query_params
from sqlalchemy import Column, String
# Define your model
class Category(BaseModel):
__tablename__ = "categories"
name = Column(String, nullable=False)
# Create CRUD class
class CategoryCRUD(CRUDBase[Category]):
def __init__(self):
query_config = QueryBuilderConfig(
allowed_filters=[AllowedFilters.exact("name")]
)
super().__init__(model=Category, use_async=True, query_config=query_config)
# Use in FastAPI route
@router.get("/categories")
async def list_categories(request: Request, db: AsyncSession = Depends(get_db)):
query_params = parse_query_params(request.query_params)
return await category_crud.list_paginated(db, query_params)
How It Works
CRUD Operations
The CRUDBase class provides all standard CRUD operations (list, get, create, update, delete) that work with any SQLAlchemy model. It automatically handles:
- Query building from URL parameters
- Filter validation and application
- Pagination with metadata
- Soft delete filtering (if supported by the model)
- Relationship loading (eager loading)
Query Building
The query builder automatically parses and validates query parameters from the request URL:
- Filters:
?filter[name]=Tech&filter[price][gte]=100 - Sorting:
?sort=name&sort=-created_at - Field Selection:
?fields=id,name - Includes:
?include=products,tags - Pagination:
?page=1&per_page=20
All query parameters are validated against your configuration, preventing SQL injection and ensuring type safety.
Base Models
The package provides ready-to-use base models with common features:
BaseModel: Includes primary key, UUID, timestamps, and soft deleteBaseModelWithUUIDPK: UUID as primary key- Individual mixins for custom combinations
Database Management
The DatabaseFactory simplifies database setup by automatically detecting the database type and configuring the appropriate drivers. It supports PostgreSQL, MySQL, and SQLite in both async and sync modes.
Context Managers
Production-ready context managers for common patterns:
- Transactions: Automatic commit/rollback
- Retries: Exponential backoff for transient failures
- Timeouts: Prevent operations from hanging
- Read-only: Enforce read-only operations
Requirements
- Python >= 3.10
- SQLAlchemy >= 2.0.45
- Pydantic >= 2.12.5
- FastAPI >= 0.128.0
Documentation
Comprehensive documentation with examples, guides, and API reference is available at:
https://mawuva.github.io/fastapi-crud-kit/
Or build it locally:
mkdocs serve
Examples
See the examples/ directory for complete working examples.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
See LICENSE file for details.
Support
For issues, questions, or contributions, please visit the GitHub repository.
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 fastapi_crud_kit-0.2.0.tar.gz.
File metadata
- Download URL: fastapi_crud_kit-0.2.0.tar.gz
- Upload date:
- Size: 35.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.11.14 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c8508e47adc44897e66a244d2c233bd3cd1b35d218b3d3b06a18f6e8c460bed
|
|
| MD5 |
f5991b1c6a88555b97a6c4f9f3f188b5
|
|
| BLAKE2b-256 |
af05ae6758fcfdd98004e0cee69b4141ebbc6d8a39053313eb59829ba75eb0b1
|
File details
Details for the file fastapi_crud_kit-0.2.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_crud_kit-0.2.0-py3-none-any.whl
- Upload date:
- Size: 55.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.11.14 Linux/6.11.0-1018-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb0f81876915c8a903dae3d596336b417f56e244565ce51fc548e5a632f89bb1
|
|
| MD5 |
16bbf88ccb532642a82cdff459e2c410
|
|
| BLAKE2b-256 |
b97fd9386900ac5f6ba27a9c266a96bdccd69a794a2c0ac2721c2934dff58582
|