Hello Guys, Sorry but i'm having a real hard time ...
# orm-help
a
Hello Guys, Sorry but i'm having a real hard time to read the doc about the updateMany, partially because some pieces are written in Typescript and it confuses me (as I don't know TS) Maybe somebody can point me to the right direction. I have several Item records on postgres that I would like to update at once. I would like to do it with an array of objects but I'm missing a piece.
Copy code
await prisma.Item.updateMany({
          data: [...updatedItems],
        })
Maybe an identifier ? PK are ID's that are present inside ;y objects, but I don't know how to let prisma see those ID and update the right records (maybe its automatic ?)
m
I think you would need to loop through that array of objects (updatedItems) and do an update() for each one of them. Something like:
Copy code
for(let item of updatedItems) {
  await prisma.Item.update({
    where: {
      id: item.id
    }
   data: {
      field: item.field,
      anotherField: item.anotherField
    }
 })
}
a
yes thanks for your answer, I thought about it but I thought it might not be how it shall be done as there is a updateMany. But maybe i didnt understand the usage of this. Anyway i'll do like this to go ahead, thanks !
r
@A DSJ 👋 You can pass data directly in
updateMany
and that will update all items with the same data. If you need to update different items with different data, then you need separate
update
calls.
a
@Ryan Thanks that's clear now !
🙌 1