Production-ready BaseRepository, BaseService, BaseSchema, BaseRouter, and Model Mixins for FastAPI + SQLAlchemy 2.0 projects
Project description
FastAPI CRUD Toolkit
Production-ready base classes for FastAPI + SQLAlchemy 2.0 projects.
Stop writing repetitive CRUD code. This toolkit provides generic, reusable base classes that handle 90% of common backend operations.
Installation
pip install fastapi-crud-toolkit
Or install from GitHub:
pip install git+https://github.com/your-username/fastapi-crud-toolkit.git
What's Included
| Module | Description | Methods |
|---|---|---|
BaseRepository |
Generic async repository | 26 DB operations |
BaseService |
Generic service layer | 40+ business logic operations |
BaseSchema |
Pydantic schemas | 16 request/response patterns |
BaseCRUDRouter |
Auto-generated endpoints | 8 REST endpoints |
Model Mixins |
Reusable column patterns | 9 mixins |
Exceptions |
Structured HTTP errors | 16 exception classes |
Quick Start
1. Define your model
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from fastapi_crud_toolkit import UUIDMixin, TimestampMixin
class Base(DeclarativeBase):
pass
class User(UUIDMixin, TimestampMixin, Base):
__tablename__ = "users"
name: Mapped[str] = mapped_column(String(255))
email: Mapped[str] = mapped_column(String(255), unique=True)
2. Create repository
from fastapi_crud_toolkit import BaseRepository
class UserRepository(BaseRepository[User]):
def __init__(self, db: AsyncSession):
super().__init__(User, db)
3. Create service
from fastapi_crud_toolkit import BaseService
class UserService(BaseService[User, UserRepository]):
resource_type = "user"
audit_enabled = True
searchable_fields = ["name", "email"]
def __init__(self, db: AsyncSession):
super().__init__(UserRepository(db), db)
4. Create endpoints (auto-generated)
from fastapi_crud_toolkit import BaseCRUDRouter
crud = BaseCRUDRouter(
service_class=UserService,
response_schema=UserResponse,
create_schema=UserCreate,
update_schema=UserUpdate,
resource_name="user",
tags=["users"],
get_db=get_db,
get_current_user=get_current_user,
)
app.include_router(crud.router, prefix="/api/v1/users")
This generates: GET /, GET /{id}, POST /, PUT /{id}, DELETE /{id}, GET /search, GET /count, POST /bulk-delete
5. Register exception handlers
from fastapi_crud_toolkit import register_exception_handlers
app = FastAPI()
register_exception_handlers(app)
BaseRepository Methods
| Category | Methods |
|---|---|
| Read | get_by_id, get_by_ids, get_all, get_by_filters, get_first, paginate, search |
| Count | count, exists, exists_by_field |
| Create | create, bulk_create, get_or_create, upsert |
| Update | update, update_by_id, bulk_update |
| Delete | delete, delete_by_id, bulk_delete, soft_delete, restore |
| Aggregate | aggregate (count/sum/avg/min/max + group_by) |
| Utility | refresh, execute_raw |
BaseService Methods
Includes everything from BaseRepository plus:
| Category | Methods |
|---|---|
| CRUD | All repo methods + automatic audit logging |
| Hooks | _validate_create, _pre_create, _post_create, _validate_update, _pre_update, _post_update, _pre_delete, _post_delete |
| Permissions | check_permission, check_permission_or_raise |
| Export/Import | to_dict, export_list, import_list |
| Utility | clone, archive, refresh, search |
Exceptions
from fastapi_crud_toolkit import NotFoundException, DuplicateException
raise NotFoundException("User", id="abc-123")
# → 404: "User with id 'abc-123' not found"
raise DuplicateException("User", field="email", value="test@mail.com")
# → 409: "User with email='test@mail.com' already exists"
Requirements
- Python >= 3.10
- SQLAlchemy >= 2.0
- Pydantic >= 2.0
- FastAPI >= 0.100
License
MIT
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_toolkit-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_crud_toolkit-0.1.0.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9977161452e722a6c662c584bbdded02f1b5ca9696709ef0b08282e24bbac872
|
|
| MD5 |
5479293972f3b1e058a372c49c250e27
|
|
| BLAKE2b-256 |
0b6b71aaa735c9d3513d74632ba7306a72a8c6fcf8ab6faaec841c502c433d77
|
File details
Details for the file fastapi_crud_toolkit-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_crud_toolkit-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
338dfaf7c76fb6349a7f3980e1029f7753aa2852527eb209cfa7137992e3c325
|
|
| MD5 |
d6b5b3948f72b3518dbae87104b163c0
|
|
| BLAKE2b-256 |
7f190b54144929dfbcc5121cb6761a4c280f91bf2b5f78ea30b4a5b5aead930b
|