PrismaP
Prisma10mo ago
4 replies
Mike

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 ]) for each item
2. Writing a custom extension or helper with an SQL builder
3. Executing raw SQL via 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 u
SET
  name   = updates.new_name,
  status = updates.new_status
FROM updates
WHERE u.id = updates.id;
Was this page helpful?