Set value based on current

Is there a shortcut for setting a value based on it's current value in Prisma, I have a field that's initially null that I would like to update in a webhook, but if it's already set i don't want to update it. Can i do this in a single query or do I have to first get the field and then update based on the response of the firrst query?
3 Replies
Neto
Neto3y ago
maybe adding a clause on where, that the initial value has to be null?
julius
juliusOP3y ago
yea but i want to update other fields unconditionally I can do this
const old = await prisma.x.findUnique({
where: { id: someId },
});

await prisma.y.updateMany({
where: { userId: someUserId },
data: {
w: !old.w ? newVal : undefined, // skip if set
// ... other fields
}
});
const old = await prisma.x.findUnique({
where: { id: someId },
});

await prisma.y.updateMany({
where: { userId: someUserId },
data: {
w: !old.w ? newVal : undefined, // skip if set
// ... other fields
}
});
but doing it in a single query would be preferred
Neto
Neto3y ago
probably you enter raw sql land
Want results from more Discord servers?
Add your server