Django-like Repository & QuerySet layer for SQLAlchemy (async-first)
sqlalchemy-repository brings a familiar, expressive, and powerful QuerySet API on top of SQLAlchemy, inspired by Django ORM — but designed for modern async Python.
It helps you write cleaner, composable database logic with support for:
- 🔍 Django-style filtering (
Q,F) - 🔗 Relation traversal via
__syntax - 📊 Aggregations (
Count,Min,Max, etc.) - ⚡ Async-first design
- 🧱 Repository patterns
- 🧠 Type-safe and extensible
-
QuerySet API
await repo.objects.filter(Q(price__gt=100) & Q(active=True)).all()
-
Django-style lookups
Q(name__icontains="bmw") Q(category__parent__id=1)
-
F expressions (field references)
await repo.objects.update(price=F("price") * 1.1)
-
Aggregations
await repo.objects.annotate(total=Count("id"), max_price=Max("price"))
-
Annotations
await repo.objects.annotate(total=F("qty") * F("unit_price"))
-
Async support (SQLAlchemy 2.0+)
-
Composable query expressions
-
Repository pattern abstraction
pip install sqlalchemy-repository- Python 3.11+
- SQLAlchemy 2.0+
from sqlalchemy_repository import BaseRepository
class ProductRepository(BaseRepository[Product]):
model = Productproducts = await repo.objects.filter(
Q(price__gt=100),
Q(category__name="Engine")
).order_by("-price").all()product = await repo.objects.get(id=1)await repo.create(
name="Brake Pad",
price=50
)await repo.objects.filter(id=1).update(
price=F("price") * 1.2
)await repo.objects.filter(price__lt=10).delete()await repo.objects.filter(
category__parent__name="Auto Parts"
)No manual joins needed — handled internally.
from sqlalchemy_repository import Q
query = Q(price__gt=100) & (Q(stock__lt=5) | Q(discount=True))
await repo.objects.filter(query).all()from sqlalchemy_repository import Count, Max
result = await repo.objects.annotate(
total=Count("id"),
max_price=Max("price")
)qs = repo.objects.annotate(
total_price=F("quantity") * F("unit_price")
)
data = await qs.all()Install dev dependencies:
pip install -e .[dev]Run linters:
ruff check .
mypy .Contributions are welcome.
- Fork the repo
- Create a feature branch
- Write tests
- Open a PR
MIT License