docs: document when-then + migrations revert change (#8089)

- Add documentation around when-then and workflow conditions
- Change the CLI reference based on change in revert migrations command.
This commit is contained in:
Shahed Nasser
2024-07-11 15:30:50 +03:00
committed by GitHub
parent e489b0c037
commit 368715ae37
4 changed files with 108 additions and 8 deletions

View File

@@ -0,0 +1,62 @@
export const metadata = {
title: `${pageNumber} Conditions in Workflow with When-Then`,
}
# {metadata.title}
In this chapter, you'll learn how to execute an action based on a condition in a workflow using the when-then utility.
## What is the When-Then Utility?
The when-then utility executes an action if a condition is satisfied.
Since if-conditions aren't allowed in the workflow constructor function, use the when-then utility if you want to execute a step based on a condition.
---
## How to Use the When-Then Utility?
For example:
export const highlights = [
["15", "input", "The data to pass as a parameter to the function in the second parameter"],
["17", "return", "The function must return a boolean value indicating whether\nthe callback function passed to `then` should be executed."],
["19", "() => {", "The function to execute if `when`'s second parameter returns a `true` value."]
]
```ts highlights={highlights}
import {
createWorkflow,
when
} from "@medusajs/workflows-sdk"
// step imports...
type WorkflowInput = {
is_active: boolean
}
const workflow = createWorkflow
<WorkflowInput, any>
("workflow", function (input) {
const result = when(
input,
(input) => {
return input.is_active
}
).then(() => {
const stepResult = isActiveStep()
return stepResult
})
// executed without condition
return anotherStep(result)
}
)
```
Then `when` utility is a function imported from `@medusajs/workflows-sdk`. It accepts the following parameters:
1. An object or the workflow's input. This data is passed as a parameter to the function in `when`'s second parameter.
2. A function that returns a boolean indicating whether to execute the action.
To specify the action to perform if the condition is satisfied, chain a `then` function to `when` and pass it a callback function. The callback function is only executed if `when`'s second parameter function returns a `true` value.

View File

@@ -75,6 +75,35 @@ const myWorkflow = createWorkflow<
})
```
### No If Conditions
You can't use if-conditions in a workflow. Instead, use the when-then utility function explained in the next chapter:
```ts
// Don't
const myWorkflow = createWorkflow<
WorkflowInput,
WorkflowOutput
>("hello-world", function (input) {
if (input.is_active) {
// perform an action
}
})
// Do (explained in the next chapter)
const myWorkflow = createWorkflow<
WorkflowInput,
WorkflowOutput
>("hello-world", function (input) {
when(input, (input) => {
return input.is_active
})
.then(() => {
// perform an action
})
})
```
---
## Step Constraints

View File

@@ -191,6 +191,10 @@ export const sidebar = sidebarAttachHrefCommonOptions(
path: "/advanced-development/workflows/constructor-constraints",
title: "Workflow Constraints",
},
{
path: "/advanced-development/workflows/conditions",
title: "Conditions in Workflows",
},
{
path: "/advanced-development/workflows/compensation-function",
title: "Compensation Function",

View File

@@ -340,12 +340,20 @@ npx medusa start
</Table.Body>
</Table>
### migrations
### migrations run
Perform migration actions to reflect changes on the database, such as running or reverting migrations.
Run the latest migrations to reflect changes on the database.
```bash
npx medusa migrations <action>
npx medusa migrations run
```
### migration revert
Revert the last migrations ran on one or more modules.
```bash
npx medusa migrations revert <module_names...>
```
#### Arguments
@@ -362,15 +370,12 @@ npx medusa migrations <action>
<Table.Row>
<Table.Cell>
`action`
`module_names`
</Table.Cell>
<Table.Cell>
The action to perform. Values can be `run`, `show`, or `revert`.
`run` is used to run the migrations; `show` is used to only show
what migrations are available to run; and `revert` is to undo the
last migration.
The name of one or more module, separated by spaces. For example, `helloModuleService`.
</Table.Cell>
<Table.Cell>