Roll back transaction prematurely
Given a Transaction, how do I roll it back without throwing an
Error
inside the execution callback method?
Solution:Jump to solution
I managed to solve it with a wrapper function. Here it is, for anyone who may need it in the future:
``ts
/**
* Wraps a Kysely transaction such that any
Err` returned from the callback results in a rollback....4 Replies
There's no other way to do that.
Why would you want to continue the function after the transaction has ended?
Oh, and you're using
this._db
inside the transaction. That's a horrible horrible idea. You need to use trx
or the queries don't take part in the transaction. And if you run queries using other connections than the trx
inside the transction, you're going to get deadlocks.Yeah, that was a mistake left over from before starting the refactor
The reason for all this is I'm trying to make transactions play nice with the error handling pattern from https://github.com/JohannesKlauss/ts-results-es
GitHub
GitHub - JohannesKlauss/ts-results-es: A typescript implementation ...
A typescript implementation of Rust's Result object. - JohannesKlauss/ts-results-es
Solution
I managed to solve it with a wrapper function. Here it is, for anyone who may need it in the future: