sea-orm/tests/common/features/event_trigger.rs
Billy Chan e246d3faaf
Cont. Upgrade to SeaQuery 0.28.0 (#1366)
* Upgrade to SeaQuery 0.28.0

* Remove unnecessary heap allocation

* Upgrade sea-query-binder

* Upgrade sea-schema

* Fix

* Upgrade sea-schema

* refactoring

Co-authored-by: Ivan Krivosheev <py.krivosheev@gmail.com>
2023-01-05 20:41:28 +08:00

71 lines
1.8 KiB
Rust

use sea_orm::entity::prelude::*;
use sea_orm::{
sea_query::{ArrayType, ColumnType, SeaRc, ValueType},
TryGetError, TryGetable,
};
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "event_trigger")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub events: Events,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Event(pub String);
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Events(pub Vec<Event>);
impl From<Events> for Value {
fn from(events: Events) -> Self {
let Events(events) = events;
Value::Array(
ArrayType::String,
Some(Box::new(
events
.into_iter()
.map(|Event(s)| Value::String(Some(Box::new(s))))
.collect(),
)),
)
}
}
impl TryGetable for Events {
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError> {
let vec: Vec<String> = res.try_get(pre, col).map_err(TryGetError::DbErr)?;
Ok(Events(vec.into_iter().map(Event).collect()))
}
}
impl ValueType for Events {
fn try_from(v: Value) -> Result<Self, sea_query::ValueTypeErr> {
let value: Option<Vec<String>> =
v.expect("This Value::Array should consist of Value::String");
let vec = match value {
Some(v) => v.into_iter().map(Event).collect(),
None => vec![],
};
Ok(Events(vec))
}
fn type_name() -> String {
stringify!(Events).to_owned()
}
fn array_type() -> ArrayType {
ArrayType::String
}
fn column_type() -> ColumnType {
ColumnType::Array(SeaRc::new(ColumnType::String(None)))
}
}