I am writing an update API which has this block of...
# orm-help
s
I am writing an update API which has this block of code.
Copy code
await this.prismaService.person.update({
      where: {
        id,
      },
      data: {
        name,
        configuration: {
          update: configuration,
        },
        address,
        age,
      }
})
Now
name
,
address
and
age
comes from request body and might be empty. I want to update these properties only if they are not empty. Is there a way to do so?
r
Hey Shivam, I believe you can do something like:
Copy code
const updateInput: PersonUpdateInput = {
  configuration: {
    update: configuration
  }
};

if(age && age.length > 0) updateInput.age = age;
if(name && name.length > 0) updateInput.name = name;
if(address && address.length > 0) updateInput.address = address;

await this.prismaService.person.update({
  where: { id },
  data: updateInput
})
you should be able to import
PersonUpdateInput
from the prisma types, as
prisma.person.update
will receive
PersonUpdateArgs
, which should include:
data: _XOR_<_PersonUpdateInput_, _PersonUncheckedUpdateInput_>
n
What Ruben mentioned is absolutely correct, however we also have a Feature Request which would allow you to do conditional updates: #5108 If you could leave a 👍 to the request then it would help our team in prioritising it.