Make varchar field one of string union

I know that CHECK constraint hasn't been implemented yet, but I want to implement a Role field that is a varchar that can only be set to "user", "paid-user", or "admin" and I want the schema definition in TS to reflect this. I know this wont set the constraint at the DB level, but I want to TS definition to reflect that only the above union of strings are valid. How can I tell drizzle that this field should be this specific TS type?
role: varchar("role", { length: 50 }).notNull(), // user|paid-user|admin
role: varchar("role", { length: 50 }).notNull(), // user|paid-user|admin
4 Replies
megamindat
megamindat8mo ago
Drizzle ORM - Overview
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
⚡Z.E.U.S⚡
⚡Z.E.U.S⚡8mo ago
Yes, you can add enum field for varchar, but it won’t throw an error(only types) when you inserting if the value is wrong. https://orm.drizzle.team/docs/column-types/pg#varchar Or you can create pgEnum/mysqlEnum https://orm.drizzle.team/docs/column-types/pg#enum https://orm.drizzle.team/docs/column-types/mysql#enumcolumn
Drizzle ORM - PostgreSQL column types
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Drizzle ORM - MySQL column types
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
TJBlackman
TJBlackmanOP8mo ago
Thank you both! I am choosing to use the pgEnum feature. An enum is saved in the DB as an integer, correct? I'm using postgres...
megamindat
megamindat8mo ago
That's saved as a string.
Want results from more Discord servers?
Add your server