Hey :wave: I’m wondering, is there something simil...
# orm-help
m
Hey 👋 I’m wondering, is there something similar to this sort of API for database transactions:
Copy code
const transactionId = prisma.trasaction()

prisma.user.create()

try {
   prisma.session.create()
} catch {
   prisma.rollback(transactionId)
}
1
a
Hey Michael 👋, We don’t have that specific API but we have
interactiveTransactions
(docs) which can accomplish a similar thing.
Copy code
prisma.$transaction(async (tx) => {
  await prisma.session.create()

  // if an error is thrown, the transaction is rolled back
})
Be aware, this feature is still in preview.
1
v
👋 Hello @Michael Roberts, did Austin's recommendation help with your question? Let us know if this is still an open question!
m
Hey @Vladi Stevanovic for whatever reason I didn’t get the ping, never mind 🙂
So if we manually throw an error at the end of the transaction block, it’s effectively the same as creating a rollback?
a
Yes, any uncaught error within the
$transaction
callback will trigger a rollback. From the docs:
If the application encounters an error along the way, the async function will throw an exception and automatically rollback the transaction.