Skip to content

Commit

Permalink
docs: add note about using dates in workflows (#10107)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser authored Nov 15, 2024
1 parent 2822972 commit 12e8405
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ You can’t 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>

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions www/apps/book/generated/edit-dates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const generatedEditDates = {
"app/learn/advanced-development/modules/module-link-directions/page.mdx": "2024-07-24T09:16:01+02:00",
"app/learn/advanced-development/admin/page.mdx": "2024-10-07T12:39:13.178Z",
"app/learn/advanced-development/workflows/long-running-workflow/page.mdx": "2024-09-30T08:43:53.129Z",
"app/learn/advanced-development/workflows/constructor-constraints/page.mdx": "2024-10-04T08:40:14.867Z",
"app/learn/advanced-development/workflows/constructor-constraints/page.mdx": "2024-11-14T16:13:19.234Z",
"app/learn/advanced-development/data-models/write-migration/page.mdx": "2024-07-15T17:46:10+02:00",
"app/learn/advanced-development/data-models/manage-relationships/page.mdx": "2024-09-10T11:39:51.167Z",
"app/learn/advanced-development/modules/remote-query/page.mdx": "2024-07-21T21:20:24+02:00",
Expand Down Expand Up @@ -88,7 +88,7 @@ export const generatedEditDates = {
"app/learn/debugging-and-testing/instrumentation/page.mdx": "2024-09-17T08:53:15.910Z",
"app/learn/advanced-development/api-routes/additional-data/page.mdx": "2024-09-30T08:43:53.120Z",
"app/learn/advanced-development/workflows/page.mdx": "2024-09-18T08:00:57.364Z",
"app/learn/advanced-development/workflows/variable-manipulation/page.mdx": "2024-11-11T13:33:41.270Z",
"app/learn/advanced-development/workflows/variable-manipulation/page.mdx": "2024-11-14T16:11:24.538Z",
"app/learn/customization/custom-features/api-route/page.mdx": "2024-09-12T12:42:34.201Z",
"app/learn/customization/custom-features/module/page.mdx": "2024-10-16T08:49:44.676Z",
"app/learn/customization/custom-features/workflow/page.mdx": "2024-09-30T08:43:53.133Z",
Expand Down

0 comments on commit 12e8405

Please sign in to comment.