starfish_core/entities/
entity_attribute.rs1use sea_orm::entity::prelude::*;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
5#[sea_orm(rs_type = "String", db_type = "String(None)")]
6pub enum Datatype {
7 #[sea_orm(string_value = "Int")]
8 Int,
9 #[sea_orm(string_value = "String")]
10 String,
11}
12
13impl Datatype {
14 pub fn value_with_datatype(&self, value: Option<&serde_json::Value>) -> Value {
15 match self {
16 Datatype::Int => {
17 if let Some(value) = value {
18 value.as_i64().into()
19 } else {
20 None::<i64>.into()
21 }
22 }
23 Datatype::String => {
24 if let Some(value) = value {
25 value.as_str().into()
26 } else {
27 None::<String>.into()
28 }
29 }
30 }
31 }
32}
33
34#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
35#[sea_orm(table_name = "entity_attribute")]
36pub struct Model {
37 #[sea_orm(primary_key)]
38 pub id: i32,
39 #[sea_orm(indexed)]
40 pub entity_id: i32,
41 #[sea_orm(indexed)]
42 pub name: String,
43 #[sea_orm(indexed)]
44 pub datatype: Datatype,
45}
46
47#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
48pub enum Relation {
49 #[sea_orm(
50 belongs_to = "super::entity::Entity",
51 from = "Column::EntityId",
52 to = "super::entity::Column::Id"
53 )]
54 Entity,
55}
56
57impl Related<super::entity::Entity> for Entity {
58 fn to() -> RelationDef {
59 Relation::Entity.def()
60 }
61}
62
63impl ActiveModelBehavior for ActiveModel {}