Skip to main content

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

MIT

Download files

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

Source Distribution

d2-0.8.0.tar.gz (165.6 kB view details)

Uploaded Source

Built Distribution

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

d2-0.8.0-py3-none-any.whl (42.5 kB view details)

Uploaded Python 3

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

Hashes for d2-0.8.0.tar.gz
Algorithm Hash digest
SHA256 0c6713e1b12999b0548e5d67e06651c0beb0015b6006b1477abdac66c8ac2c73
MD5 3c3d76d142356597c21f66302873d6b8
BLAKE2b-256 a714196f860cbb4fc26adfcf1fb27bb09ad2e2d1dfbf90b46b9a65fc96d0eed7

See more details on using hashes here.

Provenance

The following attestation bundles were made for d2-0.8.0.tar.gz:

Publisher: release.yml on okanakbulut/d2

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

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

Hashes for d2-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ddc17e0e9952df035d689863d21f068cff618eaa465a6f2fb1b256000f58f563
MD5 8c4e313ba7a38ec3e6e18826e79fb3f0
BLAKE2b-256 4afce3f693825294fa6097d7e4c249121b03fd53d9a6f4b2967b7fd96f97d27a

See more details on using hashes here.

Provenance

The following attestation bundles were made for d2-0.8.0-py3-none-any.whl:

Publisher: release.yml on okanakbulut/d2

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