sea-orm-newtype

crates.io docs.rs

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.

Example

convert by From and Into trait

```rust use std::marker::PhantomData; use uuid::Uuid;

use seaormnewtype::DeriveNewType;

/// New type for id that has specific type.

[derive(Debug, Clone, PartialEq, DeriveNewType)]

[seaormnewtype(frominto = "Uuid", primarykey)]

pub struct Id(Uuid, PhantomData);

impl From for Id { fn from(id: Uuid) -> Id { Id(id, PhantomData) } }

impl From> for Uuid { fn from(value: Id) -> Self { value.0 } }

use sea_orm::entity::prelude::*;

[derive(Debug, Clone, PartialEq)]

pub struct ModelId;

[derive(Clone, Debug, DeriveEntityModel)]

[seaorm(tablename = "foo")]

pub struct Model { #[seaorm(primarykey)] id: Id, }

[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {} ```

convert by TryFrom and Into trait

```rust use std::fmt::Debug; use std::str::FromStr;

use seaormnewtype::DeriveNewType;

[derive(Clone, Debug, PartialEq, DeriveNewType)]

[seaormnewtype(tryfrominto = "String")]

pub struct EmailAddress(email_address::EmailAddress);

[derive(Debug, thiserror::Error)]

[error("ParseError")]

pub struct ParseError;

impl TryFrom for EmailAddress { type Error = ParseError; fn tryfrom(value: String) -> Result { Ok(EmailAddress( emailaddress::EmailAddress::fromstr(&value).maperr(|_| ParseError)?, )) } }

impl From for String { fn from(value: EmailAddress) -> Self { value.0.to_string() } }

use sea_orm::entity::prelude::*;

[derive(Clone, Debug, DeriveEntityModel)]

[seaorm(tablename = "foo")]

pub struct Model { #[seaorm(primarykey)] id: uuid::Uuid, email_address: EmailAddress, }

[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {} ```

handle it as self.0 type

```rust use seaormnewtype::DeriveNewType;

[derive(Clone, Debug, PartialEq, Eq, DeriveNewType)]

[seaormnewtype(transparent)]

pub struct Integer(i32);

use sea_orm::entity::prelude::*;

[derive(Clone, Debug, DeriveEntityModel)]

[seaorm(tablename = "foo")]

pub struct Model { #[seaorm(primarykey)] id: uuid::Uuid, integer: Integer, }

[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {} ```