Making a enum/union type like in prisma, but with drizzle

I have this a role pgEnum and I want to make a enum type that is also a union type, like what happens in prisma when you make a enum
export const role = pgEnum("role", ["USER", "ADMIN"]);

// This is what i'm currently using
export enum Role {
user = "USER",
admin = "ADMIN"
}
export const role = pgEnum("role", ["USER", "ADMIN"]);

// This is what i'm currently using
export enum Role {
user = "USER",
admin = "ADMIN"
}
With prisma I would be able to do this on the schema
enum Role {
USER,
ADMIN
}
enum Role {
USER,
ADMIN
}
and use it like this inside my typescript
import { Role } from "@prisma/client";

// This has autocomplete functioning perfectly
const firstExample: Role = "USER";
const secondExample: Role = "ADMIN";
const thirdExample: Role = Role.USER;
const fourthExample: Role = Role.ADMIN
import { Role } from "@prisma/client";

// This has autocomplete functioning perfectly
const firstExample: Role = "USER";
const secondExample: Role = "ADMIN";
const thirdExample: Role = Role.USER;
const fourthExample: Role = Role.ADMIN
6 Replies
Perry
PerryOP•2y ago
The best solution I've found so far is:
export enum RoleEnum {
user = "USER",
admin = "ADMIN"
}
export type Role = `${RoleEnum}`;
export enum RoleEnum {
user = "USER",
admin = "ADMIN"
}
export type Role = `${RoleEnum}`;
But if I wanted to use the enum version I would need to import RoleEnum, which is something i don't need to do with prisma. I don't need to, but I also want to find a way to use something like InferModel on my pgEnum to make the enum but I don't know exactly how the PgEnum type works
Liam
Liam•2y ago
Hey did you wind up figuring out how to do this?
Perry
PerryOP•2y ago
For now I just have a union I will ping you when I get time to get the exact type @lermatroid this is what i'm using for now
export type Role = (typeof role.enumValues)[number];
export type Role = (typeof role.enumValues)[number];
Liam
Liam•2y ago
Thanks!
Perry
PerryOP•2y ago
No problem!
Benja
Benja•4w ago
More than one year later, and you saved me here 🚀
Want results from more Discord servers?
Add your server