Skip to main content

Module model

Module model 

Source
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 mapping
  • Repository<T, i64> trait for zero-boilerplate async CRUD
  • QueryBuilder<T> for fluent async filtering
  • DbPool for connection management (wraps sqlx::Pool)
  • Async migration runner
  • Relationship helpers (HasMany, HasOne, BelongsTo)
  • Primary keys of type i64, i32, String or Uuid (18.30.0)
  • Conventions save and delete maintain (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 with DbErrorKind::StaleVersion instead of overwriting)

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 a super::DbPool uses.
connection
Database connection configuration.
migration
Async migration runner.
pool
Async connection pool — DbPool and DbTransaction.
query
Async fluent QueryBuilder<T> for constructing SQL queries.
relation
Async relationship helpers: HasMany, HasOne, BelongsTo.
repository
Async Repository trait and ModelRepository implementation.
timestamp
Timestamp: a UTC instant with microsecond precision, the type of the created_at / updated_at / deleted_at columns the repository maintains (18.30.0).
uuid
Uuid: a 128-bit RFC 9562 UUID for primary keys (18.30.0).

Structs§

BelongsTo
Represents the inverse side of a relationship (child → owner).
CursorPage
A single page of cursor (keyset) paginated results.
DbConfig
Database connection configuration.
DbError
Error type returned by all model/DB operations.
DbTls
How a PostgreSQL or MySQL connection uses TLS (ignored for SQLite).
HasMany
Represents a one-to-many relationship from an owner to a collection of T.
HasOne
Represents a one-to-one relationship (owner → child).
MigrationStatus
Status of a single migration file.
ModelInfo
What a Model type 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’s models tool, DevTools::model::<T>()) is given each type once.
ModelRepository
Repository tied to a model type and a pool.
ModelRow
Backend-independent database row.
Page
A single page of offset-paginated results (LIMIT/OFFSET-style).
QueryBuilder
Fluent async query builder for SELECT, COUNT, DELETE, and UPDATE.
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::DbPool talks to.
DbErrorKind
What a DbError is about, for errors a caller handles differently from the rest (18.30.0).
DbPool
An async connection pool backed by sqlx.
DbSslMode
TLS modes, named as libpq’s sslmode.
DbTransaction
An in-progress database transaction.
Order
Sort direction for order_by.
Value
Backend-independent SQL value representation.

Traits§

FromColumn
Convert a Value into a Rust type.
Model
Trait generated by #[derive(Model)]. Maps a Rust struct to a database table.
PrimaryKey
A type ModelRepository can key rows by: i64, i32, String or Uuid (18.30.0). Sealed — these are the types Repository is implemented for.
Repository
Async CRUD operations for a model type.
ToColumn
Convert a Rust value into a Value.