Skip to content
This repository has been archived by the owner on Sep 27, 2021. It is now read-only.

Commit

Permalink
Add docs for transactions in hooks and delete method (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
yariksav authored Jul 16, 2020
1 parent be93947 commit 73d9e61
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
11 changes: 10 additions & 1 deletion 08-Lucid-ORM/01-Getting-Started.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ const user = await User.find(1, trx)

[source, js]
----
const user = await User.query(trx).where('username','virk).first()
const user = await User.query(trx).where('username','virk').first()
----

[source, js]
Expand All @@ -797,6 +797,15 @@ await User.createMany([
], trx)
----

Also you can pass the `trx` object to the `delete` method:

[source, js]
----
const user = await User.find(1, trx)
await user.delete(trx)
----

=== Transactions in Relationships
When using transactions, you'll need to pass a `trx` object as the third parameter of the relationship `attach` and `detach` methods:

Expand Down
21 changes: 21 additions & 0 deletions 08-Lucid-ORM/02-Hooks.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ class User extends Model {
module.exports = User
----

=== Hook Within Transactions
To use hooks within link:lucid#_transactions[transactions] just add second param `trx` into hook closure

[source, js]
----
const Model = use('Model')
const Hash = use('Hash')
class User extends Model {
static boot () {
super.boot()
this.addHook('beforeCreate', async (userInstance, trx) => {
// here you can use transaction object `trx`
})
}
}
module.exports = User
----

In the example above, the `beforeCreate` closure is executed when creating a `User` model to ensure the user's password is hashed before it's saved.

=== Hook File
Expand Down

0 comments on commit 73d9e61

Please sign in to comment.