-
-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vector of string save as serialized format in db
- Loading branch information
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use sea_orm::entity::prelude::*; | ||
use sea_orm::{TryGetError, TryGetable}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "json_vec")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i32, | ||
pub str_vec: StringVec, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} | ||
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | ||
pub struct StringVec(pub Vec<String>); | ||
|
||
impl From<StringVec> for Value { | ||
fn from(source: StringVec) -> Self { | ||
Value::String(serde_json::to_string(&source).ok().map(|s| Box::new(s))) | ||
} | ||
} | ||
|
||
impl TryGetable for StringVec { | ||
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError> { | ||
let json_str: String = res.try_get(pre, col).map_err(TryGetError::DbErr)?; | ||
serde_json::from_str(&json_str).map_err(|e| TryGetError::DbErr(DbErr::Json(e.to_string()))) | ||
} | ||
} | ||
|
||
impl sea_query::ValueType for StringVec { | ||
fn try_from(v: Value) -> Result<Self, sea_query::ValueTypeErr> { | ||
match v { | ||
Value::String(Some(x)) => Ok(StringVec( | ||
serde_json::from_str(&x).map_err(|_| sea_query::ValueTypeErr)?, | ||
)), | ||
_ => Err(sea_query::ValueTypeErr), | ||
} | ||
} | ||
|
||
fn type_name() -> String { | ||
stringify!(StringVec).to_owned() | ||
} | ||
|
||
fn column_type() -> sea_query::ColumnType { | ||
sea_query::ColumnType::String(None) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
pub mod common; | ||
|
||
pub use common::{features::*, setup::*, TestContext}; | ||
use pretty_assertions::assert_eq; | ||
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection}; | ||
|
||
#[sea_orm_macros::test] | ||
#[cfg(any( | ||
feature = "sqlx-mysql", | ||
feature = "sqlx-sqlite", | ||
feature = "sqlx-postgres" | ||
))] | ||
async fn main() -> Result<(), DbErr> { | ||
let ctx = TestContext::new("json_vec_tests").await; | ||
create_tables(&ctx.db).await?; | ||
insert_json_vec(&ctx.db).await?; | ||
ctx.delete().await; | ||
|
||
Ok(()) | ||
} | ||
|
||
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(), | ||
]) | ||
}; | ||
|
||
let result = json_vec.clone().into_active_model().insert(db).await?; | ||
|
||
assert_eq!(result, json_vec); | ||
|
||
let model = json_vec::Entity::find() | ||
.filter(json_vec::Column::Id.eq(json_vec.id)) | ||
.one(db) | ||
.await?; | ||
|
||
assert_eq!( | ||
model, | ||
Some(json_vec) | ||
); | ||
|
||
assert!(false); | ||
|
||
Ok(()) | ||
} |