K
Kysely•2y ago
ladal1

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?
Solution:
await sql<void>`rollback`.execute(trx)
await sql<void>`rollback`.execute(trx)
Okay I was doing raw sql badly, this does work at least...
Jump to solution
5 Replies
Solution
ladal1
ladal1•2y ago
await sql<void>`rollback`.execute(trx)
await sql<void>`rollback`.execute(trx)
Okay I was doing raw sql badly, this does work at least
koskimas
koskimas•2y ago
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.
ladal1
ladal1OP•2y ago
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.
Igal
Igal•2y ago
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...
ladal1
ladal1OP•2y ago
Awesome, thanks for the reply 🙂 We really like Kysely here and really like the direction you're taking it so looking forward to it
Want results from more Discord servers?
Add your server