prisma

Update multiple files in prisma


In an automated task, i'm making updates in a group of objets of the same Entity and I need to persist that changes. For example

arrayOfObjects[
Objetc A:
 id: 1
 name: 'Test'
 lastName: 'Other'
,
Object B:
 id: 2
 name: 'Pro'
 lastName: 'Top']

arrayOfId[1,2]

I try to update with updateMany, but it can't be done i think.

prisma.Object.updateMany({
 where: {
   id: {
    in: arrayOfId
  }
 },
 data: arrayOfObjects
});

Wath can i do. I try to use the prisma.Object.update in a loop of the array, but i get the following error.

Operations timed out after `N/A`. Context: The database failed to respond to a query within the configured timeout

Solution

  • I find a solution in a related post here.

    One way to do it is with the $transaction API of prisma. It can be used in two ways:

    In my case I iterate the array of objects an created and array of promises like this.

    arrayOfObjects.forEach( (object) => {
       array.push(prisma.Object.update(logic));
    })
    prisma.$transaction(array);