pub trait Model:
Send
+ Sync
+ Debug
+ Serialize
+ for<'de> Deserialize<'de> {
type PrimaryKey: Clone + Send + Sync + Debug + Display + Default;
Show 27 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 bind_json_value<'a>(
query: Query<'a, Postgres, PgArguments>,
value: &Value,
) -> ModelResult<Query<'a, Postgres, PgArguments>> { ... }
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 { ... }
async fn where_field<V>(
pool: &Pool<Postgres>,
field: &str,
value: V,
) -> ModelResult<Vec<Self>>
where Self: Sized,
for<'q> V: Send + Sync + 'static + Encode<'q, Postgres> + Type<Postgres> { ... }
async fn first_where<V>(
pool: &Pool<Postgres>,
field: &str,
value: V,
) -> ModelResult<Option<Self>>
where Self: Sized,
for<'q> V: Send + Sync + 'static + Encode<'q, Postgres> + Type<Postgres> { ... }
fn from_database_row(row: &dyn DatabaseRow) -> ModelResult<Self>
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 with field-based insertion
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 with field-based updates
Sourceasync fn delete(self, pool: &Pool<Postgres>) -> ModelResult<()>
async fn delete(self, pool: &Pool<Postgres>) -> ModelResult<()>
Delete this model instance from the database
Sourcefn bind_json_value<'a>(
query: Query<'a, Postgres, PgArguments>,
value: &Value,
) -> ModelResult<Query<'a, Postgres, PgArguments>>
fn bind_json_value<'a>( query: Query<'a, Postgres, PgArguments>, value: &Value, ) -> ModelResult<Query<'a, Postgres, PgArguments>>
Helper method to bind JSON values to SQL queries
Sourcefn query() -> QueryBuilder<Self>where
Self: Sized,
fn query() -> QueryBuilder<Self>where
Self: Sized,
Get a query builder for this model
Sourceasync fn all(pool: &Pool<Postgres>) -> ModelResult<Vec<Self>>where
Self: Sized,
async fn all(pool: &Pool<Postgres>) -> ModelResult<Vec<Self>>where
Self: Sized,
Get all records for this model with proper SQL execution
Sourceasync fn count(pool: &Pool<Postgres>) -> ModelResult<i64>where
Self: Sized,
async fn count(pool: &Pool<Postgres>) -> ModelResult<i64>where
Self: Sized,
Count all records for this model with proper SQL execution
Sourceasync fn where_field<V>(
pool: &Pool<Postgres>,
field: &str,
value: V,
) -> ModelResult<Vec<Self>>
async fn where_field<V>( pool: &Pool<Postgres>, field: &str, value: V, ) -> ModelResult<Vec<Self>>
Find models by a specific field value with proper parameter binding
Sourceasync fn first_where<V>(
pool: &Pool<Postgres>,
field: &str,
value: V,
) -> ModelResult<Option<Self>>
async fn first_where<V>( pool: &Pool<Postgres>, field: &str, value: V, ) -> ModelResult<Option<Self>>
Find the first model by a specific field value
Sourcefn from_database_row(row: &dyn DatabaseRow) -> ModelResult<Self>where
Self: Sized,
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.