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

How to use @DtoRelationIncludeId for creating an object with reference connect? #59

Open
Staijn1 opened this issue Oct 27, 2024 · 1 comment

Comments

@Staijn1
Copy link

Staijn1 commented Oct 27, 2024

Hello,

I'm currently re-writing an existing API and need to maintain backwards compatibility for my DTOs to minimize front-end changes.

In my database schema, I have two models: Type and TypeGroup. The Type model references a parent TypeGroup as shown in my Prisma schema:

model TypeGroup {
  /// @DtoCreateOptional
  /// @DtoUpdateOptional
  ID          Int       @id @default(autoincrement())

  /// @maxLength 75
  /// @IsNotEmpty()
  DESCRIPTION String    @db.VarChar(75)

  Types       Type[]
}

model Type {
  /// @DtoCreateOptional
  /// @DtoUpdateOptional
  ID                Int       @id @default(autoincrement())

  /// @DtoRelationIncludeId
  TYPEGROUP_ID      Int
  TypeGroup TypeGroup @relation(fields: [TYPEGROUP_ID], references: [ID])

  /// @maxLength 75
  /// @IsNotEmpty()
  DESCRIPTION String    @db.VarChar(75)
}

For backwards compatibility, I would like the Type creation JSON input to look like this:

{
   "DESCRIPTION": "string",
   "TYPEGROUP_ID": 1
}

However, this format is incompatible with the generated Prisma.TypeCreateInput object because TypeGroup requires the inclusion of an object with create, connectOrCreate, or connect options.

When I add the @DtoRelationCanConnectOnCreate annotation to the TypeGroup reference within the Type model, I then need to provide the following JSON structure:

{
   "DESCRIPTION": "string",
   "TYPEGROUP_ID": 1,
   "TypeGroup": {
      "connect": {
         "TYPEGROUP_ID": 1
      }
   }
}

This is more verbose than desired and is incompatible with the existing front-end structure.
Is there a way to configure the decorators in the Prisma schema so my desired JSON format is outputted, whilst passing the correct object format to Prisma (including the connect property) automatically?

I'm currently transforming the DTO object manually before passing it to the service.

Any help is much appreciated

@Staijn1 Staijn1 changed the title How to use @DtoRelationIncludeId for creating object with reference connect? How to use @DtoRelationIncludeId for creating an object with reference connect? Oct 27, 2024
@Brakebein
Copy link
Owner

All relation fields (including their id fields) are omitted by default and need to be made explicitly visible by respective annotations. When I forked this generator, only annotations like @DtoRelationCanConnectOnCreate where available that was, however, in most cases too verbose for me, like in your case. I also just wanted to pass the id. That's why I introduced the @DtoRelationIncludeId annotation. However, it does not automatically map it to Prisma.TypeCreateInput (i.e., { connect: { id: body.TYPEGROUP_ID } }).

Your Prisma schema is already correct. It should generate something like this:

class CreateTypeDto {
  ID?: string;
  TYPEGROUP_ID?: string | null;
  DESCRIPTION: string;
}

In your backend service, it would need to be used as follows:

class TypeService {
  ...
  async create(body: CreateTypeDto) {
    return this.prisma.type.create({
      ID: body.ID,
      TypeGroup: body.TYPEGROUP_ID
        ? { connect: { ID: body.TYPEGROUP_ID } }
        : undefined,
      DESCRIPTION: body.DESCRIPTION,
    });
  }
}

At least that's how we use it in our projects. So, you can't just pass the Dto directly, but need to handle it respectively in your backend service. But it's cleaner for the frontend.

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