Tengo una relación padre-hijo como la siguiente:
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[] } Soy bastante nuevo en node.js y Prisma y me pregunto si es posible actualizar User y actualizar el Profile juntos como se muestra a continuación.
const updateWithProfile = (where: IUserUniqueInput, data: IUserUpdate, medicalConditions?: IMedicalConditionWhereUniqueInput[]): Promise<IUser> => { return user.update({ where, data:{ ...data, profile: { upsert:{ ???? }, } } }); };Aquí está mi solución de trabajo para mejorar las relaciones de uno a uno y de muchos a muchos:
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 } } }); };