| 6 | import type { Generable } from './Generable' |
| 7 | |
| 8 | export class Enum implements Generable { |
| 9 | constructor( |
| 10 | protected readonly type: DMMF.SchemaEnum, |
| 11 | protected readonly useNamespace: boolean, |
| 12 | ) {} |
| 13 | |
| 14 | private isObjectEnum(): boolean { |
| 15 | return this.useNamespace && objectEnumNames.includes(this.type.name) |
| 16 | } |
| 17 | |
| 18 | private isStrictEnum(): boolean { |
| 19 | return this.useNamespace && strictEnumNames.includes(this.type.name) |
| 20 | } |
| 21 | |
| 22 | public toJS(): string { |
| 23 | const { type } = this |
| 24 | const enumVariants = `{ |
| 25 | ${indent(type.values.map((v) => `${v}: ${this.getValueJS(v)}`).join(',\n'), TAB_SIZE)} |
| 26 | }` |
| 27 | const enumBody = this.isStrictEnum() ? `makeStrictEnum(${enumVariants})` : enumVariants |
| 28 | |
| 29 | return this.useNamespace |
| 30 | ? `exports.Prisma.${type.name} = ${enumBody};` |
| 31 | : `exports.${type.name} = exports.$Enums.${type.name} = ${enumBody};` |
| 32 | } |
| 33 | |
| 34 | private getValueJS(value: string): string { |
| 35 | return this.isObjectEnum() ? `Prisma.${value}` : `'${value}'` |
| 36 | } |
| 37 | |
| 38 | public toTS(): string { |
| 39 | const { type } = this |
| 40 | |
| 41 | return `export const ${type.name}: { |
| 42 | ${indent(type.values.map((v) => `${v}: ${this.getValueTS(v)}`).join(',\n'), TAB_SIZE)} |
| 43 | }; |
| 44 | |
| 45 | export type ${type.name} = (typeof ${type.name})[keyof typeof ${type.name}]\n` |
| 46 | } |
| 47 | |
| 48 | private getValueTS(value: string): string { |
| 49 | return this.isObjectEnum() ? `typeof ${value}` : `'${value}'` |
| 50 | } |
| 51 | } |
nothing calls this directly
no outgoing calls
no test coverage detected