|
1
|
--- |
|
2
|
products: |
|
3
|
- product |
|
4
|
--- |
|
5
|
|
|
6
|
import { Prerequisites } from "docs-ui" |
|
7
|
|
|
8
|
export const metadata = { |
|
9
|
title: `${pageNumber} Guide: Define Module Link Between Brand and Product`, |
|
10
|
} |
|
11
|
|
|
12
|
# {metadata.title} |
|
13
|
|
|
14
|
In this chapter, you'll learn how to define a module link between a brand defined in the [custom Brand Module](../../custom-features/module/page.mdx), and a product defined in the [Product Module](!resources!/commerce-modules/product) that's available in your Medusa application out-of-the-box. |
|
15
|
|
|
16
|
Modules are [isolated](../../../fundamentals/modules/isolation/page.mdx) from other resources, ensuring that they're integrated into the Medusa application without side effects. However, you may need to build relations between data models of different modules, or you're trying to extend data models from [Commerce Modules](!resources!/commerce-modules) with custom properties. To do that, you define module links. |
|
17
|
|
|
18
|
A [module link](../../../fundamentals/module-links/page.mdx) forms an association between two data models of different modules while maintaining module isolation. You can then manage and query linked records of the data models using Medusa's Modules SDK. |
|
19
|
|
|
20
|
In this chapter, you'll define a module link between the `Brand` data model of the Brand Module, and the `Product` data model of the Product Module. In later chapters, you'll manage and retrieve linked product and brand records. |
|
21
|
|
|
22
|
<Prerequisites |
|
23
|
items={[ |
|
24
|
{ |
|
25
|
text: "Brand Module having a Brand data model", |
|
26
|
link: "/learn/customization/custom-features/module" |
|
27
|
} |
|
28
|
]} |
|
29
|
/> |
|
30
|
|
|
31
|
## 1. Define Link |
|
32
|
|
|
33
|
Links are defined in a TypeScript or JavaScript file under the `src/links` directory. The file defines and exports the link using `defineLink` from the Modules SDK. |
|
34
|
|
|
35
|
So, to define a link between the `Product` and `Brand` models, create the file `src/links/product-brand.ts` with the following content: |
|
36
|
|
|
37
|
 |
|
38
|
|
|
39
|
export const highlights = [ |
|
40
|
["7", "linkable", "Special `linkable` property that holds the linkable data models of `ProductModule`."], |
|
41
|
["10", "linkable", "Special `linkable` property that holds the linkable data models of `BrandModule`."], |
|
42
|
] |
|
43
|
|
|
44
|
```ts title="src/links/product-brand.ts" highlights={highlights} |
|
45
|
import BrandModule from "../modules/brand" |
|
46
|
import ProductModule from "@medusajs/medusa/product" |
|
47
|
import { defineLink } from "@medusajs/framework/utils" |
|
48
|
|
|
49
|
export default defineLink( |
|
50
|
{ |
|
51
|
linkable: ProductModule.linkable.product, |
|
52
|
isList: true, |
|
53
|
}, |
|
54
|
BrandModule.linkable.brand |
|
55
|
) |
|
56
|
``` |
|
57
|
|
|
58
|
You import each module's definition object from the `index.ts` file of the module's directory. Each module object has a special `linkable` property that holds the data models' link configurations. |
|
59
|
|
|
60
|
The `defineLink` function accepts two parameters of the same type, which is either: |
|
61
|
|
|
62
|
- The data model's link configuration, which you access from the Module's `linkable` property; |
|
63
|
- Or an object that has two properties: |
|
64
|
- `linkable`: the data model's link configuration, which you access from the Module's `linkable` property. |
|
65
|
- `isList`: A boolean indicating whether many records of the data model can be linked to the other model. |
|
66
|
|
|
67
|
So, in the above code snippet, you define a link between the `Product` and `Brand` data models. Since a brand can be associated with multiple products, you enable `isList` in the `Product` model's object. |
|
68
|
|
|
69
|
--- |
|
70
|
|
|
71
|
## 2. Sync the Link to the Database |
|
72
|
|
|
73
|
A module link is represented in the database as a table that stores the IDs of linked records. So, after defining the link, run the following command to create the module link's table in the database: |
|
74
|
|
|
75
|
```bash npx2yarn |
|
76
|
npx medusa db:migrate |
|
77
|
``` |
|
78
|
|
|
79
|
This command reflects migrations on the database and syncs module links, which creates a table for the `product-brand` link. |
|
80
|
|
|
81
|
<Note title="Tip"> |
|
82
|
|
|
83
|
You can also run the `npx medusa db:sync-links` to just sync module links without running migrations. |
|
84
|
|
|
85
|
</Note> |
|
86
|
|
|
87
|
--- |
|
88
|
|
|
89
|
## Next Steps: Extend Create Product Flow |
|
90
|
|
|
91
|
In the next chapter, you'll extend Medusa's workflow and API route that create a product to allow associating a brand with a product. You'll also learn how to link brand and product records. |