Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix enum list with default value #16

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/__tests__/__snapshots__/createMethods.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/sample.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ model User {
nullableEnum Enum?
relation UserRelation? @relation(name: "UserRelationToUser1")
relation2 UserRelation? @relation(name: "UserRelationToUser2")
enums Enum[] @default([A])
}

model User2 {
Expand Down
12 changes: 12 additions & 0 deletions src/helpers/createMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() || '[]'}`;
}
Expand Down
Loading