I have a question regarding transactions. Will the...
# prisma-client
p
I have a question regarding transactions. Will the following work and be properly rolled back when any query fails?
Copy code
const queries = []

for (const somevar of somearray) {
  const params = await someAsyncFunc(somevar)
  queries.push(prisma.someQuery(optsWithParams))
}

await prisma.$transaction(queries)
The thing Im unsure about is that query 1 might be finished before calling prisma.$transaction because there are async operations required to generate the other queries.
1
r
@Pascal Sthamer 👋 Looking at the code, this should work fine.
As long as Prisma specific operations are in your transaction, it will rollback correctly.
p
Alright, good to know. Could you give a little insight on how this works internally? From my understanding query 1 might already be written to database while an error occurs with say query 30. In other words, prisma.$transaction might receive alrady resolved query promises. How does prisma rollback all the previous queries? From my understanding they are not executed in a single database transaction?
r
Prisma queries are lazy so unless you append
await
or call them inside the transaction, they will not fire before that.
1
p
Ohhh
That makes things a lot clearer. Thanks for the insight!
👍 1
So basically it returns a promise, which never runs until .then() is called on it?
r
Yeah