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 explicitdependenciesand are applied in topological order. (Note: there is no multiple-head detection ormergeprimitive yet; parallel branches that both create the next sequence number must be reconciled by hand.) - Admin router (FastAPI):
admin_routerexposes 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_rowsprovides 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.
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 matrx_orm-3.1.81.tar.gz.
File metadata
- Download URL: matrx_orm-3.1.81.tar.gz
- Upload date:
- Size: 814.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a93aaefb58a21f31620fda36636d18f107c0bd875a676afe1a17b062b29aaba8
|
|
| MD5 |
aad8554c4513dcf6107b77097f365b50
|
|
| BLAKE2b-256 |
a4c94ab2cb32805cb6d77789c68df110b9223f3b034e9b2a1f8bce42293dee3f
|
Provenance
The following attestation bundles were made for matrx_orm-3.1.81.tar.gz:
Publisher:
publish-package.yml on AI-Matrix-Engine/aidream
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
matrx_orm-3.1.81.tar.gz -
Subject digest:
a93aaefb58a21f31620fda36636d18f107c0bd875a676afe1a17b062b29aaba8 - Sigstore transparency entry: 2513861726
- Sigstore integration time:
-
Permalink:
AI-Matrix-Engine/aidream@3ec5769bf4cd3c43fd20031f64084d6ce8b59613 -
Branch / Tag:
refs/tags/matrx-orm/v3.1.81 - Owner: https://github.com/AI-Matrix-Engine
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-package.yml@3ec5769bf4cd3c43fd20031f64084d6ce8b59613 -
Trigger Event:
push
-
Statement type:
File details
Details for the file matrx_orm-3.1.81-py3-none-any.whl.
File metadata
- Download URL: matrx_orm-3.1.81-py3-none-any.whl
- Upload date:
- Size: 662.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b29a93bc7d60486c3685ab96c776c9d8f9a59487b36de18b90db84bcb9207eb6
|
|
| MD5 |
5ff15ab99d87fc6c30b745595c7219e2
|
|
| BLAKE2b-256 |
427e666df75c2ce11b95edf8b4b5229a5f59fb162d20744767017892582105c7
|
Provenance
The following attestation bundles were made for matrx_orm-3.1.81-py3-none-any.whl:
Publisher:
publish-package.yml on AI-Matrix-Engine/aidream
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
matrx_orm-3.1.81-py3-none-any.whl -
Subject digest:
b29a93bc7d60486c3685ab96c776c9d8f9a59487b36de18b90db84bcb9207eb6 - Sigstore transparency entry: 2513861797
- Sigstore integration time:
-
Permalink:
AI-Matrix-Engine/aidream@3ec5769bf4cd3c43fd20031f64084d6ce8b59613 -
Branch / Tag:
refs/tags/matrx-orm/v3.1.81 - Owner: https://github.com/AI-Matrix-Engine
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-package.yml@3ec5769bf4cd3c43fd20031f64084d6ce8b59613 -
Trigger Event:
push
-
Statement type: