You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The technique you choose depends on your particular use case.
@@ -79,7 +79,7 @@ Consider using nested writes if:
79
79
80
80
<Tip>
81
81
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).
83
83
84
84
</Tip>
85
85
@@ -156,9 +156,9 @@ Furthermore, if an error occurs at any point, Prisma Client rolls back the entir
156
156
157
157
#### Nested writes FAQs
158
158
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?
160
160
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:
##### 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?
185
185
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([])`:
#### Can I use bulk operations with the `$transaction` API?
362
+
#### Can I use bulk operations with the `$transaction([])` API?
363
363
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([])`.
365
365
366
-
### <inlinecode>$transaction</inlinecode> API
366
+
### <inlinecode>$transaction([])</inlinecode> API
367
367
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.
369
369
370
370
Its also worth noting that operations are executed according to the order they are placed in the transaction.
> **Note**: Using a query in a transaction does not influence the order of operations in the query itself.
377
377
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.
379
379
380
-
#### When to use the `$transaction` API
380
+
#### When to use the `$transaction([])` API
381
381
382
-
Consider the `$transaction` API if:
382
+
Consider the `$transaction([])` API if:
383
383
384
384
- ✔ 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.
385
385
- ✔ 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 {
411
411
}
412
412
```
413
413
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`:
awaitprisma.$transaction([deletePosts, deleteMessages, deleteUser]) // Operations succeed or fail together
438
438
```
439
439
440
-
#### Scenario: Pre-computed IDs and the `$transaction` API
440
+
#### Scenario: Pre-computed IDs and the `$transaction([])` API
441
441
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:
443
443
444
444
```ts
445
445
awaitprisma.team.create({
@@ -472,7 +472,7 @@ model User {
472
472
}
473
473
```
474
474
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:
476
476
477
477
```ts
478
478
import { v4 } from'uuid'
@@ -524,7 +524,7 @@ await prisma.team.create({
524
524
})
525
525
```
526
526
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.
528
528
529
529
## Read, modify, write
530
530
@@ -818,7 +818,7 @@ It is now impossible for two people to book the same seat:
818
818
819
819
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.
820
820
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 -->
822
822
823
823
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.
0 commit comments