Skip to main content

Repository and Django-like QuerySet layers for SQLAlchemy with async support

Project description

📦 sqlalchemy-repository

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

🚀 Features

  • 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


📦 Installation

pip install sqlalchemy-repository

⚙️ Requirements

  • Python 3.11+
  • SQLAlchemy 2.0+

🏗 Basic Usage

Define repository

from sqlalchemy_repository import BaseRepository

class ProductRepository(BaseRepository[Product]):
    model = Product

Querying

products = await repo.objects.filter(
    Q(price__gt=100),
    Q(category__name="Engine")
).order_by("-price").all()

Get single object

product = await repo.objects.get(id=1)

Create

await repo.create(
    name="Brake Pad",
    price=50
)

Update with F expressions

await repo.objects.filter(id=1).update(
    price=F("price") * 1.2
)

Delete

await repo.objects.filter(price__lt=10).delete()

🔗 Relationships via __

await repo.objects.filter(
    category__parent__name="Auto Parts"
)

No manual joins needed — handled internally.


🔍 Q Expressions

from sqlalchemy_repository import Q

query = Q(price__gt=100) & (Q(stock__lt=5) | Q(discount=True))

await repo.objects.filter(query).all()

🧮 Aggregations

from sqlalchemy_repository import Count, Max

result = await repo.objects.annotate(
    total=Count("id"),
    max_price=Max("price")
)

🏷 Annotations

qs = repo.objects.annotate(
    total_price=F("quantity") * F("unit_price")
)

data = await qs.all()

🛠 Development

Install dev dependencies:

pip install -e .[dev]

Run linters:

ruff check .
mypy .

🤝 Contributing

Contributions are welcome.

  1. Fork the repo
  2. Create a feature branch
  3. Write tests
  4. Open a PR

📄 License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlalchemy_repository-1.1.2.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlalchemy_repository-1.1.2-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_repository-1.1.2.tar.gz.

File metadata

  • Download URL: sqlalchemy_repository-1.1.2.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sqlalchemy_repository-1.1.2.tar.gz
Algorithm Hash digest
SHA256 3e002e117ba0487e349da0ace4935cb0b927d31a8cdab5c7cae02204238bf459
MD5 1b507a872ab36248eadd88bdffc8e665
BLAKE2b-256 da2f062a9f21476a8ebd26c8f8fbbdd01e50e517338ae5c5d713e47ad50a99b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_repository-1.1.2.tar.gz:

Publisher: release.yml on AstralMortem/sqlalchemy-repository

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sqlalchemy_repository-1.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_repository-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4ecffe6cb2aa84e99180ae17a2430ef9d0b3072cb06dfb21d7a9438bba14f9ec
MD5 3e8f12f1e69cbe2c95d911db1814b8f9
BLAKE2b-256 8873964709d927e5020adb6ab563252a169129d9d7bf765e57acd5ad35e4338c

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlalchemy_repository-1.1.2-py3-none-any.whl:

Publisher: release.yml on AstralMortem/sqlalchemy-repository

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page