Hi everyone, i need help with custom typings. Is t...
# prisma-client
k
Hi everyone, i need help with custom typings. Is there a recommended way of narrowing the generated model types? Here’s what it would look like:
Copy code
model Member {
  id    Int    @id @default(autoincrement())
  email Email
}
the
Email
type would be coming from typescript. If modifying the schema like above is not possible/recommended, is it at least possible to have this type appear on the generated client types? i’d want all prisma methods on the
Member
type to expect the email field to be of type
Email
, not just
string
r
Prisma's schema's main job needs to understand how to store and read the data in the database. how you handle the type, should be done with your types and resolver - there you can return what type you need. How do you intend to store this 'email' property in the database? as a string? as a json object? when you have something like: model user { id int  userDetailStuff UserProfile } the 'userDetailStuff' is not stored in the database - instead it's just a link to the other table - but prisma needs to understand how those links are made
k
@Robert Witchell i’ll just store it as a string, but i’ll force it to be a verified email. I like using
newtype-ts
to create types for validated domain values. in this case, any variable of type
Email
will be a validated string that is structured as an email. If possible, i’d like to plug that into the generated types for my prisma models where it’ll be impossible to pass a simple string for the email field. This way whenever I’m interacting with my db models (either reading or writing), i will have the guarantee that it’ll be impossible to write a non-verified email string. does that make sense?
plus whenever i’m reading email fields, i’d see that they are of type
Email
as opposed to just
string
. This will let me write my code in a way that leverages the type system and help me understand which values are validated and not.
r
yep, so it should be
email string
Not sure how you can handle this in your resolver, as it's returning a json array, but you could resolve the email property like this, which would be similar to a datetime being resolved (i think)
email: (parent, args, context, info) { return new YourEmailObjectConstructor(parent.email) }