Trait Model

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

Show 23 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 { ... } async fn find( pool: &Pool<Postgres>, id: Self::PrimaryKey, ) -> ModelResult<Option<Self>> where Self: Sized { ... } async fn find_or_fail( pool: &Pool<Postgres>, id: Self::PrimaryKey, ) -> ModelResult<Self> where Self: Sized { ... } async fn create(pool: &Pool<Postgres>, model: Self) -> ModelResult<Self> where Self: Sized { ... } async fn update(&mut self, pool: &Pool<Postgres>) -> ModelResult<()> { ... } async fn delete(self, pool: &Pool<Postgres>) -> ModelResult<()> { ... } fn query() -> QueryBuilder<Self> where Self: Sized { ... } async fn all(pool: &Pool<Postgres>) -> ModelResult<Vec<Self>> where Self: Sized { ... } async fn count(pool: &Pool<Postgres>) -> ModelResult<i64> where Self: Sized { ... }
}
Expand description

Trait for database models with standard ORM operations

Required Associated Types§

Source

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

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

async fn find( pool: &Pool<Postgres>, id: Self::PrimaryKey, ) -> ModelResult<Option<Self>>
where Self: Sized,

Find a model by its primary key

Source

async fn find_or_fail( pool: &Pool<Postgres>, id: Self::PrimaryKey, ) -> ModelResult<Self>
where Self: Sized,

Find a model by its primary key or return an error if not found

Source

async fn create(pool: &Pool<Postgres>, model: Self) -> ModelResult<Self>
where Self: Sized,

Create a new model instance in the database (placeholder implementation)

Source

async fn update(&mut self, pool: &Pool<Postgres>) -> ModelResult<()>

Update this model instance in the database

Source

async fn delete(self, pool: &Pool<Postgres>) -> ModelResult<()>

Delete this model instance from the database

Source

fn query() -> QueryBuilder<Self>
where Self: Sized,

Get a query builder for this model

Source

async fn all(pool: &Pool<Postgres>) -> ModelResult<Vec<Self>>
where Self: Sized,

Get all records for this model

Source

async fn count(pool: &Pool<Postgres>) -> ModelResult<i64>
where Self: Sized,

Count all records for this model

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§