Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lucid models and transactions #43

Closed
arma-gast opened this issue Jul 30, 2016 · 2 comments
Closed

Lucid models and transactions #43

arma-gast opened this issue Jul 30, 2016 · 2 comments
Assignees

Comments

@arma-gast
Copy link

At first, thank you for AdonisJS.

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

@thetutlage
Copy link
Member

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.

@arma-gast
Copy link
Author

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

thetutlage added a commit that referenced this issue Jul 16, 2017
now model instance useTransaction method makes it easier to use transactions for database writes

Closes #43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants