Model

Trait Model 

Source
pub trait Model:
    Send
    + Sync
    + Debug
    + Serialize
    + for<'de> Deserialize<'de> {
    type PrimaryKey: Clone + Send + Sync + Debug + Display + Default;

Show 16 methods // Required methods fn table_name() -> &'static str; fn primary_key(&self) -> Option<Self::PrimaryKey>; fn set_primary_key(&mut self, key: Self::PrimaryKey); fn from_row(row: &PgRow) -> ModelResult<Self> where Self: Sized; fn to_fields(&self) -> HashMap<String, Value>; // Provided methods fn primary_key_name() -> &'static str { ... } fn uses_timestamps() -> bool { ... } fn uses_soft_deletes() -> bool { ... } fn created_at(&self) -> Option<DateTime<Utc>> { ... } fn set_created_at(&mut self, _timestamp: DateTime<Utc>) { ... } fn updated_at(&self) -> Option<DateTime<Utc>> { ... } fn set_updated_at(&mut self, _timestamp: DateTime<Utc>) { ... } fn deleted_at(&self) -> Option<DateTime<Utc>> { ... } fn set_deleted_at(&mut self, _timestamp: Option<DateTime<Utc>>) { ... } fn is_soft_deleted(&self) -> bool { ... } fn from_database_row(_row: &dyn DatabaseRow) -> ModelResult<Self> where Self: Sized { ... }
}
Expand description

Core trait for database models with standard ORM operations

Required Associated Types§

Source

type PrimaryKey: Clone + Send + Sync + Debug + Display + Default

The type used for this model’s primary key

Required Methods§

Source

fn table_name() -> &'static str

Table name for this model

Source

fn primary_key(&self) -> Option<Self::PrimaryKey>

Get the primary key value for this model instance

Source

fn set_primary_key(&mut self, key: Self::PrimaryKey)

Set the primary key value for this model instance

Source

fn from_row(row: &PgRow) -> ModelResult<Self>
where Self: Sized,

Create a model instance from a database row This will be automatically implemented by the derive macro

Source

fn to_fields(&self) -> HashMap<String, Value>

Convert model to field-value pairs for database operations This will be automatically implemented by the derive macro

Provided Methods§

Source

fn primary_key_name() -> &'static str

Primary key field name(s)

Source

fn uses_timestamps() -> bool

Check if this model uses timestamps (created_at, updated_at)

Source

fn uses_soft_deletes() -> bool

Check if this model supports soft deletes

Source

fn created_at(&self) -> Option<DateTime<Utc>>

Get created_at timestamp if available

Source

fn set_created_at(&mut self, _timestamp: DateTime<Utc>)

Set created_at timestamp

Source

fn updated_at(&self) -> Option<DateTime<Utc>>

Get updated_at timestamp if available

Source

fn set_updated_at(&mut self, _timestamp: DateTime<Utc>)

Set updated_at timestamp

Source

fn deleted_at(&self) -> Option<DateTime<Utc>>

Get deleted_at timestamp if available (for soft deletes)

Source

fn set_deleted_at(&mut self, _timestamp: Option<DateTime<Utc>>)

Set deleted_at timestamp (for soft deletes)

Source

fn is_soft_deleted(&self) -> bool

Check if this model instance is soft deleted

Source

fn from_database_row(_row: &dyn DatabaseRow) -> ModelResult<Self>
where Self: Sized,

Create a model instance from a database row (abstracted version) This will replace from_row in the future

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§