feat: add database entities models

This commit is contained in:
əlemi 2024-06-21 20:51:22 +02:00
parent 19e9657ed8
commit be0114601e
Signed by: alemi
GPG key ID: A4895B84D311642C
6 changed files with 123 additions and 0 deletions

9
model/Cargo.toml Normal file
View file

@ -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"] }

25
model/src/chats.rs Normal file
View file

@ -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<super::messages::Entity> for Entity {
fn to() -> RelationDef {
Relation::Messages.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

6
model/src/lib.rs Normal file
View file

@ -0,0 +1,6 @@
pub mod chats;
pub mod messages;
pub mod users;
mod prelude;
pub use prelude::*;

49
model/src/messages.rs Normal file
View file

@ -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<String>,
pub created: ChronoDateTimeUtc,
pub updated: Option<ChronoDateTimeUtc>,
}
#[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<super::chats::Entity> for Entity {
fn to() -> RelationDef {
Relation::Chats.def()
}
}
impl Related<super::users::Entity> for Entity {
fn to() -> RelationDef {
Relation::Users.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

3
model/src/prelude.rs Normal file
View file

@ -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;

31
model/src/users.rs Normal file
View file

@ -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<String>,
pub description: Option<String>,
pub avatar: Option<String>,
pub created: ChronoDateTimeUtc,
pub updated: Option<ChronoDateTimeUtc>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::messages::Entity")]
Messages,
}
impl Related<super::messages::Entity> for Entity {
fn to() -> RelationDef {
Relation::Messages.def()
}
}
impl ActiveModelBehavior for ActiveModel {}