K
Kyselyβ€’16mo 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β€’16mo 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β€’16mo 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
ladal1β€’16mo 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β€’16mo 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
ladal1β€’16mo 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