model-mysql or model-postgres or model-sqlite only.Expand description
Async Repository trait and ModelRepository implementation.
§Primary keys (18.30.0)
ModelRepository<T, ID> implements Repository<T, ID> for ID =
i64, i32, String and Uuid (the PrimaryKey types). What
save does depends on the key:
#[primary_key(auto_increment)]: a key of0(orNULL) inserts and the database picks the key; any other key updates that row.- Any other key:
savelooks the key up (soft-deleted rows included) and updates the row when it is there, inserts it otherwise. AUuid::NILkey (Uuid::default()) is replaced by a freshUuid::now_v7and inserted; an emptyStringkey is an error. The look-up and the write are two statements: two processes saving the same new key at once can both decide to insert, and the second gets the database’s unique-key error.ModelRepository::insertalways inserts.
§Conventions
The columns a model names through Model::created_at_column,
Model::updated_at_column, Model::soft_delete_column and
Model::version_column (#[timestamps], #[soft_delete],
#[version] on #[derive(Model)]) are written by the repository, never
from the entity’s fields: on insert created_at = updated_at = now,
deleted_at = NULL, version = 1; on update updated_at = now and
version = version + 1, only while the row still has the entity’s
version — otherwise save fails with
DbErrorKind::StaleVersion and
changes nothing. With a soft-delete column delete_by_id sets it,
find_by_id / find_all / count / exists_by_id skip such rows, and
ModelRepository::restore / ModelRepository::force_delete_by_id
undo or finish it. Times are UTC (Timestamp); see
timestamp for the column types per backend.
Structs§
- Model
Repository - Repository tied to a model type and a pool.
Traits§
- 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.