PGEnum -> Typescript Enum

Hey there. Is there any convenient way people have found to conveniently turn a pgEnum into a typescript enum such as below? Currently I'm creating DTO's but i figured coupling them would be much easier :). I figure ZOD might come into play but we are utilizing NestJS w/ class-validator atm.
const typeEnum = pgEnum('type', [
'short_text',
'long_text',
'single_choice',
'multiple_choice',
]);

export enum SurveyQuestionType {
SHORT_TEXT = 'short_text',
LONG_TEXT = 'long_text',
SINGLE_CHOICE = 'single_choice',
MULTIPLE_CHOICE = 'multiple_choice',
}
const typeEnum = pgEnum('type', [
'short_text',
'long_text',
'single_choice',
'multiple_choice',
]);

export enum SurveyQuestionType {
SHORT_TEXT = 'short_text',
LONG_TEXT = 'long_text',
SINGLE_CHOICE = 'single_choice',
MULTIPLE_CHOICE = 'multiple_choice',
}
4 Replies
Waldo
Waldo•2y ago
Hello! Did you ever find a solution for this?
Vanerac
Vanerac•2y ago
const valueMap = [
'short_text',
'long_text',
'single_choice',
'multiple_choice',
] as const

const typeEnum = pgEnum('type', valueMap);

export type SurveyQuestionType = typeof typeEnum[number]
const valueMap = [
'short_text',
'long_text',
'single_choice',
'multiple_choice',
] as const

const typeEnum = pgEnum('type', valueMap);

export type SurveyQuestionType = typeof typeEnum[number]
I'd try something like this
RaikuGg 🧑💻
Found any solution for this? Currently I am doing -
export const teamMemberStatusEnum = pgEnum('team_member_status', [
'Active',
'Inactive',
'Invited',
]);
export const TeamMemberStatus = z.enum(teamMemberStatusEnum.enumValues).Enum;
export const teamMemberStatusEnum = pgEnum('team_member_status', [
'Active',
'Inactive',
'Invited',
]);
export const TeamMemberStatus = z.enum(teamMemberStatusEnum.enumValues).Enum;
ê· ì–´
균어•14mo ago
im doing like this too
Want results from more Discord servers?
Add your server