Crates.io | sea-orm-newtype |
lib.rs | sea-orm-newtype |
version | 0.0.1 |
source | src |
created_at | 2023-08-03 06:17:55.349252 |
updated_at | 2023-08-03 06:17:55.349252 |
description | helper crate to implement new-type pattern for sea-orm |
homepage | |
repository | https://github.com/deepgreenAN/sea-orm-newtype |
max_upload_size | |
id | 933430 |
size | 73,810 |
This crate provides helper derive macro to implement new-type pattern for sea-orm. From sea-orm @ 0.12.x, you can use DeriveValueType macro. Please check it too.
use std::marker::PhantomData;
use uuid::Uuid;
use sea_orm_newtype::DeriveNewType;
/// New type for id that has specific type.
#[derive(Debug, Clone, PartialEq, DeriveNewType)]
#[sea_orm_newtype(from_into = "Uuid", primary_key)]
pub struct Id<T>(Uuid, PhantomData<T>);
impl<T> From<Uuid> for Id<T> {
fn from(id: Uuid) -> Id<T> {
Id(id, PhantomData)
}
}
impl<T> From<Id<T>> for Uuid {
fn from(value: Id<T>) -> Self {
value.0
}
}
use sea_orm::entity::prelude::*;
#[derive(Debug, Clone, PartialEq)]
pub struct ModelId;
#[derive(Clone, Debug, DeriveEntityModel)]
#[sea_orm(table_name = "foo")]
pub struct Model {
#[sea_orm(primary_key)]
id: Id<ModelId>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
use std::fmt::Debug;
use std::str::FromStr;
use sea_orm_newtype::DeriveNewType;
#[derive(Clone, Debug, PartialEq, DeriveNewType)]
#[sea_orm_newtype(try_from_into = "String")]
pub struct EmailAddress(email_address::EmailAddress);
#[derive(Debug, thiserror::Error)]
#[error("ParseError")]
pub struct ParseError;
impl TryFrom<String> for EmailAddress {
type Error = ParseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(EmailAddress(
email_address::EmailAddress::from_str(&value).map_err(|_| ParseError)?,
))
}
}
impl From<EmailAddress> for String {
fn from(value: EmailAddress) -> Self {
value.0.to_string()
}
}
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, DeriveEntityModel)]
#[sea_orm(table_name = "foo")]
pub struct Model {
#[sea_orm(primary_key)]
id: uuid::Uuid,
email_address: EmailAddress,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
use sea_orm_newtype::DeriveNewType;
#[derive(Clone, Debug, PartialEq, Eq, DeriveNewType)]
#[sea_orm_newtype(transparent)]
pub struct Integer(i32);
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, DeriveEntityModel)]
#[sea_orm(table_name = "foo")]
pub struct Model {
#[sea_orm(primary_key)]
id: uuid::Uuid,
integer: Integer,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}