Skip to main content

Crate torm_derive

Crate torm_derive 

Source
Expand description

Derive macros for the TORM library.

The main macro is Model, which generates the verbose boilerplate for implementing torm::orm::model::Model from a plain struct definition.

§Example

use torm::{Model, Timestamps};

#[derive(Debug, Clone, Model)]
#[model(table_name = "users", primary_key = "id")]
pub struct User {
    pub id: i64,
    pub name: String,
    pub created_at: Option<chrono::DateTime<chrono::Utc>>,
    pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
    pub timestamps: Timestamps,
}

§Field tags

In addition to the container-level #[model(table_name = "...", primary_key = "...")] attributes, each field may carry GORM-style tags that are recorded in the generated Model::schema method so that Database::auto_migrate can create the table and its indexes automatically:

  • #[model(primaryKey)] — marks the field as the primary key.
  • #[model(index)] or #[model(index = "idx_name")] — a plain index. Without a name it defaults to idx_<table>_<column>; fields sharing the same explicit name form a composite index.
  • #[model(uniqueIndex)] or #[model(uniqueIndex = "idx_name")] — a unique index (also implying a UNIQUE column constraint on a single column).

Derive Macros§

Model
Entry point for #[derive(Model)].