drizzle enums #1914
Replies: 7 comments 17 replies
-
Hey @cehenriques! I agree this would be nice. Gave you an upvote 🚀 For now, you can use two approaches: Approach 1: the
|
Beta Was this translation helpful? Give feedback.
-
I don't use Zod, so I wanted to share what I've done before in a project. |
Beta Was this translation helpful? Give feedback.
-
Hello ! // Takes an enum and return an array with each values
export function enumToPgEnum(myEnum: any): [string, ...string[]] {
return Object.values(myEnum).map((value: any) => `${value}`) as [string, ...string[]]
}
// Define an enum
export enum Role {
APPLICANT = 'applicant',
TRAINER = 'trainer',
ADMIN = 'admin',
}
// Declare drizzle enum
export const roleEnum = pgEnum('role', enumToPgEnum(Role)) I didn't use it a lot so i'm not sure this technique is perfect but it seems working for my current needs. |
Beta Was this translation helpful? Give feedback.
-
If you're just looking for the typings so you can reuse them elsewhere, you can achieve this without any extra dependencies. Use the following as an example: const onboardingSteps = ["welcome", "setup", "complete"] as const;
type OnboardingStep = typeof onboardingSteps[number];
const onboardingStepEnum = pgEnum("onboarding_step", onboardingSteps); |
Beta Was this translation helpful? Give feedback.
-
A simple work around that I found: enum eMyEnum {
Value1 = 'Value1',
Value2 = 'Value2'
}
export const myTable = mysqlTable("my_table", {
// ...other fields
myField: mysqlEnum(
"my_field",
Object.values(eCategoryType) as [eCategoryType, ...eCategoryType[]]
)
}); This will also keep the type so later when you query the table, const values = await db.query.myTable.findMany();
values.forEach(value => {
value.myField // will be of type eMyEnum
}); The reason for this is because |
Beta Was this translation helpful? Give feedback.
-
Not trying to be harsh, but I think the Drizzle team is missing the point. What we expect from a TypeScript ORM is that it provides the types we are working with. Prisma and TypeORM offer this out of the box, while Drizzle expects us to infer types or even pre-create them ourselves to do some additional work for them to be used by Drizzle. |
Beta Was this translation helpful? Give feedback.
-
If it's ok to use string unions instead of enums, here's my approach:
|
Beta Was this translation helpful? Give feedback.
-
Hi.
I just started recently using drizzle coming from Prisma, and I'm trying to convert one of my projects to drizzle.
One thing I found less intuitive is how Drizzle manages enums.
For instance, in Prisma, I would create an enum on my schema like this:
And then I could use it normally like this in my project:
But in drizzle, we create an enum as a PgEnum:
But then to access its values I can only do it like so:
I need to rely on the editor's intellisense
to remind me that "[0]" is "Red", while on Prisma's approach, I can use a traditional TS enum and directly access its values.
Am I doing something wrong? Is there a better or alternative way to use the enums?
What approach do you guys use? Do you just create an actual enum to reference it throughout your project?
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions