skip to Main Content

How can I access each element that is affected by Prisma’s updateMany method?

I would like to update the values based on the current value of each record:


   // I want to achieve something like that
   prisma.user.updateMany((record) => {

      // access the current record
      const recordID = record.id 

      // return or update the record
      return { data: { encryptedId: encrypt(recordID) }};
       };
   });

2

Answers


  1. Chosen as BEST ANSWER

    I ended up using this implementation:

     await prisma.$transaction(async (tx) => {
          const ids = await tx.user.findMany({ select: { id: true } });
    
          await Promise.all(
            ids.map(({ id }) =>
              tx.user.update({ where: { id }, data: { recordId: id } })
            )
          );
    
    

  2. check this answer you may use $transaction with map

    // recordIDs array of ids
    await prisma.$transaction(
      recordIDs.map((recordID) =>
        prisma.user.update({
          where: { id: recordID },
          data: { encryptedId: encrypt(recordID) },
        }),
      ),
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search