Available on crate features
model-mysql or model-postgres or model-sqlite only.Expand description
§Model — Async Database Access Layer
An async JPA/Hibernate-style ORM backed by sqlx. Provides:
#[derive(Model)]proc-macro for struct-to-table mappingRepository<T, i64>trait for zero-boilerplate async CRUDQueryBuilder<T>for fluent async filteringDbPoolfor connection management (wrapssqlx::Pool)- Async migration runner
- Relationship helpers (
HasMany,HasOne,BelongsTo) - Primary keys of type
i64,i32,StringorUuid(18.30.0) - Conventions
saveanddeletemaintain (18.30.0):#[timestamps](created_at/updated_at,Timestamp),#[soft_delete](deleted_at; deleted rows are left out of queries unless asked for) and#[version](optimistic locking: saving a stale copy fails withDbErrorKind::StaleVersioninstead of overwriting) - Eager loading for a whole result set (
QueryBuilder::with,Relation), many-to-many links (BelongsToMany), scopes (QueryBuilder::scope), seeds written in Rust (Seeders), read replicas (DbPool::with_replicas) and the schema as JSON (DbPool::schema) (18.30.0)
Enable one or more backend features — model-sqlite, model-postgres,
and model-mysql are not mutually exclusive, so a single binary can hold
pools to more than one backend at once (see Backend):
# SQLite
rust-web-server = { features = ["model-sqlite"] }
# PostgreSQL
rust-web-server = { features = ["model-postgres"] }
# MySQL
rust-web-server = { features = ["model-mysql"] }
# Or more than one at once
rust-web-server = { features = ["model-sqlite", "model-postgres"] }Modules§
- backend
Backend— which compiled-in database driver asuper::DbPooluses.- connection
- Database connection configuration.
- db_
schema - The database’s schema as data (18.30.0):
DbPool::schemareads every table of the connected schema — columns (type, nullability, default), primary key, indexes and foreign keys — from the backend’s catalog (sqlite_masterand thepragma_*functions on SQLite,information_schemaandpg_catalogon PostgreSQL,information_schemaon MySQL), andDbSchema::to_jsonwrites it for tools:rws db schema --jsonand the dev MCP server’sdb_schema. - eager
- Eager loading (18.30.0): a
Relationnames how one model reaches another;QueryBuilder::withloads it for a whole result set in one more query per relation (WHERE fk IN (…)), not one per row. - migration
- Async migration runner.
- pool
- Async connection pool —
DbPoolandDbTransaction. - query
- Async fluent
QueryBuilder<T>for constructing SQL queries. - relation
- Async relationship helpers:
HasMany,HasOne,BelongsTo, andBelongsToMany(18.30.0) through a join table. - repository
- Async
Repositorytrait andModelRepositoryimplementation. - seed
- Seeds written in Rust (18.30.0), run after the
seeds/*.sqlfiles byDbPool::seed_with— and by an app’s owndb seedcommand, built onrust_web_server::db_cli::run_with_seeders. - timestamp
Timestamp: a UTC instant with microsecond precision, the type of thecreated_at/updated_at/deleted_atcolumns the repository maintains (18.30.0).- uuid
Uuid: a 128-bit RFC 9562 UUID for primary keys (18.30.0).
Structs§
- Belongs
To - Represents the inverse side of a relationship (child → owner).
- Belongs
ToMany - Many-to-many through a join table (18.30.0): the rows of
Rone owner is linked to, and the links themselves —attach,detach,sync. - Column
Schema - One column.
- Cursor
Page - A single page of cursor (keyset) paginated results.
- DbConfig
- Database connection configuration.
- DbError
- Error type returned by all model/DB operations.
- DbSchema
- Every table of the connected schema, by name.
- DbTls
- How a PostgreSQL or MySQL connection uses TLS (ignored for SQLite).
- Eager
Query - A
QueryBuilderwith relations to load: fromQueryBuilder::with. Itsfetch_*answerLoadedmodels. - Foreign
KeySchema - One foreign key.
- HasMany
- Represents a one-to-many relationship from an owner to a collection of
T. - HasOne
- Represents a one-to-one relationship (owner → child).
- Index
Schema - One index.
- Loaded
- A model with the relations
QueryBuilder::withloaded for it. Derefs to the model. - Migration
Status - Status of a single migration file.
- Model
Info - What a
Modeltype maps to, read from its trait implementation: the type, its table, its columns and primary key (18.28.0). Rust can’t list the#[derive(Model)]types of a program at run time, so whatever reports models (the dev MCP server’smodelstool,DevTools::model::<T>()) is given each type once. - Model
Repository - Repository tied to a model type and a pool.
- Model
Row - Backend-independent database row.
- Page
- A single page of offset-paginated results (
LIMIT/OFFSET-style). - Query
Builder - Fluent async query builder for
SELECT,COUNT,DELETE, andUPDATE. - Relation
- How a model
Oreaches related rows ofR: forQueryBuilder::with, and to load the rows of one owner (load) or of many (load_for). Usually returned by an associated function of the owner, so call sites name it: - Replicas
- The pools of a
DbPool::Replicated: one primary, the replicas reads go to, and the round-robin position (18.30.0). - Seeders
- Seeders, run in the order added. Cheap to clone.
- Sync
Changes - What
BelongsToMany::syncchanged: the related keys it linked and unlinked. - Table
Schema - One table.
- Timestamp
- A UTC instant, microseconds since the Unix epoch.
- Uuid
- A UUID, kept as its 16 bytes.
Enums§
- Backend
- Selects which compiled-in database backend a
super::DbPooltalks to. - DbError
Kind - What a
DbErroris about, for errors a caller handles differently from the rest (18.30.0). - DbPool
- An async connection pool backed by sqlx.
- DbSsl
Mode - TLS modes, named as libpq’s
sslmode. - DbTransaction
- An in-progress database transaction.
- Order
- Sort direction for
order_by. - Relation
Kind - What a
Relationis. - Value
- Backend-independent SQL value representation.
Traits§
- From
Column - Convert a
Valueinto a Rust type. - Model
- Trait generated by
#[derive(Model)]. Maps a Rust struct to a database table. - Primary
Key - A type
ModelRepositorycan key rows by:i64,i32,StringorUuid(18.30.0). Sealed — these are the typesRepositoryis implemented for. - Repository
- Async CRUD operations for a model type.
- Seeder
- Rows put in a database by Rust code; see the module docs.
Seeders::addmakes one from a closure. - ToColumn
- Convert a Rust value into a
Value.