Any easy way to create a typescript ENUM type from pgEnum?
Prisma used to auto-gen all the enum types as well which are heavily used in our application.
12 Replies
Drizzle generates union type of strings, that we used in enums. Same for introspect. It generates schema file with enums, that were used on existing database
Any examples why you want to use
enum
instead of 'foo' | 'bar'
?We usually pass enums into zod.nativeEnum() for validation
I guess we could use z.enum, but it'd still be nice to export enum types easily. is there any way I can convert pgEnum to an Enum type?
yeah, I guess you can just change
nativeEnum
to enum
to check and get type
we can provide helper for that, so you could generate type from string union. But it will be basically same as using zod
any thoughts here @bloberenober @alexblokh ?Enums are runtime values and unions are types, so it's not possible to generate enums from unions, only the other way around
We can think about providing better enums support - we don't use them in our work projects so we didn't plan the ORM around them
I think its a common use case - but yeah I can manually support it for now
thanks!
Tracking issue - https://github.com/drizzle-team/drizzle-orm/issues/332
sorry for intruding, is this the right way?
pgEnum
doesn't accept array as const 🧐well...
I'm not sure how it can be worked around for now, maybe with some type casting
I definitely need to add support for readonly arrays to enums as the quickest fix
@Raphaël M (@rphlmr) ⚡ : this is the work around we have, you might have to create helpers for integer enums.
Is there any update on getting typescript types for enums from Drizzle?
Btw @rushil1o1 here's another way to get the values type of a pgEnum, by inspecting the table types instead of using a helper like
strEnum
: https://discord.com/channels/1043890932593987624/1138161535416553585/1148743897690931400