Skip to main content

matrx-orm

Async-first PostgreSQL ORM for Python: typed models, an expressive query builder, bidirectional migrations with dependency-ordered history, schema introspection + code generation, and a mountable FastAPI admin router. Designed for applications that want ORM ergonomics without giving up raw-SQL control.

Install

pip install matrx-orm

Python 3.13+ required. Needs a PostgreSQL server (or Supabase / any Postgres-compatible backend). The only Matrx sibling it depends on is matrx-utils.

What's in the box

  • Core model layer: Model, BaseManager, BaseDTO, ModelView, model_registry, and 50+ field types (CharField, IntegerField, UUIDField, JSONField, ForeignKey, ManyToManyField, …).
  • Query layer: QueryBuilder, expressions (F, Q), window functions, CTEs, subqueries.
  • Migrations: MigrationDB, MigrationLoader, MigrationExecutor, makemigrations, migrate — migrations declare explicit dependencies and are applied in topological order. (Note: there is no multiple-head detection or merge primitive yet; parallel branches that both create the next sequence number must be reconciled by hand.)
  • Admin router (FastAPI): admin_router exposes a ready-to-mount set of read/write endpoints for every registered model.
  • API layer (optional [api] extra): APIServer, APIConfig, TokenAuth.
  • Adapters: AsyncPostgreSQLAdapter, SupabaseAdapter, PostgRESTClientAdapter.
  • Local artifact reads: read_local_sqlite_rows provides a validated, read-only projection boundary for SQLite files owned by external tools such as Chromium.
  • Signals: pre_create, post_create, pre_save, post_save, pre_delete, post_delete.
  • Schema builder (matrx_orm.schema_builder): code generation for Python + TypeScript type definitions from the live DB schema — useful for keeping a frontend's row types in sync.

Usage

Register a database project

matrx-orm supports multiple named database projects in a single process. Register each at startup, either with an explicit config or by reading env vars:

from matrx_orm import DatabaseProjectConfig, register_database, register_database_from_env

# Explicit config
register_database(DatabaseProjectConfig(
    name="main",
    host="localhost", port=5432,
    database="myapp", user="postgres", password="…",
    default_schema="public",
))

# Or env-driven with a custom prefix
register_database_from_env(name="analytics", env_prefix="ANALYTICS_DB_")

Declare a model and query

from matrx_orm import Model, CharField, UUIDField, TimestampField, DateTimeField

class User(Model):
    class Meta:
        table = "users"
        database = "main"

    id = UUIDField(primary_key=True)
    email = CharField(max_length=320, unique=True)
    display_name = CharField(max_length=120)
    created_at = DateTimeField(auto_now_add=True)

# Querying
user = await User.objects.get(email="alice@example.com")
active = await User.objects.filter(display_name__startswith="A").order_by("-created_at").all()

Migrations

# Generate migrations from the current model definitions
python -m matrx_orm.migrations.cli makemigrations

# Apply pending migrations
python -m matrx_orm.migrations.cli migrate

The migration system tracks which branch a migration originated on and refuses to let two branches create a conflicting sequence.

Mount the admin router

Rows with composite primary keys expose an opaque __matrx_row_id in list responses and a matching virtual primary-key column descriptor. Pass that value unchanged to row-detail, update, delete, and cache-eviction routes; the router decodes it into the complete composite key. This also gives generated read-only views collision-safe row navigation.

Generated views default to id only when they project it. Every other view must declare bounded, unique columns in generate[].output.view_primary_keys; generation fails instead of guessing a first column or embedding an unbounded projected row in an identifier.

from fastapi import FastAPI
from matrx_orm import admin_router

app = FastAPI()
app.include_router(admin_router, prefix="/admin")

Instantly exposes list/get/create/update/delete endpoints for every registered model. Wrap it in your app's auth middleware.

Standalone-friendliness

No hidden env-var reads outside config.py and the schema-builder CLI. All env-var access goes through register_database_from_env, which accepts a env_prefix and an env_var_overrides map. You can run matrx-orm with zero environment variables — just build a DatabaseProjectConfig yourself and call register_database.

Contributing

See CLAUDE.md for package-specific rules. MODEL_API.md documents the full Model/QueryBuilder API. This package lives in the aidream monorepo at github.com/AI-Matrix-Engine/aidream-current.

License

MIT.

Metadata

Release files for matrx-orm 3.1.263

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for matrx-orm 3.1.263
File Size Uploaded Provenance Actions
matrx_orm-3.1.263.tar.gz 1.2 MB With provenance Details for matrx_orm-3.1.263.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for matrx-orm 3.1.263
File Interpreter ABI Platform Actions
matrx_orm-3.1.263-py3-none-any.whl Python 3 none any Details for matrx_orm-3.1.263-py3-none-any.whl

Total release size: 2.1 MB

Release files / matrx_orm-3.1.263.tar.gz

Download URL matrx_orm-3.1.263.tar.gz
Size 1.2 MB
Tags Source
SHA-256 checksum
How to use checksums
f09ace6f27f3eafc41eb292c5df2999899c7f2dae3c57e8a9530f1e3471a0e3f
BLAKE2b-256 checksum
How to use checksums
60c2705093736be03899637c35f19c9282bf7d2fe0f9b5e53d55b968a261b54e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.

Transparency log

Release files / matrx_orm-3.1.263-py3-none-any.whl

Download URL matrx_orm-3.1.263-py3-none-any.whl
Size 881.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac0c6335d2da4ea6772d3be0ed5460e2bdb7ece466b7b57fccc766a400a0eae4
BLAKE2b-256 checksum
How to use checksums
819522e9e01575f2d7b14c9acbe064cfb4bcf974fb91ff8486d9cac5bcdb6003
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.

Transparency log

Security for matrx-orm 3.1.263

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for matrx_orm-3.1.263.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.

Transparency log

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.

Release history Release notifications | RSS feed

This release

3.1.263 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page