Skip to main content

turso_orm/entity/
base_entity.rs

1//! The entity trait, modeled by [`EntityTrait`].
2//!
3//! `EntityTrait` is implemented on the unit struct `Entity` of each entity
4//! module and is the hub that names the model, column, key, active model and
5//! relation types belonging to one table. All of its methods are
6//! constructors for the query builders in `crate::query`; the builders do
7//! the actual work so that this trait stays a thin, stable surface that the
8//! derive macro can implement with a handful of associated types.
9
10use super::active_model::ActiveModelTrait;
11use super::column::ColumnTrait;
12use super::iden::IdenStatic;
13use super::model::ModelTrait;
14use super::primary_key::{PrimaryKeyToColumn, PrimaryKeyTrait};
15use super::relation::{RelationBuilder, RelationTrait, RelationType};
16use crate::query::{DeleteMany, DeleteOne, Insert, InsertMany, Select, UpdateMany, UpdateOne};
17use crate::types::IntoValueTuple;
18
19/// An entity: a table together with its model, columns, key and relations.
20///
21/// Derived by `DeriveEntityModel`; the unit struct `Entity` implements it.
22pub trait EntityTrait: IdenStatic + Default {
23    /// The model struct.
24    type Model: ModelTrait<Entity = Self> + super::FromQueryResult;
25    /// The column enum.
26    type Column: ColumnTrait;
27    /// The primary key enum.
28    type PrimaryKey: PrimaryKeyTrait + PrimaryKeyToColumn<Column = Self::Column>;
29    /// The active model struct.
30    type ActiveModel: ActiveModelTrait<Entity = Self>;
31    /// The relation enum.
32    type Relation: RelationTrait;
33
34    /// The table name.
35    const TABLE_NAME: &'static str;
36
37    /// The table name, as a method for contexts that cannot name the constant.
38    fn table_name() -> &'static str {
39        Self::TABLE_NAME
40    }
41
42    /// Starts a `belongs_to` relation, in which `Self` holds the foreign key.
43    fn belongs_to<R: EntityTrait>(_: R) -> RelationBuilder<Self, R> {
44        RelationBuilder::new(RelationType::HasOne, true)
45    }
46
47    /// Starts a `has_one` relation, in which `R` holds the foreign key.
48    fn has_one<R: EntityTrait>(_: R) -> RelationBuilder<Self, R> {
49        RelationBuilder::new(RelationType::HasOne, false)
50    }
51
52    /// Starts a `has_many` relation, in which `R` holds the foreign key.
53    fn has_many<R: EntityTrait>(_: R) -> RelationBuilder<Self, R> {
54        RelationBuilder::new(RelationType::HasMany, false)
55    }
56
57    /// Builds `SELECT * FROM table`.
58    fn find() -> Select<Self> {
59        Select::new()
60    }
61
62    /// Builds `SELECT * FROM table WHERE pk = id`.
63    ///
64    /// Composite keys are passed as a tuple in key-column order.
65    fn find_by_id<T>(id: T) -> Select<Self>
66    where
67        T: Into<<Self::PrimaryKey as PrimaryKeyTrait>::ValueType>,
68    {
69        Select::new().filter_by_pk(id.into().into_value_tuple())
70    }
71
72    /// Builds an `INSERT` of one active model.
73    fn insert<A: ActiveModelTrait<Entity = Self>>(model: A) -> Insert<A> {
74        Insert::one(model)
75    }
76
77    /// Builds a single `INSERT` of several active models.
78    fn insert_many<A: ActiveModelTrait<Entity = Self>, I: IntoIterator<Item = A>>(
79        models: I,
80    ) -> InsertMany<A> {
81        InsertMany::many(models)
82    }
83
84    /// Builds an `UPDATE` of one active model, matched by primary key.
85    fn update<A: ActiveModelTrait<Entity = Self>>(model: A) -> UpdateOne<A> {
86        UpdateOne::new(model)
87    }
88
89    /// Builds an `UPDATE` over many rows.
90    fn update_many() -> UpdateMany<Self> {
91        UpdateMany::new()
92    }
93
94    /// Builds a `DELETE` of one active model, matched by primary key.
95    fn delete<A: ActiveModelTrait<Entity = Self>>(model: A) -> DeleteOne<A> {
96        DeleteOne::new(model)
97    }
98
99    /// Builds a `DELETE` over many rows.
100    fn delete_many() -> DeleteMany<Self> {
101        DeleteMany::new()
102    }
103
104    /// Builds a `DELETE` matched by primary key.
105    fn delete_by_id<T>(id: T) -> DeleteMany<Self>
106    where
107        T: Into<<Self::PrimaryKey as PrimaryKeyTrait>::ValueType>,
108    {
109        DeleteMany::new().filter_by_pk(id.into().into_value_tuple())
110    }
111}