Raw
1 import { CodeTab, CodeTabs } from "docs-ui"
2
3 export const metadata = {
4 title: `${pageNumber} Link`,
5 }
6
7 # {metadata.title}
8
9 In this chapter, you’ll learn what Link is and how to use it to manage links.
10
11 <Note>
12
13 As of [Medusa v2.2.0](https://github.com/medusajs/medusa/releases/tag/v2.2.0), Remote Link has been deprecated in favor of Link. They have the same usage, so you only need to change the key used to resolve the tool from the Medusa container as explained below.
14
15 </Note>
16
17 ## What is Link?
18
19 Link is a class with utility methods to manage links between data models. It’s registered in the Medusa container under the `link` registration name.
20
21 For example:
22
23 ```ts collapsibleLines="1-9" expandButtonLabel="Show Imports"
24 import {
25 MedusaRequest,
26 MedusaResponse,
27 } from "@medusajs/framework/http"
28 import {
29 ContainerRegistrationKeys,
30 } from "@medusajs/framework/utils"
31
32 export async function POST(
33 req: MedusaRequest,
34 res: MedusaResponse
35 ): Promise<void> {
36 const link = req.scope.resolve(
37 ContainerRegistrationKeys.LINK
38 )
39
40 // ...
41 }
42 ```
43
44 You can use its methods to manage links, such as create or delete links.
45
46 ### Link Usage in Workflows
47
48 To perform link operations in a [workflow](../../workflows/page.mdx), Medusa provides the following steps out-of-the-box:
49
50 - [createRemoteLinkStep](!resources!/references/helper-steps/createRemoteLinkStep): Creates a link between records of two data models.
51 - [dismissRemoteLinkStep](!resources!/references/helper-steps/dismissRemoteLinkStep): Removes a link between records of two data models.
52 - [removeRemoteLinkStep](!resources!/references/helper-steps/removeRemoteLinkStep): Deletes all linked records whose links are defined with cascade deletion.
53 - [updateRemoteLinksStep](!resources!/references/helper-steps/updateRemoteLinksStep): Updates links between records of two data models.
54
55 The rest of this chapter provides examples using the Link class and the workflow steps.
56
57 ---
58
59 ## Create Link
60
61 To create a link between records of two data models, use the `create` method of Link.
62
63 For example:
64
65 <CodeTabs group="link">
66 <CodeTab label="Link" value="link">
67
68 ```ts
69 import { Modules } from "@medusajs/framework/utils"
70
71 // ...
72
73 await link.create({
74 [Modules.PRODUCT]: {
75 product_id: "prod_123",
76 },
77 blog: {
78 post_id: "post_123",
79 },
80 })
81 ```
82
83 </CodeTab>
84 <CodeTab label="createRemoteLinkStep" value="createRemoteLinkStep">
85
86 ```ts
87 import { Modules } from "@medusajs/framework/utils"
88 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
89 import {
90 createWorkflow,
91 transform,
92 } from "@medusajs/framework/workflows-sdk"
93
94 export const myWorkflow = createWorkflow(
95 "my-workflow",
96 () => {
97 // ...
98 const linkData = transform({
99 // TODO pass input data
100 }, () => {
101 return [
102 {
103 [Modules.PRODUCT]: {
104 product_id: "prod_123",
105 },
106 blog: {
107 post_id: "post_123",
108 },
109 },
110 ]
111 })
112 createRemoteLinkStep(linkData)
113 // ...
114 }
115 )
116 ```
117
118 </CodeTab>
119 </CodeTabs>
120
121 The `create` method accepts as a parameter an object. The object’s keys are the names of the linked modules.
122
123 <Note title="Important">
124
125 The keys (names of linked modules) must be in the same [direction](../directions/page.mdx) of the link definition.
126
127 </Note>
128
129 The value of each module’s property is an object, whose keys are of the format `{data_model_snake_name}_id`, and values are the IDs of the linked record.
130
131 So, in the example above, you link a record of the `Post` data model in a `blog` module to a `Product` record in the Product Module.
132
133 ### Enforced Integrity Constraints on Link Creation
134
135 Medusa enforces integrity constraints on links based on the link's relation type. So, an error is thrown in the following scenarios:
136
137 - If the link is one-to-one and one of the linked records already has a link to another record of the same data model. For example:
138
139 ```ts
140 // no error
141 await link.create({
142 [Modules.PRODUCT]: {
143 product_id: "prod_123",
144 },
145 blog: {
146 post_id: "post_123",
147 },
148 })
149
150 // throws an error because `prod_123` already has a link to `post_123`
151 await link.create({
152 [Modules.PRODUCT]: {
153 product_id: "prod_123",
154 },
155 blog: {
156 post_id: "mc_456",
157 },
158 })
159 ```
160
161 - If the link is one-to-many and the "one" side already has a link to another record of the same data model. For example, if a product can have many `Post` records, but a `Post` record can only have one product:
162
163 ```ts
164 // no error
165 await link.create({
166 [Modules.PRODUCT]: {
167 product_id: "prod_123",
168 },
169 blog: {
170 post_id: "post_123",
171 },
172 })
173
174 // also no error
175 await link.create({
176 [Modules.PRODUCT]: {
177 product_id: "prod_123",
178 },
179 blog: {
180 post_id: "mc_456",
181 },
182 })
183
184 // throws an error because `post_123` already has a link to `prod_123`
185 await link.create({
186 [Modules.PRODUCT]: {
187 product_id: "prod_456",
188 },
189 blog: {
190 post_id: "post_123",
191 },
192 })
193 ```
194
195 There are no integrity constraints in a many-to-many link, so you can create multiple links between the same records.
196
197 ---
198
199 ## Dismiss Link
200
201 To remove a link between records of two data models, use the `dismiss` method of Link.
202
203 For example:
204
205 <CodeTabs group="link">
206 <CodeTab label="Link" value="link">
207
208 ```ts
209 import { Modules } from "@medusajs/framework/utils"
210
211 // ...
212
213 await link.dismiss({
214 [Modules.PRODUCT]: {
215 product_id: "prod_123",
216 },
217 blog: {
218 post_id: "post_123",
219 },
220 })
221 ```
222
223 </CodeTab>
224 <CodeTab label="dismissRemoteLinkStep" value="dismissRemoteLinkStep">
225
226 ```ts
227 import { Modules } from "@medusajs/framework/utils"
228 import { dismissRemoteLinkStep } from "@medusajs/medusa/core-flows"
229 import {
230 createWorkflow,
231 transform,
232 } from "@medusajs/framework/workflows-sdk"
233
234 export const myWorkflow = createWorkflow(
235 "my-workflow",
236 () => {
237 // ...
238 const linkData = transform({
239 // TODO pass input data
240 }, () => {
241 return [
242 {
243 [Modules.PRODUCT]: {
244 product_id: "prod_123",
245 },
246 blog: {
247 post_id: "post_123",
248 },
249 },
250 ]
251 })
252 dismissRemoteLinkStep(linkData)
253 // ...
254 }
255 )
256 ```
257
258 </CodeTab>
259 </CodeTabs>
260
261 The `dismiss` method accepts the same parameter type as the [create method](#create-link).
262
263 <Note title="Important">
264
265 The keys (names of linked modules) must be in the same [direction](../directions/page.mdx) of the link definition.
266
267 </Note>
268
269 ---
270
271 ## Cascade Delete Linked Records
272
273 If you delete a record using a workflow or its module's service, use the `delete` method of Link to delete all linked records whose links are defined with [cascade deletion](../page.mdx#set-delete-cascades-on-link).
274
275 For example:
276
277 <CodeTabs group="link">
278 <CodeTab label="Link" value="link">
279
280 ```ts
281 import { Modules } from "@medusajs/framework/utils"
282
283 // ...
284
285 await productModuleService.deleteProducts(["prod_123"])
286
287 await link.delete({
288 [Modules.PRODUCT]: {
289 product_id: "prod_123",
290 },
291 })
292 ```
293
294 </CodeTab>
295 <CodeTab label="removeRemoteLinkStep" value="removeRemoteLinkStep">
296
297 ```ts
298 import { Modules } from "@medusajs/framework/utils"
299 import { removeRemoteLinkStep } from "@medusajs/medusa/core-flows"
300 import {
301 createWorkflow,
302 transform,
303 } from "@medusajs/framework/workflows-sdk"
304
305 export const myWorkflow = createWorkflow(
306 "my-workflow",
307 () => {
308 // ...
309 const linkData = transform({
310 // TODO pass input data
311 }, () => {
312 return [
313 {
314 [Modules.PRODUCT]: {
315 product_id: "prod_123",
316 },
317 },
318 ]
319 })
320 removeRemoteLinkStep(linkData)
321 // ...
322 }
323 )
324 ```
325
326 </CodeTab>
327 </CodeTabs>
328
329 This deletes all records linked to the deleted product.
330
331 ---
332
333 ## Restore Linked Records
334
335 If a record that was previously soft-deleted is now restored, use the `restore` method of Link to restore all linked records.
336
337 For example:
338
339 ```ts
340 import { Modules } from "@medusajs/framework/utils"
341
342 // ...
343
344 await productModuleService.restoreProducts(["prod_123"])
345
346 await link.restore({
347 [Modules.PRODUCT]: {
348 product_id: "prod_123",
349 },
350 })
351 ```
352
353 ---
354
355 ## Update Links
356
357 Links may have [custom columns](../custom-columns/page.mdx) to store additional information, such as a `metadata` column to store JSON data about the link.
358
359 You can update the custom columns of existing links by calling the `create` method again with the same linked records and the new values for the custom columns.
360
361 For example:
362
363 <CodeTabs group="link">
364 <CodeTab label="Link" value="link">
365
366 ```ts
367 import { Modules } from "@medusajs/framework/utils"
368
369 // ...
370
371 await link.create({
372 [Modules.PRODUCT]: {
373 product_id: "prod_123",
374 },
375 blog: {
376 post_id: "post_123",
377 },
378 data: {
379 metadata: {
380 featured: true,
381 },
382 },
383 })
384 ```
385
386 </CodeTab>
387 <CodeTab label="updateRemoteLinksStep" value="updateRemoteLinksStep">
388
389 ```ts
390 import { Modules } from "@medusajs/framework/utils"
391 import { updateRemoteLinksStep } from "@medusajs/medusa/core-flows"
392 import {
393 createWorkflow,
394 transform,
395 } from "@medusajs/framework/workflows-sdk"
396
397 export const myWorkflow = createWorkflow(
398 "my-workflow",
399 () => {
400 // ...
401 const linkData = transform({
402 // TODO pass input data
403 }, () => {
404 return [
405 {
406 [Modules.PRODUCT]: {
407 product_id: "prod_123",
408 },
409 blog: {
410 post_id: "post_123",
411 },
412 data: {
413 metadata: {
414 featured: true,
415 },
416 },
417 },
418 ]
419 })
420 updateRemoteLinksStep(linkData)
421 // ...
422 }
423 )
424 ```
425
426 </CodeTab>
427 </CodeTabs>
428
429 The object parameter you pass to the `create` method (or the `updateRemoteLinksStep` step) accepts an optional `data` property. The value of this property is an object whose keys are the names of the custom columns to update, and values are the new values for those columns.
430
431 Learn more in the [Custom Columns](../custom-columns/page.mdx) chapter.