({
dbEnums,
model,
oldModel,
provider,
options,
services,
defaultSchema,
}: {
dbEnums: IntrospectedEnum[];
model: Model;
oldModel: Model;
provider: IntrospectionProvider;
services: ZModelServices;
options: PullOptions;
defaultSchema: string;
})
| 22 | import { CliError } from '../../cli-error'; |
| 23 | |
| 24 | export function syncEnums({ |
| 25 | dbEnums, |
| 26 | model, |
| 27 | oldModel, |
| 28 | provider, |
| 29 | options, |
| 30 | services, |
| 31 | defaultSchema, |
| 32 | }: { |
| 33 | dbEnums: IntrospectedEnum[]; |
| 34 | model: Model; |
| 35 | oldModel: Model; |
| 36 | provider: IntrospectionProvider; |
| 37 | services: ZModelServices; |
| 38 | options: PullOptions; |
| 39 | defaultSchema: string; |
| 40 | }) { |
| 41 | if (provider.isSupportedFeature('NativeEnum')) { |
| 42 | for (const dbEnum of dbEnums) { |
| 43 | const { modified, name } = resolveNameCasing(options.modelCasing, dbEnum.enum_type); |
| 44 | if (modified) console.log(colors.gray(`Mapping enum ${dbEnum.enum_type} to ${name}`)); |
| 45 | const factory = new EnumFactory().setName(name); |
| 46 | if (modified || options.alwaysMap) |
| 47 | factory.addAttribute((builder) => |
| 48 | builder |
| 49 | .setDecl(getAttributeRef('@@map', services)) |
| 50 | .addArg((argBuilder) => argBuilder.StringLiteral.setValue(dbEnum.enum_type)), |
| 51 | ); |
| 52 | |
| 53 | dbEnum.values.forEach((v) => { |
| 54 | const { name, modified } = resolveNameCasing(options.fieldCasing, v); |
| 55 | factory.addField((builder) => { |
| 56 | builder.setName(name); |
| 57 | if (modified || options.alwaysMap) |
| 58 | builder.addAttribute((builder) => |
| 59 | builder |
| 60 | .setDecl(getAttributeRef('@map', services)) |
| 61 | .addArg((argBuilder) => argBuilder.StringLiteral.setValue(v)), |
| 62 | ); |
| 63 | |
| 64 | return builder; |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | if (dbEnum.schema_name && dbEnum.schema_name !== '' && dbEnum.schema_name !== defaultSchema) { |
| 69 | factory.addAttribute((b) => |
| 70 | b |
| 71 | .setDecl(getAttributeRef('@@schema', services)) |
| 72 | .addArg((a) => a.StringLiteral.setValue(dbEnum.schema_name)), |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | model.declarations.push(factory.get({ $container: model })); |
| 77 | } |
| 78 | } else { |
| 79 | // For providers that don't support native enums (e.g., SQLite), carry over |
| 80 | // enum declarations from the existing schema as-is by deep-cloning the AST nodes. |
| 81 | // A dummy buildReference is used since we don't need cross-reference resolution. |
no test coverage detected