Hi guys, hoping someone can help with a quick ques...
# orm-help
m
Hi guys, hoping someone can help with a quick question. Is it possible to conditionally add an upsert? That is, if a certain array was passed in the payload, add the upsert to my top-level update statement? Eg: if payload.children.map below was undefined, how can I run the below query without an error?
Copy code
prisma.parent.update({
   name: "Bob",
   age: 30,
   children: {
      upsert: payload.children.map(child => (
         where: { id: child.id },
         create: { ... },
         update: { ... }
      ))
   }
})
b
you want to update only if payload is defined, is that it?
m
Correct
b
the whole payload is undefined or the child can be undefined?
you can do something like cleanPayload = payload.children.map( x => x) cleanPayload will not have undefined then, I believe. Because can't you simply verify if payload is undefined before doing the update?
m
Sorry the payload.children array can be undefined
Or an empty array
And if it is I want the query to be:
Copy code
prisma.parent.update({
   name: "Bob",
   age: 30,
})
Instead
But this is a very simple example, my actual query is big and nested, so I can't have a condition check around the entire query
b
I see
Copy code
children: payload.children[0] && {   upsert: payload.children.map(child => (
         where: { id: child.id },
         create: { ... },
         update: { ... }
      ))
   }
would that work?
cause ultimately you can make a condition to return an object if a certain condition is met
m
I did try that, but still got an error - not 100% sure yet if it is related, but it works without it so I assume so
(works when that object is defined)
b
oh yea
Copy code
payload.children && payload.children[0] &&
kinda ugly, but u get the idea xD
m
I did payload.children.length
b
u can have a function that returns if object is defined and has content
👍 1