mayhem_db/models/server/
member_role.rs

1use sea_orm::entity::prelude::*;
2use serde::{Deserialize, Serialize};
3
4#[derive(Copy, Clone, Default, Debug, DeriveEntity, Serialize, Deserialize)]
5pub struct Entity;
6
7#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel, Serialize, Deserialize)]
8pub struct Model {
9    pub member_id: i32,
10    pub role_id: i32,
11}
12
13#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn, Serialize, Deserialize)]
14pub enum Column {
15    MemberId,
16    RoleId,
17}
18
19#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey, Serialize, Deserialize)]
20pub enum PrimaryKey {
21    MemberId,
22    RoleId,
23}
24
25#[derive(Copy, Clone, Debug, EnumIter, Serialize, Deserialize)]
26pub enum Relation {
27    Member,
28    Role,
29}
30
31impl EntityName for Entity {
32    fn table_name(&self) -> &str {
33        return "member_roles";
34    }
35}
36
37impl ColumnTrait for Column {
38    type EntityName = Entity;
39
40    fn def(&self) -> ColumnDef {
41        match self {
42            Self::MemberId => ColumnType::Integer.def(),
43            Self::RoleId => ColumnType::Integer.def(),
44        }
45    }
46}
47
48impl PrimaryKeyTrait for PrimaryKey {
49    type ValueType = i32;
50
51    fn auto_increment() -> bool {
52        return true;
53    }
54}
55
56impl Related<super::member::Entity> for Entity {
57    fn to() -> RelationDef {
58        return Relation::Member.def();
59    }
60}
61
62impl Related<super::role::Entity> for Entity {
63    fn to() -> RelationDef {
64        return Relation::Role.def();
65    }
66}
67
68impl RelationTrait for Relation {
69    fn def(&self) -> RelationDef {
70        match self {
71            Self::Member => Entity::belongs_to(super::member::Entity)
72                .from(Column::MemberId)
73                .to(super::member::Column::Id)
74                .into(),
75
76            Self::Role => Entity::belongs_to(super::role::Entity)
77                .from(Column::RoleId)
78                .to(super::role::Column::Id)
79                .into(),
80        }
81    }
82}
83
84impl ActiveModelBehavior for ActiveModel {}