Initialize the database connection pool. Must be called before any database operations.
await connect("sqlite::memory:")
await connect("postgres://user:pass@localhost/mydb")Async context manager that creates a session, yields it, commits on success, and rolls back on exception.
async with transaction() as tx:
user = await User.create(tx, username="alice")Execute raw SQL. Requires allow-raw-sql Cargo feature. Raises RuntimeError if the feature is not enabled.
rows = await execute_raw("SELECT * FROM users")Configure structured query logging. Installs telemetry bridge if not already installed. Logs queries slower than slow_query_ms as warnings.
Base class for all model definitions. See Defining models.
| Method | Description |
|---|---|
query() |
Create a QueryBuilder for this model |
create(**kwargs) |
Insert a new record |
create_many(items) |
Bulk insert records |
find_one(**filters) |
Find by primary key or filter |
delete() |
Delete this instance |
delete_many(**filters) |
Delete matching records |
to_dict() |
Serialize to dict |
to_json() |
Serialize to JSON string |
to_xml() |
Serialize to XML string |
get_field_definitions() |
Return field name → type mapping |
| Exception | Base class | When raised |
|---|---|---|
BridgeError |
Exception |
Base for all Bridge errors |
ConnectionError |
BridgeError |
Database connection failures |
QueryError |
BridgeError |
Query execution failures |
NotFoundError |
BridgeError, KeyError |
Resource not found |
ConstraintError |
BridgeError |
Constraint violation |
ValidationError |
BridgeError, ValueError |
Data validation failure |
DatabaseError |
BridgeError |
Database engine error |
ProjectionError |
BridgeError, AttributeError |
Accessing unselected field |
CompositeKeyError |
BridgeError |
Composite key operation failure |
HookAbortedError |
BridgeError |
Hook cancelled operation |
SessionExpiredError |
BridgeError |
Session exceeded lifetime |
One-to-many relationship descriptor. Requires target_model (class or string name) and foreign_key column name.
Many-to-many relationship descriptor. Requires junction table name and the two foreign key column names.
Self-referential relationship descriptor. Requires parent_key column name.
Unit of Work session with identity map and dirty tracking.
| Method | Description |
|---|---|
commit() |
Flush dirty entities and commit transaction |
rollback() |
Rollback transaction |
flush() |
Compute diffs and execute UPDATEs |
get_entity(table, pk_values) |
Look up from identity map |
set_entity(model_class, pk_values, entity) |
Store in identity map |
clear() |
Clear identity map |
get_stats() |
Returns cache metrics |
Create a new Session. Connects to the database pool initialized by connect().
Model registry mapping table names to model classes. Methods: register(), unregister(), get(), models(), clear(). Supports dict-like access (__getitem__, __setitem__, __contains__, __iter__).
Create a new BaseModel subclass with its own registry. Useful for plugin systems or multi-tenant setups.
Context manager that temporarily swaps a base class's registry. All new model declarations within the context use the provided registry.
Fluent query builder. See Query builder reference.
| Method | Returns |
|---|---|
select(*fields) |
QueryBuilder |
filter(**kwargs) |
QueryBuilder |
raw_filter(column, raw_expr) |
QueryBuilder |
limit(count) |
QueryBuilder |
with_relation(name, strategy) |
QueryBuilder |
prefetch_related(*names) |
QueryBuilder |
fetch(tx) |
List[BaseModel] |
first(tx) |
Optional[BaseModel] |
fetch_lazy(tx) |
AsyncIterator[BaseModel] |
fetch_arrow(tx) |
List[LazyModelProxy] |
Enum: JOINED_FOR_TO_ONE, SELECT_IN_FOR_TO_MANY.
Wrapper for raw SQL expressions with bound parameters.
See Migration creation.
| Method | Description |
|---|---|
generate_migration(description) |
Generate UP/DOWN SQL from model schema diff |
load_snapshot() |
Load saved schema snapshot |
save_snapshot(snapshot) |
Save schema snapshot |
Introspect a database table and return a Python model class definition as a string.
Generate a FastAPI router with CRUD endpoints for the model.
FastAPI-based admin panel with JWT auth, CSRF protection, and rate limiting.