DEV Community

dan
dan

Posted on

Reducing SQLAlchemy CRUD Boilerplate with a Type-Safe Repository Pattern

I’ve been working with FastAPI + SQLAlchemy, and one thing that always slowed our team down was the amount of repetitive CRUD boilerplate we had to write for each model.

Most existing solutions didn’t fully support:
SQLAlchemy 2.x style
Pydantic v2
proper mypy type inference
a clean Repository abstraction
So I built a small library that wraps SQLAlchemy ORM/Core queries with a generic, type-safe Repository layer.

Usage example:

class UserFilter(BaseRepoFilter):
    id: str | None = None

class UserRepo(BaseRepository[UserModel, UserSchema]):
    filter_class = UserFilter
Enter fullscreen mode Exit fullscreen mode

You get:

  • typed CRUD
  • async support
  • filtering with a Query DSL
  • optional domain/schema mapping

It already reduced a lot of boilerplate for us.
If you’re using SQLAlchemy heavily, I’d love feedback or suggestions.

Repo: https://github.com/4jades/base-repository

Top comments (0)