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§
Required Methods§
Sourcefn table_name() -> &'static str
fn table_name() -> &'static str
Table name for this model
Sourcefn primary_key(&self) -> Option<Self::PrimaryKey>
fn primary_key(&self) -> Option<Self::PrimaryKey>
Get the primary key value for this model instance
Sourcefn set_primary_key(&mut self, key: Self::PrimaryKey)
fn set_primary_key(&mut self, key: Self::PrimaryKey)
Set the primary key value for this model instance
Sourcefn from_row(row: &PgRow) -> ModelResult<Self>where
Self: Sized,
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
Provided Methods§
Sourcefn primary_key_name() -> &'static str
fn primary_key_name() -> &'static str
Primary key field name(s)
Sourcefn uses_timestamps() -> bool
fn uses_timestamps() -> bool
Check if this model uses timestamps (created_at, updated_at)
Sourcefn uses_soft_deletes() -> bool
fn uses_soft_deletes() -> bool
Check if this model supports soft deletes
Sourcefn created_at(&self) -> Option<DateTime<Utc>>
fn created_at(&self) -> Option<DateTime<Utc>>
Get created_at timestamp if available
Sourcefn set_created_at(&mut self, _timestamp: DateTime<Utc>)
fn set_created_at(&mut self, _timestamp: DateTime<Utc>)
Set created_at timestamp
Sourcefn updated_at(&self) -> Option<DateTime<Utc>>
fn updated_at(&self) -> Option<DateTime<Utc>>
Get updated_at timestamp if available
Sourcefn set_updated_at(&mut self, _timestamp: DateTime<Utc>)
fn set_updated_at(&mut self, _timestamp: DateTime<Utc>)
Set updated_at timestamp
Sourcefn deleted_at(&self) -> Option<DateTime<Utc>>
fn deleted_at(&self) -> Option<DateTime<Utc>>
Get deleted_at timestamp if available (for soft deletes)
Sourcefn set_deleted_at(&mut self, _timestamp: Option<DateTime<Utc>>)
fn set_deleted_at(&mut self, _timestamp: Option<DateTime<Utc>>)
Set deleted_at timestamp (for soft deletes)
Sourcefn is_soft_deleted(&self) -> bool
fn is_soft_deleted(&self) -> bool
Check if this model instance is soft deleted
Sourceasync fn find(
pool: &Pool<Postgres>,
id: Self::PrimaryKey,
) -> ModelResult<Option<Self>>where
Self: Sized,
async fn find(
pool: &Pool<Postgres>,
id: Self::PrimaryKey,
) -> ModelResult<Option<Self>>where
Self: Sized,
Find a model by its primary key
Sourceasync fn find_or_fail(
pool: &Pool<Postgres>,
id: Self::PrimaryKey,
) -> ModelResult<Self>where
Self: Sized,
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
Sourceasync fn create(pool: &Pool<Postgres>, model: Self) -> ModelResult<Self>where
Self: Sized,
async fn create(pool: &Pool<Postgres>, model: Self) -> ModelResult<Self>where
Self: Sized,
Create a new model instance in the database (placeholder implementation)
Sourceasync fn update(&mut self, pool: &Pool<Postgres>) -> ModelResult<()>
async fn update(&mut self, pool: &Pool<Postgres>) -> ModelResult<()>
Update this model instance in the database
Sourceasync fn delete(self, pool: &Pool<Postgres>) -> ModelResult<()>
async fn delete(self, pool: &Pool<Postgres>) -> ModelResult<()>
Delete this model instance from the database
Sourcefn query() -> QueryBuilder<Self>where
Self: Sized,
fn query() -> QueryBuilder<Self>where
Self: Sized,
Get a query builder 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.