Models & Schemas

Define a model, types, constraints, relations, migrations.

● ● ●
use runique::prelude::*;

model! {
    Product,
    table: "product",
    pk: id => Pk,
    {
        name:        text     [max_length: 255, required],
        slug:        slug     [max_length: 255, required, unique],
        description: textarea [nullable],
        price:       decimal  [required],
        stock:       int      [default: 0],
        active:      bool     [default: true],
        category_id: Pk       [required],
        created_at:  datetime [auto_now],
    },
    relations: {
        belongs_to: category via category_id [cascade],
    }
}
[required]            mandatory form field (the column is NOT NULL anyway)
[nullable]            the column accepts NULL (Option<T> on the model)
[unique]              UNIQUE constraint
[max_length: N]       VARCHAR(N) — also bounds the form field
[default: value]      SQL default value
[auto_now]            set on insert by the entity
[auto_now_update]     set on every save by the entity

// Foreign keys live in relations:, never on the field
relations: {
    belongs_to: category via category_id [cascade],
    belongs_to: eihwaz_users via owner_id [set_null],   // owner_id must be nullable
}
model! {
    Article,
    table: "article",
    pk: id => Pk,
    { title: text [required], }
}

// Enables Article::objects.all(), .filter(), .get(), etc.
impl_objects!(Entity);