From be0114601e5f3c019382e32b5f295900ea52a240 Mon Sep 17 00:00:00 2001 From: alemi Date: Fri, 21 Jun 2024 20:51:22 +0200 Subject: [PATCH] feat: add database entities models --- model/Cargo.toml | 9 ++++++++ model/src/chats.rs | 25 ++++++++++++++++++++++ model/src/lib.rs | 6 ++++++ model/src/messages.rs | 49 +++++++++++++++++++++++++++++++++++++++++++ model/src/prelude.rs | 3 +++ model/src/users.rs | 31 +++++++++++++++++++++++++++ 6 files changed, 123 insertions(+) create mode 100644 model/Cargo.toml create mode 100644 model/src/chats.rs create mode 100644 model/src/lib.rs create mode 100644 model/src/messages.rs create mode 100644 model/src/prelude.rs create mode 100644 model/src/users.rs diff --git a/model/Cargo.toml b/model/Cargo.toml new file mode 100644 index 0000000..67d8bee --- /dev/null +++ b/model/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "scct-model" +version = "0.1.0" +edition = "2021" + +[lib] + +[dependencies] +sea-orm = { version = "0.12.15", features = ["runtime-tokio-rustls", "sqlx-mysql", "sqlx-sqlite", "sqlx-postgres"] } diff --git a/model/src/chats.rs b/model/src/chats.rs new file mode 100644 index 0000000..be43e7d --- /dev/null +++ b/model/src/chats.rs @@ -0,0 +1,25 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.3 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "chats")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub id: Uuid, + pub name: String, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::messages::Entity")] + Messages, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Messages.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/model/src/lib.rs b/model/src/lib.rs new file mode 100644 index 0000000..7cc3f93 --- /dev/null +++ b/model/src/lib.rs @@ -0,0 +1,6 @@ +pub mod chats; +pub mod messages; +pub mod users; + +mod prelude; +pub use prelude::*; diff --git a/model/src/messages.rs b/model/src/messages.rs new file mode 100644 index 0000000..cc04043 --- /dev/null +++ b/model/src/messages.rs @@ -0,0 +1,49 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.3 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "messages")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub id: Uuid, + pub user_id: Uuid, + pub chat_id: Uuid, + pub content: Option, + pub created: ChronoDateTimeUtc, + pub updated: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::chats::Entity", + from = "Column::ChatId", + to = "super::chats::Column::Id", + on_update = "Cascade", + on_delete = "NoAction" + )] + Chats, + #[sea_orm( + belongs_to = "super::users::Entity", + from = "Column::UserId", + to = "super::users::Column::Id", + on_update = "Cascade", + on_delete = "NoAction" + )] + Users, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Chats.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Users.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/model/src/prelude.rs b/model/src/prelude.rs new file mode 100644 index 0000000..408d2c2 --- /dev/null +++ b/model/src/prelude.rs @@ -0,0 +1,3 @@ +pub use super::chats::Entity as Chats; +pub use super::messages::Entity as Messages; +pub use super::users::Entity as Users; diff --git a/model/src/users.rs b/model/src/users.rs new file mode 100644 index 0000000..1a9ba16 --- /dev/null +++ b/model/src/users.rs @@ -0,0 +1,31 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.3 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "users")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub id: Uuid, + pub username: String, + pub domain: String, + pub display_name: Option, + pub description: Option, + pub avatar: Option, + pub created: ChronoDateTimeUtc, + pub updated: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::messages::Entity")] + Messages, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Messages.def() + } +} + +impl ActiveModelBehavior for ActiveModel {}