elif_orm/model/
core_trait.rs

1//! Core Model Trait - Base definition for database entities
2//!
3//! Defines the fundamental Model trait with type requirements, table metadata,
4//! primary key handling, timestamp configuration, and serialization contract.
5
6use chrono::{DateTime, Utc};
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9use std::fmt::Debug;
10
11use crate::backends::DatabaseRow;
12use crate::error::ModelResult;
13
14/// Core trait for database models with standard ORM operations
15pub trait Model: Send + Sync + Debug + Serialize + for<'de> Deserialize<'de> {
16    /// The type used for this model's primary key
17    type PrimaryKey: Clone + Send + Sync + Debug + std::fmt::Display + Default;
18
19    /// Table name for this model
20    fn table_name() -> &'static str;
21
22    /// Primary key field name(s)
23    fn primary_key_name() -> &'static str {
24        "id"
25    }
26
27    /// Get the primary key value for this model instance
28    fn primary_key(&self) -> Option<Self::PrimaryKey>;
29
30    /// Set the primary key value for this model instance
31    fn set_primary_key(&mut self, key: Self::PrimaryKey);
32
33    /// Check if this model uses timestamps (created_at, updated_at)
34    fn uses_timestamps() -> bool {
35        false
36    }
37
38    /// Check if this model supports soft deletes
39    fn uses_soft_deletes() -> bool {
40        false
41    }
42
43    /// Get created_at timestamp if available
44    fn created_at(&self) -> Option<DateTime<Utc>> {
45        None
46    }
47
48    /// Set created_at timestamp
49    fn set_created_at(&mut self, _timestamp: DateTime<Utc>) {}
50
51    /// Get updated_at timestamp if available
52    fn updated_at(&self) -> Option<DateTime<Utc>> {
53        None
54    }
55
56    /// Set updated_at timestamp
57    fn set_updated_at(&mut self, _timestamp: DateTime<Utc>) {}
58
59    /// Get deleted_at timestamp if available (for soft deletes)
60    fn deleted_at(&self) -> Option<DateTime<Utc>> {
61        None
62    }
63
64    /// Set deleted_at timestamp (for soft deletes)
65    fn set_deleted_at(&mut self, _timestamp: Option<DateTime<Utc>>) {}
66
67    /// Check if this model instance is soft deleted
68    fn is_soft_deleted(&self) -> bool {
69        self.deleted_at().is_some()
70    }
71
72    /// Create a model instance from a database row
73    /// This will be automatically implemented by the derive macro
74    fn from_row(row: &sqlx::postgres::PgRow) -> ModelResult<Self>
75    where
76        Self: Sized;
77
78    /// Create a model instance from a database row (abstracted version)
79    /// This will replace from_row in the future
80    fn from_database_row(_row: &dyn DatabaseRow) -> ModelResult<Self>
81    where
82        Self: Sized,
83    {
84        // Default implementation that can be overridden
85        // For now, this requires concrete implementation by each model
86        Err(crate::error::ModelError::Serialization(
87            "from_database_row not implemented for this model - still using legacy from_row"
88                .to_string(),
89        ))
90    }
91
92    /// Convert model to field-value pairs for database operations
93    /// This will be automatically implemented by the derive macro
94    fn to_fields(&self) -> HashMap<String, serde_json::Value>;
95}