Skip to content

Commit

Permalink
update scaffold generator to use emptyAsUndefined when its needed (#8031
Browse files Browse the repository at this point in the history
)

* 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 <tobbe@tlundberg.com>
  • Loading branch information
2 people authored and jtoar committed Apr 11, 2023
1 parent 00d853d commit 1252c12
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ model Post {
metadata Json
hugeNumber BigInt @default(9007199254740992)
favorites Favorite[]
tag Tag[]
}

model User {
Expand Down Expand Up @@ -63,4 +64,11 @@ model ExcludeDefault {
updatedTime DateTime @updatedAt
nowTime DateTime @default(now())
otherTime DateTime
}
}

model Tag {
id Int @id @default(autoincrement())
name String @unique
post Post? @relation(fields: [postId], references: [id])
postId Int?
}
Original file line number Diff line number Diff line change
@@ -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'}")
})
})
8 changes: 8 additions & 0 deletions packages/cli/src/commands/generate/scaffold/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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'
Expand Down Expand Up @@ -386,6 +392,8 @@ const modelRelatedVariables = (model) => {
values: column.enumValues || [],
isList,
isEnum,
isRequired,
isRelationalField,
}
})
const editableColumns = columns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'}` : '' %>
/>
<% } %>

Expand Down

0 comments on commit 1252c12

Please sign in to comment.