Derive Macro atmosphere::Schema

source ·
#[derive(Schema)]
{
    // Attributes available to this derive:
    #[sql]
}
Expand description

A derive macro that processes structs to automatically generate schema-related code. It reads custom attributes and derives necessary traits and implementations for interacting with the database.

Entity attributes:

  • #[table(schema = "schema_name", name = "table_name")] - Set schema and table name

Field attributes:

  • #[sql(pk)] - Mark a column as primary key
  • #[sql(fk -> OtherModel)] - Mark a column as foreign key on OtherModel
  • #[sql(unique)] - Mark a column as unique
  • #[sql(timestamp = [create|update|delete])] - Mark a column as timestamp
  • #[sql(.., rename = "renamed_sql_col")] - Rename a column in the generated sql

Usage:

#[derive(Schema)]
#[table(schema = "public", name = "user")]
struct User {
    #[sql(pk)]
    id: i32,
    #[sql(unique)]
    username: String,
}

#[derive(Schema)]
#[table(schema = "public", name = "post")]
struct Post {
    #[sql(pk)]
    id: i32,
    #[sql(fk -> User, rename = "author_id")]
    author: i32,
}