Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use custom types inside Model? #116

Closed
MuhannadAlrusayni opened this issue Aug 28, 2021 · 8 comments
Closed

How to use custom types inside Model? #116

MuhannadAlrusayni opened this issue Aug 28, 2021 · 8 comments

Comments

@MuhannadAlrusayni
Copy link
Contributor

Hi, I want to use my custom types in the Model:

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model {
    pub id: AccountId,
    pub username: Username,
    pub email: Email,
    pub password: String,
    pub salt: String,
    pub created_at: DateTime,
    pub locked: Option<String>,
    pub disabled: Option<String>,
}

There are a few issues I faced while I'm trying to do so:

Another questions that arise in my head is how SeaORM would be able to convert the row table to my Model back since there is no information that tells SeaORM how to do so, so I think we need a way to do so, for example TryFrom<Value> for Email or FromSqlType trait?

Another thing that I notice is, do we really need ValueTypeDefault? why not just use Default trait since it's already bounded:

impl ValueTypeDefault for $type
where
    $type: Default,
{
    fn default() -> Self {
        Default::default()
    }
}

And I'm not sure if we going to keep this bound, since it's not possible to impl Default for all custom types.

Sorry for the long issue, but I think this is an important feature to have in an ORM. and yeah this ORM look very promising, keep up the good work

@billy1624
Copy link
Member

billy1624 commented Aug 29, 2021

Hi @MuhannadAlrusayni, thanks for raising this issue. Yes, we do not support custom type defined as above, however we do support Decimal, Uuid, etc. third party types.

But I do think we need to open up the API to support all custom types in general.

Can I know what is the type definition of Email? Is it a struct with multiple attributes inside it?

@tyt2y3
Copy link
Member

tyt2y3 commented Aug 29, 2021

I think you have made a good point, ValueTypeDefault seemingly is not needed now.
I've removed it in SeaQL/sea-query#114

@MuhannadAlrusayni
Copy link
Contributor Author

Can I know what is the type definition of Email? Is it a struct with multiple attributes inside it?

It's just newtype with constraint on the value in side (aka ValueObject):

#[derive(Display, SerializeDisplay, DeserializeFromStr)]
pub struct Email(String);

impl FromStr for Email {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> { .. }
}

and I use these types in my endpoints API to get user input, this way I can make sure values passed to my backend are valid data.

@MuhannadAlrusayni
Copy link
Contributor Author

MuhannadAlrusayni commented Aug 30, 2021

Thanks @tyt2y3 using SeaQL/sea-query#114 I was able to use my custom types. It would be nice if we have this functionality insea-orm as soon is you can :D.

Edit: Another things that's out of this issue scope, would it be possible to expose type_to_value macro or even better derive macro for newtypes to do the mapping? because I end up having almost an exact copy of type_to_value macro in my code base.

@tyt2y3
Copy link
Member

tyt2y3 commented Aug 31, 2021 via email

@MuhannadAlrusayni
Copy link
Contributor Author

I'm not sure yet, but I think version bump all what we need.

@tyt2y3
Copy link
Member

tyt2y3 commented Aug 31, 2021

Great. We will do so soon.

@tyt2y3
Copy link
Member

tyt2y3 commented Sep 4, 2021

Will you try 0.2.0 works for you?

@SeaQL SeaQL locked and limited conversation to collaborators Sep 9, 2021
@tyt2y3 tyt2y3 closed this as completed Sep 9, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants