Skip to main content
Version: 0.13.x 🚧

New Type

You can define a New Type (T) and use it as model field. The following traits have to be implemented.

  1. Implement From<T> for sea_query::Value
  2. Implement sea_orm::TryGetable for T
  3. Implement sea_query::ValueType for T
  4. If the field is Option<T>, implement sea_query::Nullable for T
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "custom_value_type")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub number: Integer,
// Postgres only
pub str_vec: StringVec,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Clone, Debug, PartialEq, Eq, DeriveValueType)]
pub struct Integer(i32);

#[derive(Clone, Debug, PartialEq, Eq, DeriveValueType)]
pub struct StringVec(pub Vec<String>);
Which `StringVec` expands to:
impl std::convert::From<StringVec> for Value {
fn from(source: StringVec) -> Self {
source.0.into()
}
}

impl sea_orm::TryGetable for StringVec {
fn try_get_by<I: sea_orm::ColIdx>(res: &QueryResult, idx: I) -> Result<Self, sea_orm::TryGetError> {
<Vec<String> as sea_orm::TryGetable>::try_get_by(res, idx).map(|v| StringVec(v))
}
}

impl sea_orm::sea_query::ValueType for StringVec {
fn try_from(v: Value) -> Result<Self, sea_orm::sea_query::ValueTypeErr> {
<Vec<String> as sea_orm::sea_query::ValueType>::try_from(v).map(|v| StringVec(v))
}

fn type_name() -> String {
stringify!(StringVec).to_owned()
}

fn array_type() -> sea_orm::sea_query::ArrayType {
sea_orm::sea_query::ArrayType::String
}

fn column_type() -> sea_orm::sea_query::ColumnType {
sea_orm::sea_query::ColumnType::String(None)
}
}

You can also define a backend-generic Vec<T> field by serialize / deserialize the object to / from JSON:

use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "json_vec")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub str_vec: ObjectVec,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
pub struct ObjectVec(pub Vec<MyObject>);

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct MyObject {
..
}
Which `ObjectVec` expands to:
impl sea_orm::TryGetableFromJson for ObjectVec {}

impl std::convert::From<ObjectVec> for sea_orm::Value {
fn from(source: ObjectVec) -> Self {
sea_orm::Value::Json(serde_json::to_value(&source).ok().map(|s| std::boxed::Box::new(s)))
}
}

impl sea_orm::sea_query::ValueType for ObjectVec {
fn try_from(v: sea_orm::Value) -> Result<Self, sea_orm::sea_query::ValueTypeErr> {
match v {
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),
}
}

fn type_name() -> String {
stringify!(ObjectVec).to_owned()
}

fn array_type() -> sea_orm::sea_query::ArrayType {
sea_orm::sea_query::ArrayType::Json
}

fn column_type() -> sea_orm::sea_query::ColumnType {
sea_orm::sea_query::ColumnType::Json
}
}

impl sea_orm::sea_query::Nullable for ObjectVec {
fn null() -> sea_orm::Value {
sea_orm::Value::Json(None)
}
}