1use sea_orm::{
2 ActiveModelBehavior, ActiveModelTrait, ConnectionTrait, EntityTrait, FromQueryResult,
3};
4
5use crate::query::{EntityDeleteMany, EntityQuery, EntityUpdateMany};
6
7#[allow(clippy::wrong_self_convention)]
15pub trait Record: Sized + Send + Sync + 'static {
16 type Entity: EntityTrait;
17 type ActiveModel: ActiveModelTrait<Entity = Self::Entity> + ActiveModelBehavior + Send + 'static;
18
19 fn from_model(model: <Self::Entity as EntityTrait>::Model) -> Self;
23
24 fn into_active_model_full(&self) -> Self::ActiveModel;
26
27 fn into_active_model(&self) -> Self::ActiveModel;
29
30 fn apply_auto_fields(am: &mut Self::ActiveModel, is_insert: bool);
32
33 fn find_all(
37 db: &impl ConnectionTrait,
38 ) -> impl std::future::Future<Output = Result<Vec<Self>, modo::Error>> + Send
39 where
40 <Self::Entity as EntityTrait>::Model: FromQueryResult + Send + Sync,
41 Self: From<<Self::Entity as EntityTrait>::Model>,
42 {
43 async {
44 use sea_orm::EntityTrait as _;
45 let models = Self::Entity::find()
46 .all(db)
47 .await
48 .map_err(crate::error::db_err_to_error)?;
49 Ok(models.into_iter().map(Self::from_model).collect())
50 }
51 }
52
53 fn query() -> EntityQuery<Self, Self::Entity>
55 where
56 <Self::Entity as EntityTrait>::Model: FromQueryResult + Send + Sync,
57 Self: From<<Self::Entity as EntityTrait>::Model>,
58 {
59 use sea_orm::EntityTrait as _;
60 EntityQuery::new(Self::Entity::find())
61 }
62
63 fn update_many() -> EntityUpdateMany<Self::Entity> {
65 use sea_orm::EntityTrait as _;
66 EntityUpdateMany::new(Self::Entity::update_many())
67 }
68
69 fn delete_many() -> EntityDeleteMany<Self::Entity> {
71 use sea_orm::EntityTrait as _;
72 EntityDeleteMany::new(Self::Entity::delete_many())
73 }
74}