Skip to main content

Module repository

Module repository 

Source
Available on crate features 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 of 0 (or NULL) inserts and the database picks the key; any other key updates that row.
  • Any other key: save looks the key up (soft-deleted rows included) and updates the row when it is there, inserts it otherwise. A Uuid::NIL key (Uuid::default()) is replaced by a fresh Uuid::now_v7 and inserted; an empty String key 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::insert always 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§

ModelRepository
Repository tied to a model type and a pool.

Traits§

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.