Skip to content

Commit 77870b1

Browse files
committed
✨ 构思设计了一套专门针对软删除与乐观锁的 CURD 扩展接口,所有后缀带 _safety 的新方法可以自动带上数据库中的相应字段工作。
1 parent 93c0cfe commit 77870b1

32 files changed

+311
-36
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ edition = "2024"
1919

2020
[workspace.dependencies]
2121
anyhow = { version = "^1", features = ["backtrace"] }
22+
async-trait = "^0.1"
2223
derive_more = { version = "^2", features = ["full"] }
2324
once_cell = "^1"
2425

packages/database/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ _utils = { path = "../utils", version = "*" }
1010

1111

1212
anyhow = { workspace = true }
13+
async-trait = { workspace = true }
1314
derive_more = { workspace = true }
1415
once_cell = { workspace = true }
1516

packages/database/src/models/area/area.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use sea_orm::entity::prelude::*;
22
use serde::{Deserialize, Serialize};
33

4-
use _utils::types::HiddenFlag;
4+
use _utils::{impl_safe_operation, types::HiddenFlag};
55

66
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
77
#[sea_orm(table_name = "area", schema_name = "genshin_map")]
@@ -75,4 +75,9 @@ impl Linked for ParentReferencingLink {
7575
}
7676
}
7777

78-
impl ActiveModelBehavior for ActiveModel {}
78+
impl_safe_operation! {
79+
active_model_ty: ActiveModel,
80+
updated_at_column_name: update_time,
81+
updated_at_column_init_expr: chrono::Utc::now().naive_utc(),
82+
del_flag_column: Column::DelFlag
83+
}

packages/database/src/models/area/item_area_public.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use _utils::impl_safe_operation;
12
use sea_orm::entity::prelude::*;
23
use serde::{Deserialize, Serialize};
34

@@ -47,4 +48,9 @@ pub enum Relation {
4748
ItemId,
4849
}
4950

50-
impl ActiveModelBehavior for ActiveModel {}
51+
impl_safe_operation! {
52+
active_model_ty: ActiveModel,
53+
updated_at_column_name: update_time,
54+
updated_at_column_init_expr: chrono::Utc::now().naive_utc(),
55+
del_flag_column: Column::DelFlag
56+
}

packages/database/src/models/common/history.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use sea_orm::entity::prelude::*;
22
use serde::{Deserialize, Serialize};
33

4-
use _utils::types::{HistoryEditType, HistoryOperationType};
4+
use _utils::{
5+
impl_safe_operation,
6+
types::{HistoryEditType, HistoryOperationType},
7+
};
58

69
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
710
#[sea_orm(table_name = "history", schema_name = "genshin_map")]
@@ -54,4 +57,9 @@ pub enum Relation {
5457
UpdaterId,
5558
}
5659

57-
impl ActiveModelBehavior for ActiveModel {}
60+
impl_safe_operation! {
61+
active_model_ty: ActiveModel,
62+
updated_at_column_name: update_time,
63+
updated_at_column_init_expr: chrono::Utc::now().naive_utc(),
64+
del_flag_column: Column::DelFlag
65+
}

packages/database/src/models/common/notice.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use _utils::impl_safe_operation;
12
use sea_orm::{entity::prelude::*, FromJsonQueryResult};
23
use serde::{Deserialize, Serialize};
34

@@ -54,4 +55,9 @@ pub enum Relation {
5455
UpdaterId,
5556
}
5657

57-
impl ActiveModelBehavior for ActiveModel {}
58+
impl_safe_operation! {
59+
active_model_ty: ActiveModel,
60+
updated_at_column_name: update_time,
61+
updated_at_column_init_expr: chrono::Utc::now().naive_utc(),
62+
del_flag_column: Column::DelFlag
63+
}

packages/database/src/models/common/route.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use sea_orm::{entity::prelude::*, FromJsonQueryResult};
22
use serde::{Deserialize, Serialize};
33

4-
use _utils::types::HiddenFlag;
4+
use _utils::{impl_safe_operation, types::HiddenFlag};
55

66
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
77
#[sea_orm(table_name = "route", schema_name = "genshin_map")]
@@ -59,4 +59,9 @@ pub enum Relation {
5959
UpdaterId,
6060
}
6161

62-
impl ActiveModelBehavior for ActiveModel {}
62+
impl_safe_operation! {
63+
active_model_ty: ActiveModel,
64+
updated_at_column_name: update_time,
65+
updated_at_column_init_expr: chrono::Utc::now().naive_utc(),
66+
del_flag_column: Column::DelFlag
67+
}

packages/database/src/models/common/score_stat.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use sea_orm::entity::prelude::*;
22
use serde::{Deserialize, Serialize};
33

4-
use _utils::types::ScopeStatType;
4+
use _utils::{impl_safe_operation, types::ScopeStatType};
55

66
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
77
#[sea_orm(table_name = "score_stat", schema_name = "genshin_map")]
@@ -57,4 +57,9 @@ pub enum Relation {
5757
UpdaterId,
5858
}
5959

60-
impl ActiveModelBehavior for ActiveModel {}
60+
impl_safe_operation! {
61+
active_model_ty: ActiveModel,
62+
updated_at_column_name: update_time,
63+
updated_at_column_init_expr: chrono::Utc::now().naive_utc(),
64+
del_flag_column: Column::DelFlag
65+
}

packages/database/src/models/icon/icon.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use _utils::impl_safe_operation;
12
use sea_orm::entity::prelude::*;
23
use serde::{Deserialize, Serialize};
34

@@ -43,4 +44,9 @@ pub enum Relation {
4344
UpdaterId,
4445
}
4546

46-
impl ActiveModelBehavior for ActiveModel {}
47+
impl_safe_operation! {
48+
active_model_ty: ActiveModel,
49+
updated_at_column_name: update_time,
50+
updated_at_column_init_expr: chrono::Utc::now().naive_utc(),
51+
del_flag_column: Column::DelFlag
52+
}

0 commit comments

Comments
 (0)