Skip to content

Commit b10f61c

Browse files
authored
fix(db-mongodb): add req to migration templates for transactions (#8407)
- Add `req` to MigrateUpArgs and MigrateDownArgs for mongodb - Improve docs for transactions and migrations
1 parent 87360f2 commit b10f61c

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

docs/database/migrations.mdx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ A migration file has two exports - an `up` function, which is called when a migr
3333
that will be called if for some reason the migration fails to complete successfully. The `up` function should contain
3434
all changes that you attempt to make within the migration, and the `down` should ideally revert any changes you make.
3535

36-
For an added level of safety, migrations should leverage Payload [transactions](/docs/database/transactions). Migration
37-
functions should make use of the `req` by adding it to the arguments of your Payload Local API calls such
38-
as `payload.create` and Database Adapter methods like `payload.db.create`.
39-
4036
Here is an example migration file:
4137

4238
```ts
@@ -53,6 +49,14 @@ export async function down({ payload, req }: MigrateDownArgs): Promise<void> {
5349
}
5450
```
5551

52+
## Using Transactions
53+
54+
When migrations are run, each migration is performed in a new [transactions](/docs/database/transactions) for you. All
55+
you need to do is pass the `req` object to any [local API](/docs/local-api/overview) or direct database calls, such as
56+
`payload.db.updateMany()`, to make database changes inside the transaction. Assuming no errors were thrown, the transaction is committed
57+
after your `up` or `down` function runs. If the migration errors at any point or fails to commit, it is caught and the
58+
transaction gets aborted. This way no change is made to the database if the migration fails.
59+
5660
## Migrations Directory
5761

5862
Each DB adapter has an optional property `migrationDir` where you can override where you want your migrations to be

docs/database/transactions.mdx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,48 @@ The following functions can be used for managing transactions:
6969
`payload.db.commitTransaction` - Takes the identifier for the transaction, finalizes any changes.
7070
`payload.db.rollbackTransaction` - Takes the identifier for the transaction, discards any changes.
7171

72+
Payload uses the `req` object to pass the transaction ID through to the database adapter. If you are not using the `req` object, you can make a new object to pass the transaction ID directly to database adapter methods and local API calls.
73+
Example:
74+
75+
```ts
76+
import payload from 'payload'
77+
import config from './payload.config'
78+
79+
const standalonePayloadScript = async () => {
80+
// initialize Payload
81+
await payload.init({ config })
82+
83+
const transactionID = await payload.db.beginTransaction()
84+
85+
try {
86+
// Make an update using the local API
87+
await payload.update({
88+
collection: 'posts',
89+
data: {
90+
some: 'data',
91+
},
92+
where: {
93+
slug: { equals: 'my-slug' }
94+
},
95+
req: { transactionID },
96+
})
97+
98+
/*
99+
You can make additional db changes or run other functions
100+
that need to be committed on an all or nothing basis
101+
*/
102+
103+
// Commit the transaction
104+
await payload.db.commitTransaction(transactionID)
105+
} catch (error) {
106+
// Rollback the transaction
107+
await payload.db.rollbackTransaction(transactionID)
108+
}
109+
}
110+
111+
standalonePayloadScript()
112+
```
113+
72114
## Disabling Transactions
73115

74116
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.

packages/db-mongodb/src/createMigration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ const migrationTemplate = ({ downSQL, imports, upSQL }: MigrationTemplateArgs):
1111
} from '@payloadcms/db-mongodb'
1212
${imports}
1313
14-
export async function up({ payload }: MigrateUpArgs): Promise<void> {
14+
export async function up({ payload, req }: MigrateUpArgs): Promise<void> {
1515
${upSQL ?? ` // Migration code`}
1616
}
1717
18-
export async function down({ payload }: MigrateDownArgs): Promise<void> {
18+
export async function down({ payload, req }: MigrateDownArgs): Promise<void> {
1919
${downSQL ?? ` // Migration code`}
2020
}
2121
`

packages/db-mongodb/src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type {
2020
JSONField,
2121
NumberField,
2222
Payload,
23+
PayloadRequest,
2324
PointField,
2425
RadioField,
2526
RelationshipField,
@@ -109,5 +110,5 @@ export type FieldToSchemaMap<TSchema> = {
109110
upload: FieldGeneratorFunction<TSchema, UploadField>
110111
}
111112

112-
export type MigrateUpArgs = { payload: Payload }
113-
export type MigrateDownArgs = { payload: Payload }
113+
export type MigrateUpArgs = { payload: Payload; req: PayloadRequest }
114+
export type MigrateDownArgs = { payload: Payload; req: PayloadRequest }

0 commit comments

Comments
 (0)