Simon
Simon

Reputation: 389

How to update a Prisma model that has a relation "one-to-many" in Nestjs

I have this two prisma models in nestjs:

model DevInformations {
  userId      Int      @unique
  relatedUser User     @relation(fields: [userId], references: [id])
  name        String
  lastName    String
  dateOfBirth DateTime
  telNumber   String
  city        String
  zip         String
  country     String
  resume      String?
  links       Links[]
}

and

model Links {
  id          Int             @id @unique @default(autoincrement())
  userId      Int
  relatedUser DevInformations @relation(fields: [userId], references: [userId])
  name        String
  link        String
}

I have no problem with the create method but I cannot find a way to update the links field without typescript yelling at me or having nestjs errors.

I don't understand the steps to update this specific field.

For example here is my code

const devInfo = await this.prisma.devInformations.update({
      where: {
        userId,
      },
      data: {
        ...rest,
        links: {
          create: [...dto.links],
        },
      },
      include: {
        links: true,
      },
    });

And my DTO:

export class UpdateDevInfosDTO {
  @IsNumber()
  @IsNotEmpty()
  userId: number;

  ....All the fields,

  @IsArray()
  links: ILinkInput[];
}

export interface ILinkInput {
  id: number;
  name: string;
  link: string;
}

Upvotes: 0

Views: 1179

Answers (1)

Zackdev
Zackdev

Reputation: 51

What kind of errors are you getting? I managed to replicate the model with no errors, but without context of what exactly you are trying to do with links, there is little help.

*EDIT: I see you are trying to change links, but your code specifically tries to change DevInformations.

Try to change links, but specify related DevInformations instead.

Wrote this code that somewhat resembles what you're trying to do.

// Find existing links that you want to update
const existingLinks = await this.prisma.links.findMany({
        where: {
          name: { // Can change to look for devInformations instead
            in: links.map((link) => link.name),
          },
        },
      });
    
// Map their names
const existingLinkNames = existingLinks.map((link) => link.name);
const linksToCreate = links.filter(
        (link) => !existingLinkNames.includes(link.name)
      );
const linksToUpdate = links.filter((hub) =>
        existingLinkNames.includes(link.name)
      );
const createdLinks = await this.prisma.links.createMany({
        data: linksToCreate.map((link) => ({
          name: link.name,
          // Connect with devInformations
          connect: {
              userId: devInfo.userId
          }
        })),
      });

// Perform same operations to match your use case
const updatedLinks = await this.prisma.links.updateMany({
        where: {
          name: {
            in: linksToUpdate.map((link) => link.name),
          },
        },
        data: {
          description: {
            set: linksToUpdate.map((link) => link.description),
          },
        },
      });

Upvotes: 2

Related Questions