kimjbstar / prisma-class-generator

Class generator from Prisma schema.
172 stars 49 forks source link

Class fields generated from the schema should be nullable instead of optional #47

Closed ricayanzon closed 1 year ago

ricayanzon commented 1 year ago

Expected Behavior

When a model inside the Prisma schema has a field that is defined as nullable (marked with the ?, that field inside the generated class should also be nullable.

Actual Behavior

The generator marks these nullable fields as optional (can be undefined) instead of nullable.

Steps to Reproduce the Problem

  1. Create a model within the schema.prisma file with a field that is nullable, e.g. model Foo { bar String? }
  2. Run npx prisma generate
  3. Check the resulting class file. For the example, it should look like this: export class Foo { bar?: string } and I'd expect it to be export class Foo { bar: string | null }

Is this just me or what's the idea behind this?

joao-moonward commented 1 year ago

I need this feature. I can't get the correct type that is generated by Prisma inclusion and Prisma class generator Here is my example:

class UserDto extends IntersectionType(
    PrismaModel.User,
    PickType(PrismaModel.UserRelations, ['address','documents']),
  )
  implements
    Prisma.UserGetPayload<{
      include: typeof UserDto.include;
    }>{

 static get include() {
    return Prisma.validator<Prisma.UserInclude>()({
      address: true,
      documents: true,
    })
 }
}

When address field in UserGetPayload is null, a type error occurs because undefined values generated by the library are not assignable to the type.

[EDIT] I wrote a solution locally, tested it and it worked. Can I request a PR?

kimjbstar commented 1 year ago

fixed by #50

ricayanzon commented 11 months ago

Thanks! But it seems that the npm package is behind the current version here - can you publish the new version in npm please? @kimjbstar