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

feat: add transaction example with customer as a parameter #36

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions content/docs/guides/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,21 @@ await db.transaction(async (trx) => {
// highlight-end
```

It is possible to give the transaction as a customer, here is a simple example.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this sentence means?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The aim is to document the fact that you can create an entity with an await Foo.create(..) and give it the transaction.

Currently, the documentation shows that you can do a new Foo() and modify the fields manually. This solution of passing the trx to the client exists and documenting it could I think help several people.

I think I phrased the sentence wrong, what do you think about:

Instead of instantiating your entity using the new keyword, you can give the transaction in the client as a parameter.


```ts
import User from '#models/user'
import db from '@adonisjs/lucid/services/db'

// highlight-start
await db.transaction(async (trx) => {
const user = await User.create({
username: 'virk'
}, { client: trx })
})
// highlight-end
```

### Model query builder

Just like the standard query builder, you can also pass the transaction to the model query builder.
Expand Down