use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::Debug;
use crate::backends::DatabaseRow;
use crate::error::ModelResult;
pub trait Model: Send + Sync + Debug + Serialize + for<'de> Deserialize<'de> {
type PrimaryKey: Clone + Send + Sync + Debug + std::fmt::Display + Default;
fn table_name() -> &'static str;
fn primary_key_name() -> &'static str {
"id"
}
fn primary_key(&self) -> Option<Self::PrimaryKey>;
fn set_primary_key(&mut self, key: Self::PrimaryKey);
fn uses_timestamps() -> bool {
false
}
fn uses_soft_deletes() -> bool {
false
}
fn created_at(&self) -> Option<DateTime<Utc>> {
None
}
fn set_created_at(&mut self, _timestamp: DateTime<Utc>) {}
fn updated_at(&self) -> Option<DateTime<Utc>> {
None
}
fn set_updated_at(&mut self, _timestamp: DateTime<Utc>) {}
fn deleted_at(&self) -> Option<DateTime<Utc>> {
None
}
fn set_deleted_at(&mut self, _timestamp: Option<DateTime<Utc>>) {}
fn is_soft_deleted(&self) -> bool {
self.deleted_at().is_some()
}
fn from_row(row: &sqlx::postgres::PgRow) -> ModelResult<Self>
where
Self: Sized;
fn from_database_row(_row: &dyn DatabaseRow) -> ModelResult<Self>
where
Self: Sized,
{
Err(crate::error::ModelError::Serialization(
"from_database_row not implemented for this model - still using legacy from_row"
.to_string(),
))
}
fn to_fields(&self) -> HashMap<String, serde_json::Value>;
}