diff --git a/www/apps/book/app/learn/advanced-development/workflows/constructor-constraints/page.mdx b/www/apps/book/app/learn/advanced-development/workflows/constructor-constraints/page.mdx index 6223860ebacb9..c98b67ef86522 100644 --- a/www/apps/book/app/learn/advanced-development/workflows/constructor-constraints/page.mdx +++ b/www/apps/book/app/learn/advanced-development/workflows/constructor-constraints/page.mdx @@ -34,7 +34,7 @@ You can’t directly manipulate variables within the workflow's constructor func -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) @@ -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`. + + + +Learn more about how Medusa creates an internal representation of a workflow [in this chapter](../variable-manipulation/page.mdx). + + + +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. diff --git a/www/apps/book/app/learn/advanced-development/workflows/variable-manipulation/page.mdx b/www/apps/book/app/learn/advanced-development/workflows/variable-manipulation/page.mdx index 42a41ce84cafc..d911e3462775e 100644 --- a/www/apps/book/app/learn/advanced-development/workflows/variable-manipulation/page.mdx +++ b/www/apps/book/app/learn/advanced-development/workflows/variable-manipulation/page.mdx @@ -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 diff --git a/www/apps/book/generated/edit-dates.mjs b/www/apps/book/generated/edit-dates.mjs index 1b0f767355a7c..a13abcbac59ed 100644 --- a/www/apps/book/generated/edit-dates.mjs +++ b/www/apps/book/generated/edit-dates.mjs @@ -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", @@ -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",