turso_orm/entity/
base_entity.rs1use 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
19pub trait EntityTrait: IdenStatic + Default {
23 type Model: ModelTrait<Entity = Self> + super::FromQueryResult;
25 type Column: ColumnTrait;
27 type PrimaryKey: PrimaryKeyTrait + PrimaryKeyToColumn<Column = Self::Column>;
29 type ActiveModel: ActiveModelTrait<Entity = Self>;
31 type Relation: RelationTrait;
33
34 const TABLE_NAME: &'static str;
36
37 fn table_name() -> &'static str {
39 Self::TABLE_NAME
40 }
41
42 fn belongs_to<R: EntityTrait>(_: R) -> RelationBuilder<Self, R> {
44 RelationBuilder::new(RelationType::HasOne, true)
45 }
46
47 fn has_one<R: EntityTrait>(_: R) -> RelationBuilder<Self, R> {
49 RelationBuilder::new(RelationType::HasOne, false)
50 }
51
52 fn has_many<R: EntityTrait>(_: R) -> RelationBuilder<Self, R> {
54 RelationBuilder::new(RelationType::HasMany, false)
55 }
56
57 fn find() -> Select<Self> {
59 Select::new()
60 }
61
62 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 fn insert<A: ActiveModelTrait<Entity = Self>>(model: A) -> Insert<A> {
74 Insert::one(model)
75 }
76
77 fn insert_many<A: ActiveModelTrait<Entity = Self>, I: IntoIterator<Item = A>>(
79 models: I,
80 ) -> InsertMany<A> {
81 InsertMany::many(models)
82 }
83
84 fn update<A: ActiveModelTrait<Entity = Self>>(model: A) -> UpdateOne<A> {
86 UpdateOne::new(model)
87 }
88
89 fn update_many() -> UpdateMany<Self> {
91 UpdateMany::new()
92 }
93
94 fn delete<A: ActiveModelTrait<Entity = Self>>(model: A) -> DeleteOne<A> {
96 DeleteOne::new(model)
97 }
98
99 fn delete_many() -> DeleteMany<Self> {
101 DeleteMany::new()
102 }
103
104 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}