Skip to content

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

Merged
jannis6023 merged 7 commits into
jannis6023:mainfrom
dshk0718:main
Dec 9, 2025
Merged

fix(Generator): 🐛 Fix the issue with mapping default values in the generated Prisma classes.#1
jannis6023 merged 7 commits into
jannis6023:mainfrom
dshk0718:main

Conversation

@dshk0718
Copy link
Copy Markdown

@dshk0718 dshk0718 commented Dec 3, 2025

This PR is also here: kimjbstar#77
However, given that the original author of the prisma-class-generator is completely AFK and a no-show on that repo (this PR has been open on that repo for 2 weeks now; and there are many other open PRs that are not even reviewed), I am attempting to merge this fix to your fork here, since your fork was already published recently as a separate prisma-class-generator NPM package on the NPM registry.

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.

Here's more detail on what this PR fixes:
@jannis6023
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 kimjbstar#34, kimjbstar#56, kimjbstar#68, and kimjbstar#76 are fixed with the fixes I am proposing to push.

kimjbstar and others added 7 commits August 12, 2024 18:28
…d 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.

Update the PostgreSQL Prisma schema for testing the default values.

Reformat the changed files.
@jannis6023
Copy link
Copy Markdown
Owner

Hi, thanks a lot for your pull request and the participation in this project. I will check the changes in the next days, as this is not a full time project for me. I am rather thinking about what is required to make this generator work with new prisma client!

Did you already try this?

@dshk0718
Copy link
Copy Markdown
Author

dshk0718 commented Dec 3, 2025

Hello, you're welcome. No, I have not tried to make this generator work with the new Prisma Client v7, if that's what you are asking. I plan on using this generator with v6 for now, as we need this fix to be put in before we can utilize this generator for our company's project.

@dshk0718
Copy link
Copy Markdown
Author

dshk0718 commented Dec 5, 2025

@jannis6023
Not to pressure you, but any plans to review, approve, and merge this PR soon?

@jannis6023
Copy link
Copy Markdown
Owner

@dshk0718 hey, i have not yet been able to look into the PR. I had to finish the prisma v7 migration first. I will push this to a new branch - already deployed on npmjs as 7.0.0!

i will check your pr on prisma v6 first and will then migrate it to v7 if there occur any changes.

@dshk0718
Copy link
Copy Markdown
Author

dshk0718 commented Dec 5, 2025

@jannis6023
Ah, that sounds good! Let me know if you need anything from me for this PR!

@dshk0718
Copy link
Copy Markdown
Author

dshk0718 commented Dec 8, 2025

@jannis6023
Just checking with you here again. Have you had the chance to review this PR?

@jannis6023 jannis6023 merged commit f5352f5 into jannis6023:main Dec 9, 2025
@jannis6023
Copy link
Copy Markdown
Owner

Hi, I just merged the pull request and release 0.6.0 to npm.
I will insanely have to set up a correct build pipeline - but don't really have enough time right now...

@dshk0718
Copy link
Copy Markdown
Author

dshk0718 commented Dec 9, 2025

Hello @jannis6023,
Thank you so much for taking your time to review and merge my PR for fixing that critical issue! I truly appreciate it.
So, speaking of which, I still see the version number 7.0.1 on the NPM registry as of now, which was what it was before you merged this PR to your fork branch. Are you saying that you've already published this PR merge to the NPM registry, even though the version number hasn't changed?

@dshk0718
Copy link
Copy Markdown
Author

dshk0718 commented Dec 18, 2025

@jannis6023
I've just noticed that the changes merged from this PR haven't been published to the NPM registry:
image
(The following screenshot shows just one of the changes that I've worked on that is not there.)
https://www.npmjs.com/package/@jannis6023/prisma-class-generator
You mentioned that the changes from this PR have been released. Is it on a different NPM package?

@dshk0718
Copy link
Copy Markdown
Author

dshk0718 commented Dec 18, 2025

@jannis6023
Oops, never mind. I think I found the 0.6.0 release, along with my changes I've pushed on this PR, that you were talking about:
image
https://www.npmjs.com/package/@jannis6023/prisma-class-generator/v/0.6.0?activeTab=code

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.

5 participants