在 Prisma 中更新父项和更新子项

Update parent and upsert child in Prisma

我的亲子关系如下:

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 Gender {
  id      Int       @id @default(autoincrement())
  text    String
  Profile Profile[]
}
model MedicalCondition {
  id       Int       @id @default(autoincrement())
  text     String
  Profiles 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[]
}

我对 node.js 和 Prisma 很陌生,我想知道是否可以像下面那样一起更新 User 和更新插入 Profile

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

这是我的更新插入 one-to-one 和 many-to-many 关系的工作解决方案:

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 }
    }
  });
};