How to use beginTransaction from Database?

Hi People,

I’m having trouble using it, and I’m doing it as follows, but the mistake I have is that it says ‘trx.close ()’ doesn’t exist.

class MyController extends BaseController {

    async index ({ response }) {

        const trx = await Database.beginTransaction()

        try {

            // code

            if (success) {
                trx.commit()
            } else {
                trx.rollback()
                await trx.close()
            }

            return response.status(200).send({
                status: true,
                message: 'Success'
            })

        } catch (e) {

            trx.rollback()

            await trx.close()

            return response.status(500).send({
                status: false,
                message: 'Error'
            })

        }

    }
}

Why you want to call trx.close()?

Commit and rollback manage all transaction stuff for you.

You can try the callback transaction version too:

await Database.transaction(async (trx) => {
  await trx.insert({username: 'virk'}).into('users')
})