d2
Async-first PostgreSQL ORM for Python with a composable query builder and integrated migrations.
- Declarative schema — define tables and views as Python classes with type annotations
- Composable query builder — immutable, chainable SELECT/INSERT/UPDATE/DELETE builders
- Integrated migrations — schema diffing, codegen, apply/rollback, lint checks
- Async-first — built on asyncpg; results deserialised via msgspec
Documentation: https://okanakbulut.github.io/d2/ (source in docs/ — every example is a doctest, verified in CI)
Installation
Requires Python 3.14+ and PostgreSQL.
pip install d2
Quick example
from d2 import Table, Field, PrimaryKey, Unique, field, db
class User(Table):
id: PrimaryKey[int] = field(default=db.serial())
name: Field[str]
email: Unique[str]
q = (
User
.select(User.id, User.name)
.where(User.email.ilike("%@example.com"))
.order_by(User.id)
.limit(10)
)
q.build()
# ('SELECT "users"."id","users"."name" FROM "public"."users"
# WHERE "users"."email" ILIKE $1 ORDER BY "users"."id" ASC LIMIT 10',
# ('%@example.com',))
See Getting Started for configuration, connections, and running your first migration.
Development
The project is managed with uv:
uv sync # install dependencies
uv run pytest -m 'not integration' # unit tests + doc doctests (no database needed)
docker compose up -d # start PostgreSQL 17 for integration tests
uv run pytest # full suite
The documentation site lives in website/ (Astro + Starlight) and renders the markdown in docs/ directly:
cd website
npm install
npm run dev
License
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 d2-0.8.0.tar.gz.
File metadata
- Download URL: d2-0.8.0.tar.gz
- Upload date:
- Size: 165.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c6713e1b12999b0548e5d67e06651c0beb0015b6006b1477abdac66c8ac2c73
|
|
| MD5 |
3c3d76d142356597c21f66302873d6b8
|
|
| BLAKE2b-256 |
a714196f860cbb4fc26adfcf1fb27bb09ad2e2d1dfbf90b46b9a65fc96d0eed7
|
Provenance
The following attestation bundles were made for d2-0.8.0.tar.gz:
Publisher:
release.yml on okanakbulut/d2
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
d2-0.8.0.tar.gz -
Subject digest:
0c6713e1b12999b0548e5d67e06651c0beb0015b6006b1477abdac66c8ac2c73 - Sigstore transparency entry: 2180783058
- Sigstore integration time:
-
Permalink:
okanakbulut/d2@05109a38b55e890ee512d6bc8d7d5ae4c59f69bd -
Branch / Tag:
refs/tags/v0.8.0 - Owner: https://github.com/okanakbulut
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@05109a38b55e890ee512d6bc8d7d5ae4c59f69bd -
Trigger Event:
push
-
Statement type:
File details
Details for the file d2-0.8.0-py3-none-any.whl.
File metadata
- Download URL: d2-0.8.0-py3-none-any.whl
- Upload date:
- Size: 42.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ddc17e0e9952df035d689863d21f068cff618eaa465a6f2fb1b256000f58f563
|
|
| MD5 |
8c4e313ba7a38ec3e6e18826e79fb3f0
|
|
| BLAKE2b-256 |
4afce3f693825294fa6097d7e4c249121b03fd53d9a6f4b2967b7fd96f97d27a
|
Provenance
The following attestation bundles were made for d2-0.8.0-py3-none-any.whl:
Publisher:
release.yml on okanakbulut/d2
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
d2-0.8.0-py3-none-any.whl -
Subject digest:
ddc17e0e9952df035d689863d21f068cff618eaa465a6f2fb1b256000f58f563 - Sigstore transparency entry: 2180783251
- Sigstore integration time:
-
Permalink:
okanakbulut/d2@05109a38b55e890ee512d6bc8d7d5ae4c59f69bd -
Branch / Tag:
refs/tags/v0.8.0 - Owner: https://github.com/okanakbulut
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@05109a38b55e890ee512d6bc8d7d5ae4c59f69bd -
Trigger Event:
push
-
Statement type: