Skip to content

Commit d781624

Browse files
authored
feat: add disableTransaction to local api (#8697)
1 parent 08f8831 commit d781624

File tree

15 files changed

+108
-22
lines changed

15 files changed

+108
-22
lines changed

docs/database/transactions.mdx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ desc: Database transactions are fully supported within Payload.
88

99
Database transactions allow your application to make a series of database changes in an all-or-nothing commit. Consider an HTTP request that creates a new **Order** and has an `afterChange` hook to update the stock count of related **Items**. If an error occurs when updating an **Item** and an HTTP error is returned to the user, you would not want the new **Order** to be persisted or any other items to be changed either. This kind of interaction with the database is handled seamlessly with transactions.
1010

11-
By default, Payload will use transactions for all operations, as long as it is supported by the configured database. Database changes are contained within all Payload operations and any errors thrown will result in all changes being rolled back without being committed. When transactions are not supported by the database, Payload will continue to operate as expected without them.
11+
By default, Payload will use transactions for all data changing operations, as long as it is supported by the configured database. Database changes are contained within all Payload operations and any errors thrown will result in all changes being rolled back without being committed. When transactions are not supported by the database, Payload will continue to operate as expected without them.
1212

1313
<Banner type="info">
1414
<strong>Note:</strong>
@@ -114,3 +114,18 @@ standalonePayloadScript()
114114
## Disabling Transactions
115115

116116
If you wish to disable transactions entirely, you can do so by passing `false` as the `transactionOptions` in your database adapter configuration. All the official Payload database adapters support this option.
117+
118+
In addition to allowing database transactions to be disabled at the adapter level. You can prevent Payload from using a transaction in direct calls to the local API by adding `disableTransaction: true` to the args. For example:
119+
120+
```ts
121+
await payload.update({
122+
collection: 'posts',
123+
data: {
124+
some: 'data',
125+
},
126+
where: {
127+
slug: { equals: 'my-slug' }
128+
},
129+
req: { disableTransaction: true },
130+
})
131+
```

docs/local-api/overview.mdx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,21 @@ Both options function in exactly the same way outside of one having HMR support
7777

7878
You can specify more options within the Local API vs. REST or GraphQL due to the server-only context that they are executed in.
7979

80-
| Local Option | Description |
81-
| ------------------ | ------------ |
82-
| `collection` | Required for Collection operations. Specifies the Collection slug to operate against. |
83-
| `data` | The data to use within the operation. Required for `create`, `update`. |
84-
| `depth` | [Control auto-population](../queries/depth) of nested relationship and upload fields. |
85-
| `locale` | Specify [locale](/docs/configuration/localization) for any returned documents. |
86-
| `fallbackLocale` | Specify a [fallback locale](/docs/configuration/localization) to use for any returned documents. |
87-
| `overrideAccess` | Skip access control. By default, this property is set to true within all Local API operations. |
88-
| `overrideLock` | By default, document locks are ignored (`true`). Set to `false` to enforce locks and prevent operations when a document is locked by another user. [More details](../admin/locked-documents).|
89-
| `user` | If you set `overrideAccess` to `false`, you can pass a user to use against the access control checks. |
90-
| `showHiddenFields` | Opt-in to receiving hidden fields. By default, they are hidden from returned documents in accordance to your config. |
91-
| `pagination` | Set to false to return all documents and avoid querying for document counts. |
92-
| `context` | [Context](/docs/hooks/context), which will then be passed to `context` and `req.context`, which can be read by hooks. Useful if you want to pass additional information to the hooks which shouldn't be necessarily part of the document, for example a `triggerBeforeChange` option which can be read by the BeforeChange hook to determine if it should run or not. |
93-
| `disableErrors` | When set to `true`, errors will not be thrown. Instead, the `findByID` operation will return `null`, and the `find` operation will return an empty documents array. |
80+
| Local Option | Description |
81+
|-----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
82+
| `collection` | Required for Collection operations. Specifies the Collection slug to operate against. |
83+
| `data` | The data to use within the operation. Required for `create`, `update`. |
84+
| `depth` | [Control auto-population](../queries/depth) of nested relationship and upload fields. |
85+
| `locale` | Specify [locale](/docs/configuration/localization) for any returned documents. |
86+
| `fallbackLocale` | Specify a [fallback locale](/docs/configuration/localization) to use for any returned documents. |
87+
| `overrideAccess` | Skip access control. By default, this property is set to true within all Local API operations. |
88+
| `overrideLock` | By default, document locks are ignored (`true`). Set to `false` to enforce locks and prevent operations when a document is locked by another user. [More details](../admin/locked-documents). |
89+
| `user` | If you set `overrideAccess` to `false`, you can pass a user to use against the access control checks. |
90+
| `showHiddenFields` | Opt-in to receiving hidden fields. By default, they are hidden from returned documents in accordance to your config. |
91+
| `pagination` | Set to false to return all documents and avoid querying for document counts. |
92+
| `context` | [Context](/docs/hooks/context), which will then be passed to `context` and `req.context`, which can be read by hooks. Useful if you want to pass additional information to the hooks which shouldn't be necessarily part of the document, for example a `triggerBeforeChange` option which can be read by the BeforeChange hook to determine if it should run or not. |
93+
| `disableErrors` | When set to `true`, errors will not be thrown. Instead, the `findByID` operation will return `null`, and the `find` operation will return an empty documents array. |
94+
| `disableTransaction` | When set to `true`, a [database transactions](../database/transactions) will not be initialized. |
9495

9596
_There are more options available on an operation by operation basis outlined below._
9697

packages/payload/src/collections/operations/create.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type Arguments<TSlug extends CollectionSlug> = {
3434
collection: Collection
3535
data: RequiredDataFromCollectionSlug<TSlug>
3636
depth?: number
37+
disableTransaction?: boolean
3738
disableVerificationEmail?: boolean
3839
draft?: boolean
3940
overrideAccess?: boolean
@@ -48,7 +49,7 @@ export const createOperation = async <TSlug extends CollectionSlug>(
4849
let args = incomingArgs
4950

5051
try {
51-
const shouldCommit = await initTransaction(args.req)
52+
const shouldCommit = !args.disableTransaction && (await initTransaction(args.req))
5253

5354
ensureUsernameOrEmail<TSlug>({
5455
authOptions: args.collection.config.auth,

packages/payload/src/collections/operations/delete.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { buildAfterOperation } from './utils.js'
2222
export type Arguments = {
2323
collection: Collection
2424
depth?: number
25+
disableTransaction?: boolean
2526
overrideAccess?: boolean
2627
overrideLock?: boolean
2728
req: PayloadRequest
@@ -41,7 +42,7 @@ export const deleteOperation = async <TSlug extends CollectionSlug>(
4142
let args = incomingArgs
4243

4344
try {
44-
const shouldCommit = await initTransaction(args.req)
45+
const shouldCommit = !args.disableTransaction && (await initTransaction(args.req))
4546
// /////////////////////////////////////
4647
// beforeOperation - Collection
4748
// /////////////////////////////////////

packages/payload/src/collections/operations/deleteByID.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { buildAfterOperation } from './utils.js'
1919
export type Arguments = {
2020
collection: Collection
2121
depth?: number
22+
disableTransaction?: boolean
2223
id: number | string
2324
overrideAccess?: boolean
2425
overrideLock?: boolean
@@ -32,7 +33,7 @@ export const deleteByIDOperation = async <TSlug extends CollectionSlug>(
3233
let args = incomingArgs
3334

3435
try {
35-
const shouldCommit = await initTransaction(args.req)
36+
const shouldCommit = !args.disableTransaction && (await initTransaction(args.req))
3637

3738
// /////////////////////////////////////
3839
// beforeOperation - Collection

packages/payload/src/collections/operations/duplicate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { buildAfterOperation } from './utils.js'
2828
export type Arguments = {
2929
collection: Collection
3030
depth?: number
31+
disableTransaction?: boolean
3132
draft?: boolean
3233
id: number | string
3334
overrideAccess?: boolean
@@ -42,7 +43,7 @@ export const duplicateOperation = async <TSlug extends CollectionSlug>(
4243
const operation = 'create'
4344

4445
try {
45-
const shouldCommit = await initTransaction(args.req)
46+
const shouldCommit = !args.disableTransaction && (await initTransaction(args.req))
4647

4748
// /////////////////////////////////////
4849
// beforeOperation - Collection

packages/payload/src/collections/operations/local/create.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type Options<TSlug extends CollectionSlug> = {
1616
context?: RequestContext
1717
data: RequiredDataFromCollectionSlug<TSlug>
1818
depth?: number
19+
disableTransaction?: boolean
1920
disableVerificationEmail?: boolean
2021
draft?: boolean
2122
fallbackLocale?: TypedLocale
@@ -38,6 +39,7 @@ export default async function createLocal<TSlug extends CollectionSlug>(
3839
collection: collectionSlug,
3940
data,
4041
depth,
42+
disableTransaction,
4143
disableVerificationEmail,
4244
draft,
4345
file,
@@ -61,6 +63,7 @@ export default async function createLocal<TSlug extends CollectionSlug>(
6163
collection,
6264
data,
6365
depth,
66+
disableTransaction,
6467
disableVerificationEmail,
6568
draft,
6669
overrideAccess,

packages/payload/src/collections/operations/local/delete.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type BaseOptions<TSlug extends CollectionSlug> = {
1414
*/
1515
context?: RequestContext
1616
depth?: number
17+
disableTransaction?: boolean
1718
fallbackLocale?: TypedLocale
1819
locale?: TypedLocale
1920
overrideAccess?: boolean
@@ -55,6 +56,7 @@ async function deleteLocal<TSlug extends CollectionSlug>(
5556
id,
5657
collection: collectionSlug,
5758
depth,
59+
disableTransaction,
5860
overrideAccess = true,
5961
overrideLock,
6062
showHiddenFields,
@@ -73,6 +75,7 @@ async function deleteLocal<TSlug extends CollectionSlug>(
7375
id,
7476
collection,
7577
depth,
78+
disableTransaction,
7679
overrideAccess,
7780
overrideLock,
7881
req: await createLocalReq(options, payload),

packages/payload/src/collections/operations/local/duplicate.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type Options<TSlug extends CollectionSlug> = {
1414
*/
1515
context?: RequestContext
1616
depth?: number
17+
disableTransaction?: boolean
1718
draft?: boolean
1819
fallbackLocale?: TypedLocale
1920
id: number | string
@@ -32,6 +33,7 @@ export async function duplicate<TSlug extends CollectionSlug>(
3233
id,
3334
collection: collectionSlug,
3435
depth,
36+
disableTransaction,
3537
draft,
3638
overrideAccess = true,
3739
showHiddenFields,
@@ -57,6 +59,7 @@ export async function duplicate<TSlug extends CollectionSlug>(
5759
id,
5860
collection,
5961
depth,
62+
disableTransaction,
6063
draft,
6164
overrideAccess,
6265
req,

packages/payload/src/collections/operations/local/update.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type BaseOptions<TSlug extends CollectionSlug> = {
2424
context?: RequestContext
2525
data: DeepPartial<RequiredDataFromCollectionSlug<TSlug>>
2626
depth?: number
27+
disableTransaction?: boolean
2728
draft?: boolean
2829
fallbackLocale?: TypedLocale
2930
file?: File
@@ -73,6 +74,7 @@ async function updateLocal<TSlug extends CollectionSlug>(
7374
collection: collectionSlug,
7475
data,
7576
depth,
77+
disableTransaction,
7678
draft,
7779
file,
7880
filePath,
@@ -101,6 +103,7 @@ async function updateLocal<TSlug extends CollectionSlug>(
101103
collection,
102104
data,
103105
depth,
106+
disableTransaction,
104107
draft,
105108
overrideAccess,
106109
overrideLock,

0 commit comments

Comments
 (0)