pub trait Entity:
Clone
+ Send
+ Sync
+ 'static {
type Service: Send + Sync;
// Required methods
fn resource_name() -> &'static str;
fn resource_name_singular() -> &'static str;
fn service_from_host(
host: &Arc<dyn Any + Send + Sync>,
) -> Result<Arc<Self::Service>>;
fn id(&self) -> Uuid;
fn entity_type(&self) -> &str;
fn created_at(&self) -> DateTime<Utc>;
fn updated_at(&self) -> DateTime<Utc>;
fn deleted_at(&self) -> Option<DateTime<Utc>>;
fn status(&self) -> &str;
// Provided methods
fn tenant_id(&self) -> Option<Uuid> { ... }
fn is_deleted(&self) -> bool { ... }
fn is_active(&self) -> bool { ... }
}Expand description
Base trait for all entities in the system.
This trait provides the fundamental metadata needed for any entity type. All entities have:
- id: Unique identifier
- type: Entity type name (e.g., “user”, “product”)
- created_at: Creation timestamp
- updated_at: Last modification timestamp
- deleted_at: Soft deletion timestamp (optional)
- status: Current status of the entity
Required Associated Types§
Required Methods§
Sourcefn resource_name() -> &'static str
fn resource_name() -> &'static str
The plural resource name used in URLs (e.g., “users”, “companies”)
Sourcefn resource_name_singular() -> &'static str
fn resource_name_singular() -> &'static str
The singular resource name (e.g., “user”, “company”)
Sourcefn service_from_host(
host: &Arc<dyn Any + Send + Sync>,
) -> Result<Arc<Self::Service>>
fn service_from_host( host: &Arc<dyn Any + Send + Sync>, ) -> Result<Arc<Self::Service>>
Extract the service instance from the application host/state
Sourcefn entity_type(&self) -> &str
fn entity_type(&self) -> &str
Get the entity type name
Sourcefn created_at(&self) -> DateTime<Utc>
fn created_at(&self) -> DateTime<Utc>
Get the creation timestamp
Sourcefn updated_at(&self) -> DateTime<Utc>
fn updated_at(&self) -> DateTime<Utc>
Get the last update timestamp
Sourcefn deleted_at(&self) -> Option<DateTime<Utc>>
fn deleted_at(&self) -> Option<DateTime<Utc>>
Get the deletion timestamp (soft delete)
Provided Methods§
Sourcefn tenant_id(&self) -> Option<Uuid>
fn tenant_id(&self) -> Option<Uuid>
Get the tenant ID for multi-tenant isolation.
Returns None by default for single-tenant applications or system-wide entities. Override this method to enable multi-tenancy for specific entity types.
§Multi-Tenant Usage
impl Entity for MyEntity {
fn tenant_id(&self) -> Option<Uuid> {
self.tenant_id // Return actual tenant_id field
}
}Sourcefn is_deleted(&self) -> bool
fn is_deleted(&self) -> bool
Check if the entity has been soft-deleted
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.