docs: add note about using dates in workflows (#10107)

This commit is contained in:
Shahed Nasser
2024-11-15 18:42:56 +02:00
committed by GitHub
parent 2822972e04
commit 12e8405cca
3 changed files with 70 additions and 4 deletions
@@ -34,7 +34,7 @@ You cant directly manipulate variables within the workflow's constructor func
<Note>
Learn more about why you can't manipulate variables [in this chapter](../conditions/page.mdx#why-if-conditions-arent-allowed-in-workflows)
Learn more about why you can't manipulate variables [in this chapter](../variable-manipulation/page.mdx)
</Note>
@@ -79,6 +79,49 @@ const myWorkflow = createWorkflow(
})
```
### Create Dates in transform
When you use `new Date()` in a workflow's constructor function, the date is evaluated when Medusa creates the internal representation of the workflow, not during execution.
Instead, create the date using `transform`.
<Note>
Learn more about how Medusa creates an internal representation of a workflow [in this chapter](../variable-manipulation/page.mdx).
</Note>
For example:
export const dateHighlights = [
["5", "new Date()", "Don't create a date directly in the constructor function."],
["16", "transform", "Use the `transform` function to create a date variable."]
]
```ts highlights={dateHighlights}
// Don't
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
const today = new Date()
return new WorkflowResponse({
today
})
})
// Do
const myWorkflow = createWorkflow(
"hello-world",
function (input: WorkflowInput) {
const today = transform({}, () => new Date())
return new WorkflowResponse({
today
})
})
```
### No If Conditions
You can't use if-conditions in a workflow.
@@ -6,7 +6,7 @@ export const metadata = {
In this chapter, you'll learn how to use the `transform` utility to manipulate variables in a workflow.
## Why Variable Manipulation isn't Allowed in Workflows?
## Why Variable Manipulation isn't Allowed in Workflows
Medusa creates an internal representation of the workflow definition you pass to `createWorkflow` to track and store its steps.
@@ -117,6 +117,29 @@ You then pass the `ids` variable as a parameter to the `doSomethingStep`.
---
## Example: Creating a Date
If you create a date with `new Date()` in a workflow's constructor function, Medusa evaluates the date's value when it creates the internal representation of the workflow, not when the workflow is executed.
So, use `transform` instead to create a date variable with `new Date()`.
For example:
```ts
const myWorkflow = createWorkflow(
"hello-world",
() => {
const today = transform({}, () => new Date())
doSomethingStep(today)
}
)
```
In this workflow, `today` is only evaluated when the workflow is executed.
---
## Caveats
### Transform Evaluation