Brakebein / prisma-generator-nestjs-dto

Generates NestJS DTO classes from Prisma Schema
Apache License 2.0
47 stars 25 forks source link

CreateDto missing null type for optional scalar fields #34

Closed nawilson closed 7 months ago

nawilson commented 10 months ago

Issue

The generated CreateDto does not include the null type for optional scalar fields.

Expected Behaviour

If a scalar field is nullable, the null type should be included in the CreateDto file.

The example below shows the middleName field being defined as optional in Prisma. Prisma treats this as automatic nullable in the database (and generates the correct type in @prisma/client).

However, the type generated in CreateDto is string instead of string | null.

Example:

schema.prisma

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

// https://github.com/Brakebein/prisma-generator-nestjs-dto
generator nestjsDto {
  provider                        = "prisma-generator-nestjs-dto"
  output                          = "../libs/shared/models/src/lib"
  outputToNestJsResourceStructure = "true"
  flatResourceStructure           = "false"
  exportRelationModifierClasses   = "true"
  reExport                        = "true"
  createDtoPrefix                 = "Create"
  updateDtoPrefix                 = "Update"
  dtoSuffix                       = "Dto"
  entityPrefix                    = ""
  entitySuffix                    = ""
  classValidation                 = "true"
  fileNamingStyle                 = "kebab"
  noDependencies                  = "false"
  outputType                      = "class"
  definiteAssignmentAssertion     = "true"
  requiredResponseApiProperty     = "true"
  prettier                        = "false"
}

model Person {
  id         Int     @id @unique @default(autoincrement())
  lastName   String  @map("last_name")
  firstName  String  @map("first_name")
  middleName String? @map("middle_name")
}

create-person.dto.ts

import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';

export class CreatePersonDto {
  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  lastName!: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  firstName!: string;

  @ApiProperty({
    required: false,
    nullable: true,
  })
  @IsOptional()
  @IsString()
  middleName?: string;
}
nawilson commented 10 months ago

See PR #27 by @zackdotcomputer

Brakebein commented 10 months ago

I finally merged this PR. New release v1.19.0 now online.