varchar but cast to typescript enum?

I have the following table:
export const Table = pgTable('table', {
...
status: varchar('status', { length: 32 }).notNull(),
...
export const Table = pgTable('table', {
...
status: varchar('status', { length: 32 }).notNull(),
...
The type of this field will be string. I don't want to use a db enum, but I want to automatically define the type of this field as type STATUS, which is a TS enum. How would I do this?
2 Replies
Angelelz
Angelelz12mo ago
There is a $type() method:
export const Table = pgTable('table', {
...
status: varchar('status', { length: 32 }).notNull().$type<STATUS>(),
...
export const Table = pgTable('table', {
...
status: varchar('status', { length: 32 }).notNull().$type<STATUS>(),
...
Now, be careful because this won't do any runtime validation for you, this just casts the type
PGT
PGT11mo ago
@Angelelz looks like this is what i need, will try it out, thanks!
Want results from more Discord servers?
Add your server