Transaction client

The Transaction client extends the Query client and has following extra properties on top of the standard query client.

You can access the transaction query client as follows:

import Database from '@ioc:Adonis/Lucid/Database'
const trx = await Database.transaction()
// for a given connection
const trx = await Database
.connection('pg')
.transaction()

You can also define the transaction isolation level as follows.

await Database.transaction({
isolationLevel: 'read uncommitted'
})

Following is the list of available isolation levels.

  • "read uncommitted"
  • "read committed"
  • "snapshot"
  • "repeatable read"
  • "serializable"

Methods/Properties

Following is the list of methods and properties available on the transaction client class.

commit

Commit the transaction

await trx.commit()

rollback

Rollback the transaction

await trx.rollback()

isCompleted

Find if the transaction has been completed or not.

if (!trx.isCompleted) {
await trx.commit()
}

Events

The transaction client also events the following events when the transaction is committed or rolled back.

trx.once('commit', (self) => {
console.log(self)
})
trx.once('rollback', (self) => {
console.log(self)
})