Skip to content

Commit 5125610

Browse files
authored
fix(prisma-client-transactions-guide): Make $transaction([]) explicit (#5122)
Right now the document refers to the batch transaction API only as "`$transaction` API". But interactive transactions of course also use `$transaction()`, just with a different parameter (functions instead of array). Calling it the "`$transaction([])` API" at least lets us differentiate. This needs to be cleaned up on a higher level though as well.
1 parent 98b8b5f commit 5125610

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

content/300-guides/100-performance-and-optimization/100-prisma-client-transactions-guide.mdx

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Prisma Client supports six different ways of handling transactions for three dif
2828
| Scenario | Available techniques |
2929
| :------------------ | :-------------------------------------------------------------------------------------------------------------- |
3030
| Dependent writes | <ul><li>Nested writes</li></ul> |
31-
| Independent writes | <ul><li>`$transaction` API</li><li>Batch operations</li></ul> |
31+
| Independent writes | <ul><li>`$transaction([])` API</li><li>Batch operations</li></ul> |
3232
| Read, modify, write | <ul><li>Idempotent operations</li><li>Optimistic concurrency control</li><li>Interactive transactions</li></ul> |
3333

3434
The technique you choose depends on your particular use case.
@@ -79,7 +79,7 @@ Consider using nested writes if:
7979

8080
<Tip>
8181

82-
If you [pre-compute your IDs, you can choose between a nested write or using the `$transaction` API](#scenario-pre-computed-ids-and-the-transaction-api).
82+
If you [pre-compute your IDs, you can choose between a nested write or using the `$transaction([])` API](#scenario-pre-computed-ids-and-the-transaction-api).
8383

8484
</Tip>
8585

@@ -156,9 +156,9 @@ Furthermore, if an error occurs at any point, Prisma Client rolls back the entir
156156

157157
#### Nested writes FAQs
158158

159-
##### Why can't I use the `$transaction` API to solve the same problem?
159+
##### Why can't I use the `$transaction([])` API to solve the same problem?
160160

161-
The `$transaction` API does not allow you to pass IDs between distinct operations. In the following example, `createUserOperation.id` is not available yet:
161+
The `$transaction([])` API does not allow you to pass IDs between distinct operations. In the following example, `createUserOperation.id` is not available yet:
162162

163163
```ts highlight=12;delete
164164
const createUserOperation = prisma.user.create({
@@ -181,9 +181,9 @@ const createTeamOperation = prisma.team.create({
181181
await prisma.$transaction([createUserOperation, createTeamOperation])
182182
```
183183

184-
##### Nested writes support nested updates, but updates are not dependent writes - should I use the `$transaction` API?
184+
##### Nested writes support nested updates, but updates are not dependent writes - should I use the `$transaction([])` API?
185185

186-
It is correct to say that because you know the ID of the team, you can update the team and its team members independently within a `$transaction`. The following example performs both operations in a `$transaction`:
186+
It is correct to say that because you know the ID of the team, you can update the team and its team members independently within a `$transaction([])`. The following example performs both operations in a `$transaction([])`:
187187

188188
```ts
189189
const updateTeam = prisma.team.update({
@@ -247,7 +247,7 @@ const updateTeam = await prisma.team.update({
247247
Yes, but this is a combination of scenarios and techniques:
248248

249249
- Creating a team and assigning users is a dependent write - use nested writes
250-
- Creating all teams and users at the same time is an independent write because team/user combination #1 and team/user combination #2 are unrelated writes - use the `$transaction` API
250+
- Creating all teams and users at the same time is an independent write because team/user combination #1 and team/user combination #2 are unrelated writes - use the `$transaction([])` API
251251

252252
```ts
253253
// Nested write
@@ -274,7 +274,7 @@ const createTwo = prisma.team.create({
274274
},
275275
})
276276

277-
// $transaction API
277+
// $transaction([]) API
278278
await prisma.$transaction([createTwo, createOne])
279279
```
280280

@@ -359,13 +359,13 @@ await prisma.team.deleteMany({
359359
})
360360
```
361361

362-
#### Can I use bulk operations with the `$transaction` API?
362+
#### Can I use bulk operations with the `$transaction([])` API?
363363

364-
Yes - for example, you can include multiple `deleteMany` operations inside a `$transaction`.
364+
Yes - for example, you can include multiple `deleteMany` operations inside a `$transaction([])`.
365365

366-
### <inlinecode>$transaction</inlinecode> API
366+
### <inlinecode>$transaction([])</inlinecode> API
367367

368-
The `$transaction` API is generic solution to independent writes that allows you to run multiple operations as a single, atomic operation - if any operation fails, Prisma rolls back the entire transaction.
368+
The `$transaction([])` API is generic solution to independent writes that allows you to run multiple operations as a single, atomic operation - if any operation fails, Prisma rolls back the entire transaction.
369369

370370
Its also worth noting that operations are executed according to the order they are placed in the transaction.
371371

@@ -375,11 +375,11 @@ await prisma.$transaction([iRunFirst, iRunSecond, iRunThird])
375375

376376
> **Note**: Using a query in a transaction does not influence the order of operations in the query itself.
377377
378-
As Prisma Client evolves, use cases for the `$transaction` API will increasingly be replaced by more specialized bulk operations (such as `createMany`) and nested writes.
378+
As Prisma Client evolves, use cases for the `$transaction([])` API will increasingly be replaced by more specialized bulk operations (such as `createMany`) and nested writes.
379379

380-
#### When to use the `$transaction` API
380+
#### When to use the `$transaction([])` API
381381

382-
Consider the `$transaction` API if:
382+
Consider the `$transaction([])` API if:
383383

384384
- ✔ You want to update a batch that includes different types of records, such as emails and users. The records do not need to be related in any way.
385385
- ✔ You want to batch raw SQL queries (`$executeRaw`) - for example, for features that Prisma Client does not yet support.
@@ -411,7 +411,7 @@ model PrivateMessage {
411411
}
412412
```
413413

414-
If a user invokes the right to be forgotten, we must delete three records: the user record, private messages, and posts. It is critical that _all_ delete operations succeed together or not at all, which makes this a use case for a transaction. However, using a single bulk operation like `deleteMany` is not possible in this scenario because we need to delete across three models. Instead, we can use the `$transaction` API to run three operations together - two `deleteMany` and one `delete`:
414+
If a user invokes the right to be forgotten, we must delete three records: the user record, private messages, and posts. It is critical that _all_ delete operations succeed together or not at all, which makes this a use case for a transaction. However, using a single bulk operation like `deleteMany` is not possible in this scenario because we need to delete across three models. Instead, we can use the `$transaction([])` API to run three operations together - two `deleteMany` and one `delete`:
415415

416416
```ts
417417
const id = 9 // User to be deleted
@@ -437,9 +437,9 @@ const deleteUser = prisma.user.delete({
437437
await prisma.$transaction([deletePosts, deleteMessages, deleteUser]) // Operations succeed or fail together
438438
```
439439

440-
#### Scenario: Pre-computed IDs and the `$transaction` API
440+
#### Scenario: Pre-computed IDs and the `$transaction([])` API
441441

442-
Dependent writes are not supported by the `$transaction` API - if operation A relies on the ID generated by operation B, use [nested writes](#nested-writes). However, if you _pre-computed_ IDs (for example, by generating GUIDs), your writes become independent. Consider the sign-up flow from the nested writes example:
442+
Dependent writes are not supported by the `$transaction([])` API - if operation A relies on the ID generated by operation B, use [nested writes](#nested-writes). However, if you _pre-computed_ IDs (for example, by generating GUIDs), your writes become independent. Consider the sign-up flow from the nested writes example:
443443

444444
```ts
445445
await prisma.team.create({
@@ -472,7 +472,7 @@ model User {
472472
}
473473
```
474474

475-
Refactor the sign-up flow example to use the `$transaction` API instead of nested writes:
475+
Refactor the sign-up flow example to use the `$transaction([])` API instead of nested writes:
476476

477477
```ts
478478
import { v4 } from 'uuid'
@@ -524,7 +524,7 @@ await prisma.team.create({
524524
})
525525
```
526526

527-
There's no compelling reason to switch to manually generated IDs and the `$transaction` API if you are already using auto-generated IDs and nested writes.
527+
There's no compelling reason to switch to manually generated IDs and the `$transaction([])` API if you are already using auto-generated IDs and nested writes.
528528

529529
## Read, modify, write
530530

@@ -818,7 +818,7 @@ It is now impossible for two people to book the same seat:
818818
819819
If you have an existing application, it can be a significant undertaking to refactor your application to use optimistic concurrency control. Interactive Transactions offers a useful escape hatch for cases like this.
820820
821-
To create an interactive transaction, pass an async function into [$transaction](#transaction-api).
821+
To create an interactive transaction, pass an async function into [$transaction](#transaction-api). <!-- TODO This is terrible wording, as that other section is meant to describe the batch $transaction([]) API, and not $transaction in general -->
822822
823823
The first argument passed into this async function is an instance of Prisma Client. Below, we will call this instance `tx`. Any Prisma call invoked on this `tx` instance is encapsulated into the transaction.
824824

0 commit comments

Comments
 (0)