diff --git a/tests/common/features/json_vec.rs b/tests/common/features/json_vec.rs index 8e2fdcc6..5eb62349 100644 --- a/tests/common/features/json_vec.rs +++ b/tests/common/features/json_vec.rs @@ -1,5 +1,5 @@ use sea_orm::entity::prelude::*; -use sea_orm::{TryGetError, TryGetable}; +use sea_orm::TryGetableFromJson; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; pub struct Model { #[sea_orm(primary_key)] pub id: i32, - pub str_vec: StringVec, + pub str_vec: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] @@ -18,26 +18,25 @@ impl ActiveModelBehavior for ActiveModel {} #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct StringVec(pub Vec); +impl TryGetableFromJson for StringVec {} + impl From for Value { fn from(source: StringVec) -> Self { - Value::String(serde_json::to_string(&source).ok().map(Box::new)) - } -} - -impl TryGetable for StringVec { - fn try_get_by(res: &QueryResult, idx: I) -> Result { - let json_str: String = res.try_get_by(idx).map_err(TryGetError::DbErr)?; - serde_json::from_str(&json_str).map_err(|e| TryGetError::DbErr(DbErr::Json(e.to_string()))) + sea_orm::Value::Json( + serde_json::to_value(&source) + .ok() + .map(|s| std::boxed::Box::new(s)), + ) } } impl sea_query::ValueType for StringVec { fn try_from(v: Value) -> Result { match v { - Value::String(Some(x)) => Ok(StringVec( - serde_json::from_str(&x).map_err(|_| sea_query::ValueTypeErr)?, - )), - _ => Err(sea_query::ValueTypeErr), + sea_orm::Value::Json(Some(json)) => { + Ok(serde_json::from_value(*json).map_err(|_| sea_orm::sea_query::ValueTypeErr)?) + } + _ => Err(sea_orm::sea_query::ValueTypeErr), } } @@ -46,10 +45,16 @@ impl sea_query::ValueType for StringVec { } fn array_type() -> sea_orm::sea_query::ArrayType { - sea_orm::sea_query::ArrayType::String + sea_orm::sea_query::ArrayType::Json } fn column_type() -> sea_query::ColumnType { - sea_query::ColumnType::String(None) + sea_query::ColumnType::Json + } +} + +impl sea_orm::sea_query::Nullable for StringVec { + fn null() -> sea_orm::Value { + sea_orm::Value::Json(None) } } diff --git a/tests/common/features/json_vec_derive.rs b/tests/common/features/json_vec_derive.rs index ebe95495..cc6e177b 100644 --- a/tests/common/features/json_vec_derive.rs +++ b/tests/common/features/json_vec_derive.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; pub struct Model { #[sea_orm(primary_key)] pub id: i32, - pub str_vec: StringVec, + pub str_vec: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/tests/common/features/schema.rs b/tests/common/features/schema.rs index 058474e0..6e1f71cc 100644 --- a/tests/common/features/schema.rs +++ b/tests/common/features/schema.rs @@ -313,7 +313,7 @@ pub async fn create_json_vec_table(db: &DbConn) -> Result { .auto_increment() .primary_key(), ) - .col(ColumnDef::new(json_vec::Column::StrVec).string().not_null()) + .col(ColumnDef::new(json_vec::Column::StrVec).json()) .to_owned(); create_table(db, &create_table_stmt, JsonVec).await diff --git a/tests/json_vec_tests.rs b/tests/json_vec_tests.rs index 0fa183a9..9af2a330 100644 --- a/tests/json_vec_tests.rs +++ b/tests/json_vec_tests.rs @@ -23,7 +23,11 @@ async fn main() -> Result<(), DbErr> { pub async fn insert_json_vec(db: &DatabaseConnection) -> Result<(), DbErr> { let json_vec = json_vec::Model { id: 1, - str_vec: json_vec::StringVec(vec!["1".to_string(), "2".to_string(), "3".to_string()]), + str_vec: Some(json_vec::StringVec(vec![ + "1".to_string(), + "2".to_string(), + "3".to_string(), + ])), }; let result = json_vec.clone().into_active_model().insert(db).await?; @@ -43,11 +47,11 @@ pub async fn insert_json_vec(db: &DatabaseConnection) -> Result<(), DbErr> { pub async fn insert_json_vec_derive(db: &DatabaseConnection) -> Result<(), DbErr> { let json_vec = json_vec_derive::Model { id: 2, - str_vec: json_vec_derive::StringVec(vec![ + str_vec: Some(json_vec_derive::StringVec(vec![ "4".to_string(), "5".to_string(), "6".to_string(), - ]), + ])), }; let result = json_vec.clone().into_active_model().insert(db).await?;