#How to `updateMany()` from an array without looping

12 messages · Page 1 of 1 (latest)

tribal gate
#

I'm trying to update the values of each item which has a different ID and cost:

await prisma.item.updateMany({
  data: [
    Object.entries(prices).map(([key, value]) => ({
      where: {
        id: key
      },
      data: {
        cost: value.cost
      }
    }))
  ]
});

However, I'm getting the following error:

Argument `data`: Invalid value provided. Expected ItemUpdateManyMutationInput or ItemUncheckedUpdateManyInput, provided ((Object, Object, Object, Object, Object))

I know that this will work:

for (const [key, value] of Object.entries(prices)) {
  await prisma.item.update({
    where: {
      id: key
    },
    data: {
      cost: value.cost
    }
  });
}```
But this isn't great for performance, the amount of queries, overhead, etc.
#

How to updateMany() from an array without looping

tribal gate
#

Bump

haughty tide
jovial turtle
#

How would you handle this if you need to make sure all of them happen, or all a reversed?

haughty tide
jovial turtle
#

no matter what promise I use

haughty tide
jovial turtle
#

is it possible to use transactions and get all models back if it was successful? I tried figuring it out a few days ago and didnt see a way to do that

#

update many only returns a number. not the full record being updated

haughty tide