modo-db-macros
Procedural macros powering the modo-db entity and migration system.
This crate is an implementation detail of modo-db. Consume these macros through the
modo_db re-exports (modo_db::entity and modo_db::migration) — do not add
modo-db-macros as a direct dependency.
Macros
#[modo_db::entity(table = "...")]
Transforms an annotated struct into a fully-formed SeaORM entity module and registers it
with the inventory collector so modo_db::sync_and_migrate discovers it at startup.
Struct-level options
Place these as a second #[entity(...)] attribute on the struct itself.
| Option | Effect |
|---|---|
timestamps |
Injects created_at and updated_at: DateTime<Utc> columns; sets them in before_save. |
soft_delete |
Injects deleted_at: Option<DateTime<Utc>> and generates query helpers on the module. |
framework |
Marks the entity as framework-internal (hidden from user schema). |
index(columns = ["col1", "col2"]) |
Creates a composite index. Add unique inside for a unique index. |
Field-level options
Place these as #[entity(...)] on individual struct fields.
| Option | Effect |
|---|---|
primary_key |
Marks the field as the primary key. |
auto_increment = true|false |
Overrides SeaORM's default auto-increment behaviour. |
auto = "ulid"|"nanoid" |
Generates a ULID or NanoID before insert. Only valid on primary_key fields. |
unique |
Adds a unique constraint. |
indexed |
Creates a single-column index. |
column_type = "<type>" |
Overrides the inferred SeaORM column type string. |
default_value = <literal> |
Sets a column default value. |
default_expr = "<expr>" |
Sets a default SQL expression. |
belongs_to = "<Entity>" |
Declares a BelongsTo relation to the named entity. |
on_delete = "<action>" |
FK action on delete: Cascade, SetNull, Restrict, NoAction, SetDefault. |
on_update = "<action>" |
FK action on update. Same values as on_delete. |
has_many |
Declares a HasMany relation (field excluded from the model columns). |
has_one |
Declares a HasOne relation (field excluded from the model columns). |
via = "<JoinEntity>" |
Many-to-many via a join entity. Used with has_many or has_one. |
renamed_from = "<old>" |
Records a rename hint as a column comment. |
Generated module
For a struct named Foo, the macro emits a pub mod foo { ... } containing:
Model— the SeaORM model struct with all columnsActiveModel— SeaORM active modelEntity— SeaORM entity typeColumn— column enumRelation— relation enumActiveModelBehaviorimpl — runsbefore_savewhentimestampsorautois used- Soft-delete helpers (when
soft_deleteis set):find,find_by_id,with_deleted,only_deleted,soft_delete,restore,force_delete
Basic entity example
Entity with relations
Composite index
#[modo_db::migration(version = <u64>, description = "...")]
Registers an async SQL migration function. modo_db::sync_and_migrate runs all
registered migrations in ascending version order after schema sync.
The annotated function must be async fn(db: &impl ConnectionTrait) -> Result<(), DbErr>.
async
Integration with modo-db
Register entities and migrations simply by declaring them — no manual registration call is needed. Then at startup:
let db = connect.await?;
sync_and_migrate.await?;
sync_and_migrate discovers all #[modo_db::entity] and #[modo_db::migration]
declarations via inventory and applies them.