Skip to content

fix(Generator): 🐛 Fix the issue with mapping default values in the generated Prisma classes.#77

Open
dshk0718 wants to merge 22 commits into
kimjbstar:mainfrom
dshk0718:main
Open

fix(Generator): 🐛 Fix the issue with mapping default values in the generated Prisma classes.#77
dshk0718 wants to merge 22 commits into
kimjbstar:mainfrom
dshk0718:main

Conversation

@dshk0718
Copy link
Copy Markdown

@dshk0718 dshk0718 commented Nov 21, 2025

PR Changes Summary

Fix the issue with the default values for the mapped fields incorrectly being assigned to a date object like new Date('1') for Int fields (if the default value was 1 in this case) or not being assigned at all.

Fixes

Proposed Changes

  • Make the necessary fixes to fix the issues with mapping and assigning the default values in the generated Prisma classes.
  • Add a new field releasedAt to the Product model in the PostgreSQL Prisma schema for testing date fields with a fixed date-time value (not using Prisma @default(now()) directive or Prisma @updatedAt directive).
  • When checking if the default value is provided, do an actual check to validate that the default value was not undefined or null, instead of just checking on the truthiness of the default value. Checking only for the truthiness of the default value was causing the default value of 0 for the Int type fields not to get assigned the default value of 0 in the generated class.
  • Add logic to assign a default value for the fields with the default values set with the Prisma @updatedAt directive.
  • All the above changes have been tested and confirmed to be working correctly.

한국어 번역본

PR 변경 줄거리

매핑된 필드의 기본값이 Int 필드의 new Date('1')과 (기본값이 1 인 경우) 같은 날짜 객체에 잘못 할당되었거나 전혀 할당되지 않은 문제를 해결합니다.

해결하는 문제 점들

제안된 변경 사항

  • 생성된 Prisma 클래스의 매핑 및 기본값 할당 문제를 해결하기 위해 필요한 수정을 수행합니다.
  • 고정된 날짜-시간 기본값 (기본값이 Prisma @default(now()) 지시문이나 @updatedAt 지시문으로 설정되지 않은 경우) 매핑을 테스트하기 위해 PostgreSQL Prisma 스키마의 Product 모델에 새로운 날짜 필드 releaseAt을 추가합니다.
  • 기본값이 주어졌는지 확인하는 절차에서 기본값의 진실성만 (truthiness) 체크하지말고 기본값이 확실히 undefined 또는 null이 아닌지 확인하도록 조건 수정을 수행합니다. 기본값의 진실성만 체크하는 경우에는 0과 같은 값이 생성된 Prisma 클래스에 할당되지 않습니다.
  • 기본값이 Prisma @updatedAt 지시문으로 설정된 필드의 기본값이 생성된 Prisma 클래스에서 할당이 되도록 논리를 더합니다.
  • 위의 모든 변경 사항들은 테스트되었으며 모두 올바르게 정상적으로 작동하는 것으로 확인되었습니다.

@dshk0718
Copy link
Copy Markdown
Author

@kimjbstar
Just to verify it here, this is the output of the generated Prisma class with these fixes on this PR:

import { Category } from './category'
import { Company } from './company'
import { ProductType, ProductAnotherType } from '@prisma/client'
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'

export class Product {
	@ApiProperty({ type: Number })
	id: number

	@ApiProperty({ type: String })
	title: string

	@ApiProperty({ type: String })
	desc: string = 'abc'

	@ApiProperty({ type: Object })
	images: object

	@ApiPropertyOptional({ type: Boolean })
	isShown?: boolean = false

	@ApiProperty({ type: Number })
	stock: number = 0

	@ApiProperty({ enum: ProductType, enumName: 'ProductType' })
	type: ProductType

	@ApiProperty({ enum: ProductAnotherType, enumName: 'ProductAnotherType' })
	anotherType: ProductAnotherType = ProductAnotherType.AA

	@ApiPropertyOptional({ type: Number })
	averageRating?: number

	@ApiProperty({ type: Number })
	categoryId: number

	@ApiProperty({ type: Number })
	companyId: number

	@ApiProperty({ type: () => Category })
	category: Category

	@ApiProperty({ type: () => Company })
	company: Company

	@ApiProperty({ type: Date })
	releasedAt: Date = new Date('2024-01-01T00:00:00+00:00')

	@ApiProperty({ type: Date })
	createdAt: Date = new Date()

	@ApiProperty({ type: Date })
	updatedAt: Date = new Date()
}

As you can see, all the issues that were mentioned in #34, #56, #68, and #76 are fixed with the fixes I am proposing to push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants