From bf9d73de6ec7415b4ff915752559254a72744cda Mon Sep 17 00:00:00 2001 From: Sam Huang <35948805+samthuang@users.noreply.github.com> Date: Sat, 8 Apr 2023 00:12:09 +0900 Subject: [PATCH 1/6] update use scaffold generator to use emptyAsUndefined when its needed --- .../scaffold/templates/components/NameForm.tsx.template | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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'}` : '' %> /> <% } %> From dda5d66746833186cc6bd59f6959927f3b493433 Mon Sep 17 00:00:00 2001 From: Sam Huang <35948805+samthuang@users.noreply.github.com> Date: Sun, 9 Apr 2023 19:42:26 +0900 Subject: [PATCH 2/6] 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 --- packages/cli/src/commands/generate/scaffold/scaffold.js | 8 ++++++++ 1 file changed, 8 insertions(+) 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 From f2cddf2ba20b0eeebc50cba9926b0798030bc96e Mon Sep 17 00:00:00 2001 From: Sam Huang <35948805+samthuang@users.noreply.github.com> Date: Sun, 9 Apr 2023 22:00:31 +0900 Subject: [PATCH 3/6] test includes emptyAsUndefined on optional relation form field --- .../generate/scaffold/__tests__/editableColumns.test.js | 4 ++++ .../generate/scaffold/__tests__/fixtures/schema.prisma | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/commands/generate/scaffold/__tests__/editableColumns.test.js b/packages/cli/src/commands/generate/scaffold/__tests__/editableColumns.test.js index 5b90bdfcde2a..abdd0a567984 100644 --- a/packages/cli/src/commands/generate/scaffold/__tests__/editableColumns.test.js +++ b/packages/cli/src/commands/generate/scaffold/__tests__/editableColumns.test.js @@ -45,6 +45,10 @@ describe('editable columns', () => { expect(form).toMatch('name="number"') }) + test("includes optional relational fields with an emptyAs('undefined')", async () => { + expect(form).toMatch("emptyAs={'undefined'}") + }) + test('includes DateTime fields', async () => { expect(form).toMatch('name="otherTime"') }) 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..35f6211271ae 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[] + exclude ExcludeDefault[] } model User { @@ -63,4 +64,6 @@ model ExcludeDefault { updatedTime DateTime @updatedAt nowTime DateTime @default(now()) otherTime DateTime -} \ No newline at end of file + post Post? @relation(fields: [postId], references: [id]) + postId Int? +} From 2f900985e9cb8e6c80e9956e00a649ab6363ca02 Mon Sep 17 00:00:00 2001 From: Sam Huang <35948805+samthuang@users.noreply.github.com> Date: Mon, 10 Apr 2023 11:14:41 +0900 Subject: [PATCH 4/6] isolate the use emptyAsUndefined test --- .../__tests__/editableColumns.test.js | 4 --- .../scaffold/__tests__/fixtures/schema.prisma | 11 ++++-- .../shouldUseEmptyAsUndefined.test.js | 35 +++++++++++++++++++ 3 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js diff --git a/packages/cli/src/commands/generate/scaffold/__tests__/editableColumns.test.js b/packages/cli/src/commands/generate/scaffold/__tests__/editableColumns.test.js index abdd0a567984..5b90bdfcde2a 100644 --- a/packages/cli/src/commands/generate/scaffold/__tests__/editableColumns.test.js +++ b/packages/cli/src/commands/generate/scaffold/__tests__/editableColumns.test.js @@ -45,10 +45,6 @@ describe('editable columns', () => { expect(form).toMatch('name="number"') }) - test("includes optional relational fields with an emptyAs('undefined')", async () => { - expect(form).toMatch("emptyAs={'undefined'}") - }) - test('includes DateTime fields', async () => { expect(form).toMatch('name="otherTime"') }) 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 35f6211271ae..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,7 +25,7 @@ model Post { metadata Json hugeNumber BigInt @default(9007199254740992) favorites Favorite[] - exclude ExcludeDefault[] + tag Tag[] } model User { @@ -64,6 +64,11 @@ model ExcludeDefault { updatedTime DateTime @updatedAt nowTime DateTime @default(now()) otherTime DateTime - post Post? @relation(fields: [postId], references: [id]) - postId Int? +} + +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..79eb4821b0ed --- /dev/null +++ b/packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js @@ -0,0 +1,35 @@ +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 files + let form + + beforeAll(async () => { + files = await scaffold.files({ + ...getDefaultArgs(defaults), + model: 'Tag', + tests: true, + nestScaffoldByModel: true, + }) + form = + files[ + path.normalize( + '/path/to/project/web/src/components/Tag/TagForm/TagForm.js' + ) + ] + }) + + test("includes optional relational fields with an emptyAs('undefined')", async () => { + expect(form).toMatch("emptyAs={'undefined'}") + }) +}) From 5ce69193f487cce597fc1c879f11e7866f47f474 Mon Sep 17 00:00:00 2001 From: Tobbe Lundberg Date: Mon, 10 Apr 2023 07:17:35 +0200 Subject: [PATCH 5/6] Minor tweaks to the test --- .../__tests__/shouldUseEmptyAsUndefined.test.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js b/packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js index 79eb4821b0ed..9ad901c23fa6 100644 --- a/packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js +++ b/packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js @@ -11,25 +11,22 @@ import * as scaffold from '../scaffold' jest.mock('execa') describe('relational form field', () => { - let files let form beforeAll(async () => { - files = await scaffold.files({ + const files = await scaffold.files({ ...getDefaultArgs(defaults), model: 'Tag', tests: true, nestScaffoldByModel: true, }) - form = - files[ - path.normalize( - '/path/to/project/web/src/components/Tag/TagForm/TagForm.js' - ) - ] + + 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')", async () => { - expect(form).toMatch("emptyAs={'undefined'}") + test("includes optional relational fields with an emptyAs('undefined')", () => { + expect(form).toMatch("emptyAs={'Undefined'}") }) }) From 8d76da1a252683ca7802b649ab42dd0c75e789ed Mon Sep 17 00:00:00 2001 From: Tobbe Lundberg Date: Mon, 10 Apr 2023 07:38:42 +0200 Subject: [PATCH 6/6] fix test --- .../scaffold/__tests__/shouldUseEmptyAsUndefined.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js b/packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js index 9ad901c23fa6..4f75b74123ed 100644 --- a/packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js +++ b/packages/cli/src/commands/generate/scaffold/__tests__/shouldUseEmptyAsUndefined.test.js @@ -27,6 +27,6 @@ describe('relational form field', () => { }) test("includes optional relational fields with an emptyAs('undefined')", () => { - expect(form).toMatch("emptyAs={'Undefined'}") + expect(form).toMatch("emptyAs={'undefined'}") }) })