lucid icon indicating copy to clipboard operation
lucid copied to clipboard

Lucid models and transactions

Open arma-gast opened this issue 10 years ago • 2 comments

At first, thank you for AdonisJS.

Is it possible to save or update model instances using transactions?

arma-gast avatar Jul 30 '16 09:07 arma-gast

You can make use of transactions as defined here http://adonisjs.com/docs/3.0/query-builder#database-transactions when trying to fetch data via models.

For example

const Database = use('Database')
const trx = yield Database.beginTransaction()

const users = yield User.query().transacting(trx).fetch()
trx.commit()

But that is not the ideal use case, a normal web application makes use of transactions when trying to persist data.

For now there is no out of the box way to make use of transactions with models, but I believe it will be fairly simple to add that and final implementation may look like this.

const Database = use('Database')
const trx = yield Database.beginTransaction()

const user = new User()
user.username = 'liza'
user.password = 'secret'
user.useTransaction(trx)
yield user.save()
trx.commit()

You can also share your thoughts on same.

thetutlage avatar Jul 30 '16 19:07 thetutlage

Agreed. Syntax is simple and readable. Can't wait to see it in release.

arma-gast avatar Aug 02 '16 14:08 arma-gast