From aba2fbbae1b2ea3c614f49bf769e806741d89661 Mon Sep 17 00:00:00 2001 From: Justin Boyson Date: Wed, 15 Nov 2023 11:28:12 -0600 Subject: [PATCH 1/2] fix enum list with default value --- src/helpers/createMethods.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/helpers/createMethods.ts b/src/helpers/createMethods.ts index 9bbf530..b4a1cab 100644 --- a/src/helpers/createMethods.ts +++ b/src/helpers/createMethods.ts @@ -20,6 +20,18 @@ function getFieldDefinition( }`; } if (field.hasDefaultValue) { + if (field.isList && field.kind === 'enum') { + const enumName = field.type; + const enumValues = enums.find((it) => it.name === enumName)?.values || []; + if (enumValues.length === 0) { + logger.warn( + `Enum ${enumName} has no enum values. Field ${field.name} won't be generated.`, + ); + } + else { + return `${field.name}: [${enumName}.${field.default}]`; + } + } if (field.isList) { return `${field.name}: ${field.default?.toString() || '[]'}`; } From 8743766a2c1e832f9dce694ed47cc62d40764d1f Mon Sep 17 00:00:00 2001 From: Justin Boyson Date: Wed, 15 Nov 2023 11:59:02 -0600 Subject: [PATCH 2/2] update prisma sample & snapshot --- src/__tests__/__snapshots__/createMethods.test.ts.snap | 5 +++++ src/__tests__/sample.prisma | 1 + 2 files changed, 6 insertions(+) diff --git a/src/__tests__/__snapshots__/createMethods.test.ts.snap b/src/__tests__/__snapshots__/createMethods.test.ts.snap index aeae45f..7bd32e0 100644 --- a/src/__tests__/__snapshots__/createMethods.test.ts.snap +++ b/src/__tests__/__snapshots__/createMethods.test.ts.snap @@ -76,6 +76,7 @@ export function fakeUserComplete() { enum: faker.helpers.arrayElement([Enum.A, Enum.B, Enum.C] as const), enumWithDefault: Enum.A, nullableEnum: null, + enums: [Enum.A], }; } export function fakeUser2Complete() { @@ -175,6 +176,7 @@ export function fakeUserComplete() { enum: faker.helpers.arrayElement([Enum.A, Enum.B, Enum.C] as const), enumWithDefault: Enum.A, nullableEnum: undefined, + enums: [Enum.A], }; } export function fakeUser2Complete() { @@ -274,6 +276,7 @@ export function fakeUserComplete() { enum: faker.helpers.arrayElement([Enum.A, Enum.B, Enum.C] as const), enumWithDefault: Enum.A, nullableEnum: undefined, + enums: [Enum.A], }; } export function fakeUser2Complete() { @@ -373,6 +376,7 @@ export function fakeUserComplete() { enum: faker.helpers.arrayElement([Enum.A, Enum.B, Enum.C] as const), enumWithDefault: Enum.A, nullableEnum: undefined, + enums: [Enum.A], }; } export function fakeUser2Complete() { @@ -472,6 +476,7 @@ export function fakeUserComplete() { enum: faker.helpers.arrayElement([Enum.A, Enum.B, Enum.C] as const), enumWithDefault: Enum.A, nullableEnum: undefined, + enums: [Enum.A], }; } export function fakeUser2Complete() { diff --git a/src/__tests__/sample.prisma b/src/__tests__/sample.prisma index 6272866..8b9b8ac 100644 --- a/src/__tests__/sample.prisma +++ b/src/__tests__/sample.prisma @@ -46,6 +46,7 @@ model User { nullableEnum Enum? relation UserRelation? @relation(name: "UserRelationToUser1") relation2 UserRelation? @relation(name: "UserRelationToUser2") + enums Enum[] @default([A]) } model User2 {