Help migrating a nested Prisma update

Been trying to set this up without going through the raw sql route for some time but cant seem to wrap my head around it. Basically the structure here is I have a Result that can optionally have an alert or a note. If an alert or note already exist then update them. Any help here is appreciated it! thank you

 const updatedResult: Result = await prisma.result
    .update({
      where: {
        id: resultId,
      },
      data: {
        reviewedBy: resultData.reviewedBy,
        note: {
          upsert: {
            create: {
              reason: resultData.reason,
              patientId: resultData.patientId,
              detail: resultData.detail,
            },
            update: {
              reason: resultData.reason,
              detail: resultData.detail,
            },
          },
        },
        alert: alertId
          ? {
              update: {
                acknowledged: true,
              },
            }
          : undefined,
      },
      include: {
        note: true,
        alert: true,
      },
    })
Was this page helpful?