Skip to main content

Model

Trait Model 

Source
pub trait Model:
    Sized
    + Send
    + Sync
    + 'static {
    // Required methods
    fn table_name() -> &'static str;
    fn columns() -> Vec<ColumnDef>;
    fn to_row(&self) -> Result<Row>;
    fn from_row(row: Row) -> Result<Self>;
    fn pk_value(&self) -> Result<Value>;

    // Provided method
    fn primary_key() -> &'static str { ... }
}
Expand description

Core trait that every ORM model must implement.

You should never implement this by hand. Apply #[derive(Model)] to your struct and the macro generates all methods automatically from the field types and #[rusticx(...)] attributes.

§Example

#[derive(Debug, Serialize, Deserialize, Model)]
#[rusticx(table = "orders")]
pub struct Order {
    #[rusticx(primary_key)]
    pub id: uuid::Uuid,
    pub user_id: uuid::Uuid,
    pub total: f64,
    pub placed_at: chrono::DateTime<chrono::Utc>,
}

Required Methods§

Source

fn table_name() -> &'static str

Table name (SQL) or collection name (MongoDB).

Defaults to the struct name converted to snake_case and pluralised (Userusers). Override with #[rusticx(table = "my_table")].

Source

fn columns() -> Vec<ColumnDef>

Full column schema — used by migrate() to emit CREATE TABLE DDL.

Source

fn to_row(&self) -> Result<Row>

Serialize all persisted fields into a flat Row map.

Called by Repository::insert and Repository::save before handing data to the backend adapter.

Source

fn from_row(row: Row) -> Result<Self>

Deserialize a Row map (returned by the backend) into Self.

Called by every find* method after fetching raw rows.

Source

fn pk_value(&self) -> Result<Value>

Extract the primary key value from this instance.

Used by Repository::save to decide insert vs. update and by Repository::delete_by_id.

Provided Methods§

Source

fn primary_key() -> &'static str

Primary key column name (default: "id").

Override at struct level: #[rusticx(primary_key = "uuid")].

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§