简体   繁体   中英

Is there a way to batch update objects in prisma using a nested create

prisma v1.28.3,
nodeJs: v10.15.3

Let's say we have the following prisma definitions

type ScheduledCharge {
 processedAt: DateTime!
 transactions: [Transaction!]!
}

type Transaction {
 id: ID! @unique
 amount: number
}

Now, given that we have a collection of ScheduledCharge , is there a way I could batch update ScheduledCharge with a nested object creation, ideally something like this

prisma.updateManyScheduledCharges({
  where: {
    id_in: [1, 2, 3]
  },
  data: {
    transactions: {
      create: [{
        amount,
      }]
    }
  },
})

But the mentioned above isn't generated by the prisma client, I could however loop through the scheduled charge collection and do the following

for (const { id: scheduledChargeId } of scheduledCharges) {
  prisma.updateScheduledCharge({
    where: {
      id: scheduledChargeId
    },
    data: {
      transactions: {
        connect: [{
          id: transactionId,
        }]
      }
    },
  })
}

If I do the mentioned above, does anyone know if I can use MySQL transactions with prisma client and do a rollback if any of the updates failed?

Prisma client does indeed generate the id_in filter: Prisma Id_in演示

That being said, we prisma has a new spec which will enable better batching and transaction functionalities. See: https://github.com/prisma/rfcs/blob/new-ts-client-rfc/text/0000-new-ts-client.md

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM