Rollback transaction
Hi, I'm trying to figure out if it's possible to intentionally rollback a transaction, the idea is that if one insert into the database ends with certain return value, the whole transaction should end and the insert rolled back. Knex has
trx.rollback
on the callback parameter, but I couldn't find anything like that on Kysely, am I missing something or is this not yet implemented?5 Replies
Solution
Okay I was doing raw sql badly, this does work at least
If you throw an error from the transaction callback, the transaction will be rolled back
Ugh, there's no way to change the solution. The marked solution is wrong and you shouldn't do that. You'll end up with a
rollback
and another rollback
/commit
once the transaction callback finishes.Tried to change the solution to yours, personally I still feel like the interface is missing, as ending the transactions by throwing an exception (which has to be caught as it's rethrown) is not the best solution for query builder.
Hey 👋
We've had this in mind, and are considering an alternative API: https://github.com/kysely-org/kysely/issues/257
GitHub
More fine-grained transaction API with savepoints? · Issue #257 · k...
try { const trx = await db.begin().isolationLevel('serializable').execute() await trx.insertInto(...).execute() await trx.savepoint('foobar') try { await trx.updateTable(...).execut...
Awesome, thanks for the reply 🙂 We really like Kysely here and really like the direction you're taking it so looking forward to it