Search code examples
node.jsexpressprisma

Update parent and upsert child in Prisma


I have a parent-child relation like below:

model User {
  id               Int                @id @default(autoincrement())
  authId           String             @unique @default("N/A") @map("auth_id") @db.VarChar(128)
  email            String             @unique @db.VarChar(256)
  firstName        String             @map("first_name") @db.VarChar(64)
  lastName         String             @map("last_name") @db.VarChar(64)
  profile          Profile?
}

model Profile {
  id                Int                @id @default(autoincrement())
  dob               String             @db.VarChar(32)
  weightLbs         Float              @map("weight_lbs")
  heightIn          Int                @map("height_in") @db.SmallInt
  user              User               @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId            Int                @unique @map("user_id")
  gender            Gender             @relation(fields: [genderId], references: [id])
  genderId          Int                @map("gender_id")
  lifeStyle         LifeStyle?         @relation(fields: [lifeStyleId],              @map("life_style_id")
  medicalConditions MedicalCondition[]
}

model Gender {
  id      Int       @id @default(autoincrement())
  text    String
  Profile Profile[]
}

model MedicalCondition {
  id       Int       @id @default(autoincrement())
  text     String
  Profiles Profile[]
}

I'm wondering is it possible to update User and upsert Profile together like below?

const updateWithProfile = (where: IUserUniqueInput, data: IUserUpdate, medicalConditions?: IMedicalConditionWhereUniqueInput[]): Promise<IUser> => {
  return user.update({
    where,
    data:{
      ...data,
      profile: {
        upsert:{
          ????
        },
      }
    }
  });
};

Solution

  • Here is my working solution for upsert one-to-one and many-to-many relations:

    const update = async (
      data: IProfileUpdate,
      medicalConditions?: IMedicalConditionWhereUniqueInput[]
    ): Promise<IProfile> => {
      return profile.upsert({
        where: {
          userId: data.userId,
        },
        update: {
          ...data,
          // set removes previous values in mapping table and connect inserts new IDs
          medicalConditions: { set: [], connect: medicalConditions }
        },
        create: {
          ...data,
          medicalConditions: { connect: medicalConditions }
        }
      });
    };