How can I perform batch updates unique entities in Prisma?
How can I perform batch updates in Prisma — akin to createMany — by passing an array or collection to a single operation? I’m working inside long-lived interactive transactions and need to update multiple records by their unique IDs. What’s the best approach?
1. Using
Promise.all([ tx.model.update(…)*N ])
Promise.all([ tx.model.update(…)*N ])
for each item 2. Writing a custom extension or helper with an SQL builder 3. Executing raw SQL via
tx.$executeRaw
tx.$executeRaw
Could you also show an example SQL query that accomplishes this? (I’m not very familiar with SQL, so any guidance is appreciated.)
WITH updates (id, new_name, new_status) AS ( VALUES (1, 'Alice', 'active'), (2, 'Bob', 'pending'), (5, 'Charlie', 'banned'))UPDATE users uSET name = updates.new_name, status = updates.new_statusFROM updatesWHERE u.id = updates.id;
WITH updates (id, new_name, new_status) AS ( VALUES (1, 'Alice', 'active'), (2, 'Bob', 'pending'), (5, 'Charlie', 'banned'))UPDATE users uSET name = updates.new_name, status = updates.new_statusFROM updatesWHERE u.id = updates.id;