How to use custom types inside Model? #139
Replies: 8 comments
-
Hi @MuhannadAlrusayni, thanks for raising this issue. Yes, we do not support custom type defined as above, however we do support 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 |
Beta Was this translation helpful? Give feedback.
-
I think you have made a good point, |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
Thanks @tyt2y3 using SeaQL/sea-query#114 I was able to use my custom types. It would be nice if we have this functionality in Edit: Another things that's out of this issue scope, would it be possible to expose |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I'm not sure yet, but I think version bump all what we need. |
Beta Was this translation helpful? Give feedback.
-
Great. We will do so soon. |
Beta Was this translation helpful? Give feedback.
-
Will you try |
Beta Was this translation helpful? Give feedback.
-
Hi, I want to use my custom types in the Model:
There are a few issues I faced while I'm trying to do so:
ValueType
for my custom types because it requiresValueTypeDefault
that's not implemented for all of my custom types (e.g. you cannot have default impl forEmail
)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 exampleTryFrom<Value> for Email
orFromSqlType
trait?Another thing that I notice is, do we really need
ValueTypeDefault
? why not just useDefault
trait since it's already bounded: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
Beta Was this translation helpful? Give feedback.
All reactions