Skip to main content

Model

Derive Macro Model 

Source
#[derive(Model)]
{
    // Attributes available to this derive:
    #[model]
}
Expand description

Implements Model and OnTable<Table = Self> for a table, plus a typed Col constant per field.

use fastauth_core::plugin_prelude::*;

#[derive(Model, Clone)]
#[model(table = "api_key")]
pub struct ApiKey {
    #[model(id)]
    pub id: Id,
    #[model(references = User, on_delete = Cascade, index)]
    pub user_id: Id,
    #[model(unique, hidden)]
    pub key_hash: Hash,
    #[model(default = now)]
    pub created_at: OffsetDateTime,
}

let _: Where<ApiKey> = ApiKey::KEY_HASH.eq(Hash::sha256("ak_123"));

#[model(table = "..")] is required. Field attributes, all inside #[model(..)]:

  • id: the primary key.
  • references = Model: a foreign key to that model’s id column, with on_delete = Cascade | SetNull | Restrict (default Restrict).
  • unique, index, hidden (never serialized to clients).
  • default = ..: as for Fields.

Every field is a column; created_at and updated_at are not added for you. Encoding clones each field, so field types must be Clone.