|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Data Manipulation in Workflows with transform`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you'll learn how to use `transform` from the Workflows SDK to manipulate data and variables in a workflow. |
|
8
|
|
|
9
|
## Why Variable Manipulation isn't Allowed in Workflows |
|
10
|
|
|
11
|
Medusa creates an internal representation of the workflow definition you pass to `createWorkflow` to track and store its steps. |
|
12
|
|
|
13
|
At that point, variables in the workflow don't have any values. They only do when you execute the workflow. |
|
14
|
|
|
15
|
So, you can only pass variables as parameters to steps. But, in a workflow, you can't change a variable's value or, if the variable is an array, loop over its items. |
|
16
|
|
|
17
|
Instead, use `transform` from the Workflows SDK. |
|
18
|
|
|
19
|
<Note title="Tip"> |
|
20
|
|
|
21
|
Restrictions for variable manipulation is only applicable in a workflow's definition. You can still manipulate variables in your step's code. |
|
22
|
|
|
23
|
</Note> |
|
24
|
|
|
25
|
--- |
|
26
|
|
|
27
|
## What is the transform Utility? |
|
28
|
|
|
29
|
`transform` creates a new variable as the result of manipulating other variables. |
|
30
|
|
|
31
|
For example, consider you have two strings as the output of two steps: |
|
32
|
|
|
33
|
```ts |
|
34
|
const str1 = step1() |
|
35
|
const str2 = step2() |
|
36
|
``` |
|
37
|
|
|
38
|
To concatenate the strings, you create a new variable `str3` using the `transform` function: |
|
39
|
|
|
40
|
export const highlights = [ |
|
41
|
["14", "str3", "Holds the result returned by `transform`'s second parameter function."], |
|
42
|
["15", "", "Specify the data to pass as a parameter to the function in the second parameter."], |
|
43
|
["16", "data", "The data passed in the first parameter of `transform`."], |
|
44
|
["16", "`${data.str1}${data.str2}`", "Return the concatenated strings."] |
|
45
|
] |
|
46
|
|
|
47
|
```ts highlights={highlights} |
|
48
|
import { |
|
49
|
createWorkflow, |
|
50
|
WorkflowResponse, |
|
51
|
transform, |
|
52
|
} from "@medusajs/framework/workflows-sdk" |
|
53
|
// step imports... |
|
54
|
|
|
55
|
const myWorkflow = createWorkflow( |
|
56
|
"hello-world", |
|
57
|
function (input) { |
|
58
|
const str1 = step1(input) |
|
59
|
const str2 = step2(input) |
|
60
|
|
|
61
|
const str3 = transform( |
|
62
|
{ str1, str2 }, |
|
63
|
(data) => `${data.str1}${data.str2}` |
|
64
|
) |
|
65
|
|
|
66
|
return new WorkflowResponse(str3) |
|
67
|
} |
|
68
|
) |
|
69
|
``` |
|
70
|
|
|
71
|
`transform` accepts two parameters: |
|
72
|
|
|
73
|
1. The first parameter is an object of variables to manipulate. The object is passed as a parameter to `transform`'s second parameter function. |
|
74
|
2. The second parameter is the function performing the variable manipulation. |
|
75
|
|
|
76
|
The value returned by the second parameter function is returned by `transform`. So, the `str3` variable holds the concatenated string. |
|
77
|
|
|
78
|
You can use the returned value in the rest of the workflow, either to pass it as an input to other steps or to return it in the workflow's response. |
|
79
|
|
|
80
|
--- |
|
81
|
|
|
82
|
## Example: Looping Over Array |
|
83
|
|
|
84
|
Use `transform` to loop over arrays to create another variable from the array's items. |
|
85
|
|
|
86
|
For example: |
|
87
|
|
|
88
|
```ts collapsibleLines="1-7" expandButtonLabel="Show Imports" |
|
89
|
import { |
|
90
|
createWorkflow, |
|
91
|
WorkflowResponse, |
|
92
|
transform, |
|
93
|
} from "@medusajs/framework/workflows-sdk" |
|
94
|
// step imports... |
|
95
|
|
|
96
|
type WorkflowInput = { |
|
97
|
items: { |
|
98
|
id: string |
|
99
|
name: string |
|
100
|
}[] |
|
101
|
} |
|
102
|
|
|
103
|
const myWorkflow = createWorkflow( |
|
104
|
"hello-world", |
|
105
|
function ({ items }: WorkflowInput) { |
|
106
|
const ids = transform( |
|
107
|
{ items }, |
|
108
|
(data) => data.items.map((item) => item.id) |
|
109
|
) |
|
110
|
|
|
111
|
doSomethingStep(ids) |
|
112
|
|
|
113
|
// ... |
|
114
|
} |
|
115
|
) |
|
116
|
``` |
|
117
|
|
|
118
|
This workflow receives an `items` array in its input. |
|
119
|
|
|
120
|
You use `transform` to create an `ids` variable, which is an array of strings holding the `id` of each item in the `items` array. |
|
121
|
|
|
122
|
You then pass the `ids` variable as a parameter to the `doSomethingStep`. |
|
123
|
|
|
124
|
--- |
|
125
|
|
|
126
|
## Example: Creating a Date |
|
127
|
|
|
128
|
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. |
|
129
|
|
|
130
|
So, use `transform` instead to create a date variable with `new Date()`. |
|
131
|
|
|
132
|
For example: |
|
133
|
|
|
134
|
```ts |
|
135
|
const myWorkflow = createWorkflow( |
|
136
|
"hello-world", |
|
137
|
() => { |
|
138
|
const today = transform({}, () => new Date()) |
|
139
|
|
|
140
|
doSomethingStep(today) |
|
141
|
} |
|
142
|
) |
|
143
|
``` |
|
144
|
|
|
145
|
In this workflow, `today` is only evaluated when the workflow is executed. |
|
146
|
|
|
147
|
--- |
|
148
|
|
|
149
|
## Caveats |
|
150
|
|
|
151
|
### Transform Evaluation |
|
152
|
|
|
153
|
`transform`'s value is only evaluated if you pass its output to a step or in the workflow response. |
|
154
|
|
|
155
|
For example, if you have the following workflow: |
|
156
|
|
|
157
|
```ts |
|
158
|
const myWorkflow = createWorkflow( |
|
159
|
"hello-world", |
|
160
|
function (input) { |
|
161
|
const str = transform( |
|
162
|
{ input }, |
|
163
|
(data) => `${data.input.str1}${data.input.str2}` |
|
164
|
) |
|
165
|
|
|
166
|
return new WorkflowResponse("done") |
|
167
|
} |
|
168
|
) |
|
169
|
``` |
|
170
|
|
|
171
|
Since `str`'s value isn't used as a step's input or passed to `WorkflowResponse`, its value is never evaluated. |
|
172
|
|
|
173
|
### Data Validation |
|
174
|
|
|
175
|
`transform` should only be used to perform variable or data manipulation. |
|
176
|
|
|
177
|
If you want to perform some validation on the data, use a step or [when-then](../conditions/page.mdx) instead. |
|
178
|
|
|
179
|
For example: |
|
180
|
|
|
181
|
```ts |
|
182
|
// DON'T |
|
183
|
const myWorkflow = createWorkflow( |
|
184
|
"hello-world", |
|
185
|
function (input) { |
|
186
|
const str = transform( |
|
187
|
{ input }, |
|
188
|
(data) => { |
|
189
|
if (!input.str1) { |
|
190
|
throw new Error("Not allowed!") |
|
191
|
} |
|
192
|
} |
|
193
|
) |
|
194
|
} |
|
195
|
) |
|
196
|
|
|
197
|
// DO |
|
198
|
const validateHasStr1Step = createStep( |
|
199
|
"validate-has-str1", |
|
200
|
({ input }) => { |
|
201
|
if (!input.str1) { |
|
202
|
throw new Error("Not allowed!") |
|
203
|
} |
|
204
|
} |
|
205
|
) |
|
206
|
|
|
207
|
const myWorkflow = createWorkflow( |
|
208
|
"hello-world", |
|
209
|
function (input) { |
|
210
|
validateHasStr1({ |
|
211
|
input, |
|
212
|
}) |
|
213
|
|
|
214
|
// workflow continues its execution only if |
|
215
|
// the step doesn't throw the error. |
|
216
|
} |
|
217
|
) |
|
218
|
``` |