Brakebein / prisma-generator-nestjs-dto

Generates NestJS DTO classes from Prisma Schema
Apache License 2.0
45 stars 24 forks source link

Prisma `Type` not supported #2

Closed Zyrakia closed 2 years ago

Zyrakia commented 2 years ago

Hey, this project has been a lifesaver for maintaining a source of truth for my project, but I have recently ran into errors while using the Type keyword in my Prisma schema. This is used to model embedded documents within a MongoDB based schema.

I get this error, probably because it expects a Model and not a Type: "Error: related model 'Variation' for 'Product.variations' not found"

type Image {
    url    String
    width  Int
    height Int
}

type Variation {
    name        String
    description String?
    price       Float?
    image       Image
}

model Product {
    id          String      @id @default(auto()) @map("_id") @db.ObjectId
    name        String
    description String
    price       Float?
    variations  Variation[]
    images      Image[]
}

model FeaturedProduct {
    id          String      @id @default(auto()) @map("_id") @db.ObjectId
    name        String
    description String
    price       Float?
    variations  Variation[]
    images      Image[]
}
Brakebein commented 2 years ago

I have recently implemented support of composite types, but I haven't published it yet, because it isn't yet complete regarding class validation. And triple-slash comments aren't parsed by the underlying prisma parser (see https://github.com/prisma/prisma/issues/13726), so the properties cannot be annotated.

But the basics do work, so I could release a beta version which you can test.

Zyrakia commented 2 years ago

I have recently implemented support of composite types, but I haven't published it yet, because it isn't yet complete regarding class validation. And triple-slash comments aren't parsed by the underlying prisma parser (see https://github.com/prisma/prisma/issues/13726), so the properties cannot be annotated.

But the basics do work, so I could release a beta version which you can test.

Absolutely, that would be amazing. Also, on a separate note, maybe I can open an issue for this, or even open a PR, but is there any way to disable swagger generation / disable generation of entities?

I could definitely browse through the code base and see if I can implement it myself thought.

Brakebein commented 2 years ago

I just did some improvements regarding the composite types and just released beta version, which you can check out: https://www.npmjs.com/package/@brakebein/prisma-generator-nestjs-dto/v/1.13.0-beta.0

You can set noDependencies = "false" which disables any imports from NestJs or Prisma including swagger decorators. But if you rely on some special imports (like Prisma.InputJsonValue), this may also be missing then.

Zyrakia commented 2 years ago

It's working wonders so far, thanks so much. I'll let you know if I run into any issues.

Zyrakia commented 2 years ago

I've been using it for a few days now, and so far it's worked well, besides one issue.

I have a route to update a product in my backend, this takes the body parameter UpdateProductDto, which is validated by the validation pipeline with the generated class-validator decorators. This works wonders, besides the fact that the variations type inside the UpdateProductDto validation class is UpdateVariationDto, but with this route, I want the value of the variations to completely replace the old one, not edit indices, so I would need each of the array items to be CreateVariationDto. Is there any way to achieve this?

To clarify, I want to be able to turn this:

@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => UpdateVariationDto)
variations?: UpdateVariationDto[];

into this, since I need each array item to be a full variation entry, not a partial one:

@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => CreateVariationDto)
variations?: CreateVariationDto[];

Would this be possible with a triple-slash decorator on the field in the Prisma schema?

model Product {
    id          String      @id @default(auto()) @map("_id") @db.ObjectId
    name        String
    description String
    price       Float?
    /// @NoPartialUpdate (or something of the sorts)
    variations  Variation[]
    images      Image[] // This property has the same issue, all the indices are validated by `UpdateImageDto`
}
Brakebein commented 2 years ago

I added the annotation @DtoTypeFullUpdate for this use case. New release: https://www.npmjs.com/package/@brakebein/prisma-generator-nestjs-dto/v/1.13.0-beta.1