Async ORM Kit
Django-inspired models, managers, expressions and QuerySets — powered by SQLAlchemy's async infrastructure.
async-orm-kit gives you an expressive, chainable ORM query API while preserving SQLAlchemy 2.x mappings, drivers, sessions, transactions and composable SQL expressions. It's an async-first library for applications that want Django-style querying without adopting Django or losing SQLAlchemy control.
users = await (
User.objects.filter(session, active=True, age__gte=18)
.exclude(email__isnull=True)
.order_by("-created_at", "id")
.limit(20)
)
Features
| Area | Capabilities |
|---|---|
| Models & managers | SQLAlchemy declarative models, default/custom managers, model validation, serialization helpers, model-specific exceptions |
| QuerySets | Lazy immutable chaining, filter, exclude, get, first, count, exists, projections, ordering, pagination, async streaming |
| Lookups & logic | Q AND/OR/NOT, field and relationship traversal, NULL handling, nested JSON path lookups |
| Expressions | F, Value, Case/When, arithmetic and atomic SQL updates, typed SQL functions |
| Aggregations | Count, Sum, Avg, Min, Max, grouped annotations, HAVING, filtered and custom aggregates |
| Advanced SQL | Subquery, nested OuterRef, Exists, window functions, UNION / INTERSECT / EXCEPT, CTEs and recursive CTEs |
| Relationships | select_related, prefetch_related, relationship planning, correlated collection exclusions, related-row counts |
| Database writes | Create/update/delete, bulk operations, conflict handling, generated defaults, transaction-aware lifecycle hooks |
| PostgreSQL | JSONB and ARRAY operators, row-lock options and dialect-specific upserts |
| Extensibility | QuerySet-local custom lookups, trusted SQL function/aggregate factories, deferred fields, keyset pagination |
| Observability | Event-backed QueryProfiler, SQL timings, task-scoped captures, bounded history, heuristic N+1 detection |
| Integrations | Optional async-storage-kit / FileField, Alembic and FastAPI adapters |
The design is intentionally session-explicit, transaction-neutral and SQLAlchemy-native. QuerySet methods don't hide network connections, migrations, commits or rollbacks.
Installation
Requirements: CPython 3.11–3.15, SQLAlchemy 2.x and a compatible async database driver. Only SQLAlchemy is a mandatory runtime dependency.
# Install the source checkout with SQLite support
python -m pip install -e ".[sqlite]"
# Distribution extras, when a published build is available:
pip install "async-orm-kit[postgres]" # asyncpg
pip install "async-orm-kit[mysql]" # asyncmy
pip install "async-orm-kit[storage]" # async-storage-kit
pip install "async-orm-kit[migrations]" # Alembic
pip install "async-orm-kit[fastapi]" # FastAPI
SQLite uses aiosqlite; PostgreSQL uses asyncpg; MySQL uses asyncmy. The package does not start a database or configure a connection pool for you. See Installation and the database compatibility matrix.
Quick start
Define models with familiar SQLAlchemy mapping primitives:
from sqlalchemy import Boolean, ForeignKey, String
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlalchemy.orm import Mapped, mapped_column, relationship
from async_orm_kit import Model
engine = create_async_engine("sqlite+aiosqlite:///./app.db")
SessionFactory = async_sessionmaker(engine, expire_on_commit=False)
class Author(Model):
__tablename__ = "authors"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(120))
active: Mapped[bool] = mapped_column(Boolean, default=True)
posts: Mapped[list["Post"]] = relationship(back_populates="author")
class Post(Model):
__tablename__ = "posts"
id: Mapped[int] = mapped_column(primary_key=True)
author_id: Mapped[int] = mapped_column(ForeignKey("authors.id"))
title: Mapped[str] = mapped_column(String(255))
view_count: Mapped[int] = mapped_column(default=0)
author: Mapped[Author] = relationship(back_populates="posts")
In a disposable development database you can create tables through SQLAlchemy. For deployed applications, use reviewed Alembic migrations:
async with engine.begin() as connection:
await connection.run_sync(Model.metadata.create_all)
Write through the caller-owned transaction:
async with SessionFactory() as session:
async with session.begin():
author = await Author.objects.create(session, name="Ada")
await Post.objects.create(session, author_id=author.id, title="Async SQL")
Query with eager loading and normal AsyncSession ownership:
async with SessionFactory() as session:
authors = await (
Author.objects.filter(session, active=True)
.prefetch_related("posts")
.order_by("name")
)
for author in authors:
print(author.name, len(author.posts))
Important: Manager methods accept session as their first argument. A QuerySet remembers that session; chaining methods such as .filter(...) and .order_by(...) do not take it again. Alternatively, Author.objects.using(session) returns a session-bound manager proxy.
Querying and expressions
Boolean logic, aggregation and atomic updates
from async_orm_kit import Count, F, Q, Sum
# Boolean predicates and relationship traversal
query = Author.objects.filter(
session,
Q(name__icontains="ada") | Q(posts__title__icontains="sql"),
)
# Count related rows, preserving parents with no related records
ranked = await (
Author.objects.annotate(session, post_count=Count("posts"))
.filter(post_count__gte=1)
.order_by("-post_count")
)
# Updates use SQL-side expressions and still require an application transaction.
async with SessionFactory() as write_session:
async with write_session.begin():
await Post.objects.filter(write_session, id=42).update(
view_count=F("view_count") + 1
)
Use values(...).annotate(...) for grouped summaries and aggregate(...) for a terminal summary dictionary. Complex combinations of aggregate annotations and row predicates support Q OR/AND/NOT using correlated existence semantics. See Aggregations and Expressions.
Subqueries, set operations and JSON
from async_orm_kit import Exists, OuterRef
matching = Post.objects.filter(
session, author_id=OuterRef("id"), title__icontains="async"
)
authors = await Author.objects.filter(session, Exists(matching))
# Set operations on compatible projections and the same session
first = Author.objects.filter(session, active=True).values("id", "name")
second = Author.objects.filter(session, name__startswith="A").values("id", "name")
selected = await first.union(second).filter(name__icontains="a").order_by("id")
# For a model with a mapped SQLAlchemy JSON column named payload:
# events = await Event.objects.filter(session, payload__profile__age__gte=18)
The package also supports typed JSONPath, PostgreSQL JSONB/ARRAY expressions, CTEs, window functions and SQLAlchemy-composable subqueries. Dialect-specific operators are not silently emulated with text matching. See Advanced SQL, JSON & ARRAY and Correlated subqueries.
Pagination and streaming
base = Author.objects.order_by(session, "id")
first_page = await base.limit(25)
if first_page:
next_page = await base.seek_after(first_page[-1].id).limit(25)
async for author in base.stream():
process(author)
Keyset pagination requires a deterministic non-nullable ordering that includes the primary key. It accepts cursor values, not an opaque signed token. Keep the session open throughout streaming. See Pagination.
Transactions and integrations
You own the session and transaction
async with SessionFactory() as session:
async with session.begin():
await Author.objects.create(session, name="Grace")
Model and QuerySet mutation methods flush or execute SQL without committing or rolling back. A failed flush requires rollback before session reuse. Sessions must not be shared across concurrent tasks. Model post_save and post_delete observers run after flush, before commit; they aren't after-commit hooks.
Optional framework and storage integrations
- FastAPI: request-scoped
AsyncSessiondependency, explicit application transactions and safe exception responses. - Alembic: import model metadata for autogeneration; schema migrations remain application-owned.
- FileField / async-storage-kit: asynchronous upload, streaming download, metadata and backend adapters. Object storage is not transactionally coupled to the database.
- Observability: SQLAlchemy-event query timing, bounded execution traces and suspected N+1 patterns.
Documentation
The primary documentation is authored in reStructuredText and built with Sphinx. Start at docs/index.rst, or open the sections directly:
| Getting started | ORM fundamentals | Advanced queries | Operations |
|---|---|---|---|
| Install | Models | Expressions | Compatibility |
| Quick start | Managers | Aggregations | Troubleshooting |
| Design | QuerySets | Subqueries | Development |
| Sessions | Relationships | Set operations & CTEs | Release policy |
The complete API reference documents exported classes, methods, functions, exceptions, integrations and custom expression types. Supporting architecture decisions are in docs/architecture/ADR/.
Build locally
python -m pip install -e ".[docs]"
sphinx-build -b html -W --keep-going docs docs/_build/html
The repository also supplies .readthedocs.yaml for hosted Sphinx builds.
Development and quality
uv lock --check
uv sync --locked
uv run pytest -q
uv run ruff check src tests .github/scripts
uv run ruff format --check src tests .github/scripts
uv run mypy src/async_orm_kit
uv run bandit -c pyproject.toml -r src/async_orm_kit
The source includes offline SQL correctness tests and native-driver integration suites for aiosqlite, asyncpg and asyncmy. A skipped provider test is not passing compatibility evidence. Before the first stable release, the supported Python/OS matrix, live database tests, security/type checks, distributions and documentation build must all be verified. See Release readiness.
Contributing, security and licensing
Contributions and community conduct are governed by the existing detailed CONTRIBUTING.md and CODE_OF_CONDUCT.md documents. Report security issues using SECURITY.md, rather than public issues. Release notes are in CHANGELOG.md.
Async ORM Kit is licensed under the MIT License.
Metadata
Release files for async-orm-kit 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| async_orm_kit-1.0.0.tar.gz | 553.0 kB | Without provenance | Details for async_orm_kit-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| async_orm_kit-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for async_orm_kit-1.0.0-py3-none-any.whl |
Total release size: 768.1 kB
Release files / async_orm_kit-1.0.0.tar.gz
| Download URL | async_orm_kit-1.0.0.tar.gz |
|---|---|
| Size | 553.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
83fe2fbdf2e0c4b204a96ed1896d533e0f62dffb575acaa49d4dda24f2ea42bf
|
|
BLAKE2b-256 checksum How to use checksums |
d74dfb75ec2c9db298b47e8edcc5de689ae1a11ed60dda464b8308d442fe7b3b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / async_orm_kit-1.0.0-py3-none-any.whl
| Download URL | async_orm_kit-1.0.0-py3-none-any.whl |
|---|---|
| Size | 215.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
efde9daf2a485f63b4d45636cbd5b856d7e8dd35413ef16eb9266dc6101a7c7e
|
|
BLAKE2b-256 checksum How to use checksums |
b76ebd9d43b91f1bb3e4f76d4c490b1da24686582d42884256f69f2ca8b165d6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for async-orm-kit 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.