From 1252c129681eb617c677438999cf1acfaf312b0d Mon Sep 17 00:00:00 2001 From: Sam Huang <35948805+samthuang@users.noreply.github.com> Date: Mon, 10 Apr 2023 21:26:19 +0900 Subject: [PATCH] update scaffold generator to use emptyAsUndefined when its needed (#8031) * update use scaffold generator to use emptyAsUndefined when its needed * identify that a prisma model field is a relational field In addition to checking the field name ends with 'id', checks that the field name matches one of the model's relations * test includes emptyAsUndefined on optional relation form field * isolate the use emptyAsUndefined test * Minor tweaks to the test --------- Co-authored-by: Tobbe Lundberg --- .../scaffold/__tests__/fixtures/schema.prisma | 10 +++++- .../shouldUseEmptyAsUndefined.test.js | 32 +++++++++++++++++++ .../commands/generate/scaffold/scaffold.js | 8 +++++ .../components/NameForm.tsx.template | 5 +-- 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js diff --git a/packages/cli/src/commands/generate/scaffold/__tests__/fixtures/schema.prisma b/packages/cli/src/commands/generate/scaffold/__tests__/fixtures/schema.prisma index 57a48307e67d..70fc02557b92 100644 --- a/packages/cli/src/commands/generate/scaffold/__tests__/fixtures/schema.prisma +++ b/packages/cli/src/commands/generate/scaffold/__tests__/fixtures/schema.prisma @@ -25,6 +25,7 @@ model Post { metadata Json hugeNumber BigInt @default(9007199254740992) favorites Favorite[] + tag Tag[] } model User { @@ -63,4 +64,11 @@ model ExcludeDefault { updatedTime DateTime @updatedAt nowTime DateTime @default(now()) otherTime DateTime -} \ No newline at end of file +} + +model Tag { + id Int @id @default(autoincrement()) + name String @unique + post Post? @relation(fields: [postId], references: [id]) + postId Int? +} diff --git a/packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js b/packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js new file mode 100644 index 000000000000..4f75b74123ed --- /dev/null +++ b/packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js @@ -0,0 +1,32 @@ +globalThis.__dirname = __dirname +import path from 'path' + +// Load mocks +import '../../../../lib/test' + +import { getDefaultArgs } from '../../../../lib' +import { yargsDefaults as defaults } from '../../helpers' +import * as scaffold from '../scaffold' + +jest.mock('execa') + +describe('relational form field', () => { + let form + + beforeAll(async () => { + const files = await scaffold.files({ + ...getDefaultArgs(defaults), + model: 'Tag', + tests: true, + nestScaffoldByModel: true, + }) + + const tagFormPath = + '/path/to/project/web/src/components/Tag/TagForm/TagForm.js' + form = files[path.normalize(tagFormPath)] + }) + + test("includes optional relational fields with an emptyAs('undefined')", () => { + expect(form).toMatch("emptyAs={'undefined'}") + }) +}) diff --git a/packages/cli/src/commands/generate/scaffold/scaffold.js b/packages/cli/src/commands/generate/scaffold/scaffold.js index 899a1868f0b3..14f357e9910b 100644 --- a/packages/cli/src/commands/generate/scaffold/scaffold.js +++ b/packages/cli/src/commands/generate/scaffold/scaffold.js @@ -344,6 +344,8 @@ const modelRelatedVariables = (model) => { }, } + const relations = relationsForModel(model).map((relation) => relation) + const columns = model.fields .filter((field) => field.kind !== 'object') .map((column) => { @@ -359,6 +361,10 @@ const modelRelatedVariables = (model) => { : null } + const isRelationalField = + column.name.endsWith('Id') && + relations.some((relation) => column.name.includes(relation)) + const isRequired = column.isRequired const isEnum = column.kind === 'enum' const isList = column.isList const enumType = isEnum && isList ? 'EnumList' : 'Enum' @@ -386,6 +392,8 @@ const modelRelatedVariables = (model) => { values: column.enumValues || [], isList, isEnum, + isRequired, + isRelationalField, } }) const editableColumns = columns diff --git a/packages/cli/src/commands/generate/scaffold/templates/components/NameForm.tsx.template b/packages/cli/src/commands/generate/scaffold/templates/components/NameForm.tsx.template index b95328af1895..89aca4e16a74 100644 --- a/packages/cli/src/commands/generate/scaffold/templates/components/NameForm.tsx.template +++ b/packages/cli/src/commands/generate/scaffold/templates/components/NameForm.tsx.template @@ -100,8 +100,9 @@ const ${singularPascalName}Form = (props: ${singularPascalName}FormProps) => { name="${column.name}" ${column.defaultProp}={${column.deserializeFunction && column.deserializeFunction + '('}props.${singularCamelName}?.<%= column.name %>${column.deserializeFunction && ')'}} className="rw-input" - errorClassName="rw-input rw-input-error"<%= !column.validation ? '' : ` - validation=${column.validation}` %> + errorClassName="rw-input rw-input-error" + <%= !column.validation ? '' : `validation=${column.validation}` %> + <%= column.isRelationalField && !column.isRequired ? `emptyAs={'undefined'}` : '' %> /> <% } %>