Skip to content
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
16 changes: 8 additions & 8 deletions packages/generator/src/prisma/field.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {singlePascal, singleCamel, pluralCamel} from "../utils/plurals"
import {singlePascal, uncapitalize, capitalize} from "../utils/plurals"
import {log} from "@blitzjs/display"

export enum FieldType {
Expand Down Expand Up @@ -57,8 +57,8 @@ export class Field {
// 'name:type?[]:attribute' => Field
static parse(input: string): Field[] {
const [_fieldName, _fieldType = "String", attribute] = input.split(":")
let fieldName = singleCamel(_fieldName)
let fieldType = singlePascal(_fieldType)
let fieldName = uncapitalize(_fieldName)
let fieldType = capitalize(_fieldType)
let isRequired = true
let isList = false
let isUpdatedAt = false
Expand All @@ -73,7 +73,7 @@ export class Field {
}
if (fieldType.includes("[]")) {
fieldType = fieldType.replace("[]", "")
fieldName = pluralCamel(fieldName)
fieldName = uncapitalize(fieldName)
isList = true
}
// use original unmodified field name in case the list handling code
Expand All @@ -82,17 +82,17 @@ export class Field {
// this field is an object type, not a scalar type
const relationType = Relation[_fieldName]
// translate the type into the name since they should stay in sync
fieldName = singleCamel(fieldType)
fieldName = uncapitalize(fieldType)
fieldType = singlePascal(fieldType)

switch (relationType) {
case Relation.hasOne:
// current model gets single `modelName ModelName` association field
// current model gets single association field
isList = false
break
case Relation.hasMany:
// current model gets single `modelNames ModelName[]` association field
fieldName = pluralCamel(fieldName)
// current model gets single association field
fieldName = uncapitalize(fieldName)
isList = true
isRequired = true
break
Expand Down
4 changes: 0 additions & 4 deletions packages/generator/src/utils/plurals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,3 @@ export function uncapitalize(input: string): string {
export const singlePascal = pipe(singular, capitalize)

export const singleCamel = pipe(singular, uncapitalize)

export const pluralPascal = pipe(plural, capitalize)

export const pluralCamel = pipe(plural, uncapitalize)
38 changes: 18 additions & 20 deletions packages/generator/test/prisma/field.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ describe("Field model", () => {
expect(Field.parse("userId: int").toString()).toMatchInlineSnapshot(`"userId int"`)
})

it("generates models for pluralized property, making pluralized name fields", () => {
expect(Field.parse("names:string").toString()).toMatchInlineSnapshot(`"names String"`)
expect(Field.parse("userIds: int").toString()).toMatchInlineSnapshot(`"userIds int"`)
})

it("serializes optional types", () => {
expect(Field.parse("name:string?").toString()).toMatchInlineSnapshot(`"name String?"`)
})

it("serializes list types, pluralizing fields", () => {
expect(Field.parse("users:int[]").toString()).toMatchInlineSnapshot(`"users Int[]"`)
expect(Field.parse("user:int[]").toString()).toMatchInlineSnapshot(`"users Int[]"`)
})

it("pluralizes lists and makes single fields singular", () => {
expect(Field.parse("name:string").toString()).toEqual(Field.parse("names:string").toString())
expect(Field.parse("userIds:string[]").toString()).toEqual(
Field.parse("userId:string[]").toString(),
)
expect(Field.parse("user:int[]").toString()).toMatchInlineSnapshot(`"user Int[]"`)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prevent field name from being converted to plural forms when creating a Field with a list type property.

})

it("appends simple attributes", () => {
Expand Down Expand Up @@ -62,21 +60,21 @@ describe("Field model", () => {

it("handles hasOne relations", () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prevent automatic conversion of field name when creating a Field with relationships, such as hasOne.

expect(Field.parse("hasOne:task").toString()).toMatchInlineSnapshot(`"task Task"`)
expect(Field.parse("hasOne:tasks").toString()).toMatchInlineSnapshot(`"task Task"`)
expect(Field.parse("hasOne:tasks").toString()).toMatchInlineSnapshot(`"tasks Task"`)
expect(Field.parse("hasOne:task?").toString()).toMatchInlineSnapshot(`"task Task?"`)
expect(Field.parse("hasOne:tasks?").toString()).toMatchInlineSnapshot(`"task Task?"`)
expect(Field.parse("hasOne:tasks?").toString()).toMatchInlineSnapshot(`"tasks Task?"`)
// list identifier should be ignored for singular relations
expect(Field.parse("hasOne:task[]").toString()).toMatchInlineSnapshot(`"task Task"`)
expect(Field.parse("hasOne:tasks[]").toString()).toMatchInlineSnapshot(`"task Task"`)
expect(Field.parse("hasOne:tasks[]").toString()).toMatchInlineSnapshot(`"tasks Task"`)
})

it("handles hasMany relations", () => {
expect(Field.parse("hasMany:task").toString()).toMatchInlineSnapshot(`"tasks Task[]"`)
expect(Field.parse("hasMany:task").toString()).toMatchInlineSnapshot(`"task Task[]"`)
expect(Field.parse("hasMany:tasks").toString()).toMatchInlineSnapshot(`"tasks Task[]"`)
expect(Field.parse("hasMany:task[]").toString()).toMatchInlineSnapshot(`"tasks Task[]"`)
expect(Field.parse("hasMany:task[]").toString()).toMatchInlineSnapshot(`"task Task[]"`)
expect(Field.parse("hasMany:tasks[]").toString()).toMatchInlineSnapshot(`"tasks Task[]"`)
// can't have optional lists, should erase optional param
expect(Field.parse("hasMany:task?").toString()).toMatchInlineSnapshot(`"tasks Task[]"`)
expect(Field.parse("hasMany:task?").toString()).toMatchInlineSnapshot(`"task Task[]"`)
})

it("handles belongsTo relations", () => {
Expand All @@ -85,21 +83,21 @@ describe("Field model", () => {
taskId Int"
`)
expect(Field.parse("belongsTo:tasks").join("\n")).toMatchInlineSnapshot(`
"task Task @relation(fields: [taskId], references: [id])
taskId Int"
"tasks Task @relation(fields: [tasksId], references: [id])
tasksId Int"
`)
expect(Field.parse("belongsTo:task?").join("\n")).toMatchInlineSnapshot(`
"task Task? @relation(fields: [taskId], references: [id])
taskId Int?"
`)
expect(Field.parse("belongsTo:tasks?").join("\n")).toMatchInlineSnapshot(`
"task Task? @relation(fields: [taskId], references: [id])
taskId Int?"
"tasks Task? @relation(fields: [tasksId], references: [id])
tasksId Int?"
`)
// ignore list directives, not a valid relation type
expect(Field.parse("belongsTo:tasks[]").join("\n")).toMatchInlineSnapshot(`
"task Task @relation(fields: [taskId], references: [id])
taskId Int"
"tasks Task @relation(fields: [tasksId], references: [id])
tasksId Int"
`)
})
})