Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prisma Type not supported #2

Closed
Zyrakia opened this issue Sep 1, 2022 · 6 comments
Closed

Prisma Type not supported #2

Zyrakia opened this issue Sep 1, 2022 · 6 comments

Comments

@Zyrakia
Copy link

Zyrakia commented Sep 1, 2022

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
Copy link
Owner

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 prisma/prisma#13726), so the properties cannot be annotated.

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

@Zyrakia
Copy link
Author

Zyrakia commented Sep 2, 2022

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 prisma/prisma#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
Copy link
Owner

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
Copy link
Author

Zyrakia commented Sep 3, 2022

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

@Zyrakia Zyrakia closed this as completed Sep 3, 2022
@Zyrakia
Copy link
Author

Zyrakia commented Sep 7, 2022

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`
}

@Zyrakia Zyrakia reopened this Sep 7, 2022
Brakebein added a commit that referenced this issue Sep 9, 2022
@Brakebein
Copy link
Owner

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

@Zyrakia Zyrakia closed this as completed Sep 11, 2022
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

No branches or pull requests

2 participants