1 export const metadata = {
2 title: `${pageNumber} Define Module Link`,
3 }
4
5 # {metadata.title}
6
7 In this chapter, you’ll learn what a module link is and how to define one.
8
9 ## What is a Module Link?
10
11 Medusa's modular architecture isolates modules from one another to ensure they can be integrated into your application without side effects. Module isolation has other benefits, which you can learn about in the [Module Isolation chapter](../modules/isolation/page.mdx). Since modules are isolated, you can't access another module's data models to add a relation to it or extend it. Instead, you use a module link.
12
13 A module link forms an association between two data models of different modules while maintaining module isolation. Using module links, you can build virtual relations between your custom data models and data models in the Commerce Modules, which is useful as you extend the features provided by the Commerce Modules. Then, Medusa creates a link table in the database to store the IDs of the linked records. You'll learn more about link tables later in this chapter.
14
15 For example, the [Brand Customizations Tutorial](../../customization/extend-features/page.mdx) shows how to create a Brand Module that adds the concept of brands to your application, then link those brands to a product.
16
17 ---
18
19 ## How to Define a Module Link?
20
21 ### 1. Create Link File
22
23 Module links are defined in a TypeScript or JavaScript file under the `src/links` directory. The file defines the link using `defineLink` from the Modules SDK and exports it.
24
25 For example:
26
27 export const highlights = [
28 ["6", "linkable", "Special `linkable` property that holds the linkable data models of `BlogModule`."],
29 ["7", "linkable", "Special `linkable` property that holds the linkable data models of `ProductModule`."],
30 ]
31
32 ```ts title="src/links/blog-product.ts" highlights={highlights}
33 import BlogModule from "../modules/blog"
34 import ProductModule from "@medusajs/medusa/product"
35 import { defineLink } from "@medusajs/framework/utils"
36
37 export default defineLink(
38 ProductModule.linkable.product,
39 BlogModule.linkable.post
40 )
41 ```
42
43 The `defineLink` function accepts as parameters the link configurations of each module's data model. A module has a special `linkable` property that holds these configurations for its data models.
44
45 In this example, you define a module link between the `blog` module's `post` data model and the Product Module's `Product` data model.
46
47 ### 2. Sync Links
48
49 After defining the link, run the `db:sync-links` command:
50
51 ```bash npx2yarn
52 npx medusa db:sync-links
53 ```
54
55 The Medusa application creates a new table for your module link to store the IDs of linked records.
56
57 You can also use the `db:migrate` command, which runs both the migrations and syncs the links.
58
59 Use either of these commands whenever you make changes to your link definitions. For example, run this command if you remove your link definition file.
60
61 ---
62
63 ### Module Link's Database Table
64
65 When you define a module link, the Medusa application creates a table in the database for that module link. The table's name is a combination of the names of the two data models linked in the format `module1_table1_module2_table2`, where:
66
67 - `module1` and `module2` are the names of the modules.
68 - `table1` and `table2` are the table names of the data models.
69
70 For example, if you define a link between the `Product` data model from the [Product Module](!resources!/commerce-modules/product) and a `Post` data model from a Blog Module, the table name would be `product_product_blog_post`.
71
72 The table has two columns, each storing the ID of a record from the linked data models. For example, the `product_product_blog_post` table would have columns `product_id` and `post_id`. These columns store only the IDs of the linked records and do not hold a foreign key constraint.
73
74 Then, when you create links between records of the data models, the IDs of these data models are stored as a new record in the link's table.
75
76 <Note title="Tip">
77
78 You can also add custom columns in the link table as explained in the [Add Columns to Link Table chapter](./custom-columns/page.mdx).
79
80 </Note>
81
82 ![Diagram illustration for module links](https://res.cloudinary.com/dza7lstvk/image/upload/v1741696766/Medusa%20Book/custom-links_vezsx8.jpg)
83
84 ---
85
86 ## When to Use Module Links
87
88 <Note title="Use module links when" type="success">
89
90 - You want to create a relation between data models from different modules.
91 - You want to extend the data model of another module.
92
93 </Note>
94
95 <Note title="Don't use module links if" type="error">
96
97 You want to create a relationship between data models in the same module. Use data model relationships instead.
98
99 </Note>
100
101 ---
102
103 ## Define a List Module Link
104
105 By default, a module link establishes a one-to-one relation: a record of a data model is linked to one record of the other data model.
106
107 To specify that a data model can have multiple of its records linked to the other data model's record, use the `isList` option.
108
109 For example:
110
111 ```ts
112 import BlogModule from "../modules/blog"
113 import ProductModule from "@medusajs/medusa/product"
114 import { defineLink } from "@medusajs/framework/utils"
115
116 export default defineLink(
117 ProductModule.linkable.product,
118 {
119 linkable: BlogModule.linkable.post,
120 isList: true,
121 }
122 )
123 ```
124
125 In this case, you pass an object of configuration as a parameter instead. The object accepts the following properties:
126
127 - `linkable`: The data model's link configuration.
128 - `alias`: A custom alias to use when querying the linked records. By default, Medusa derives the alias from the data model's name. Available since Medusa [v2.17.2](https://github.com/medusajs/medusa/releases/tag/v2.17.2).
129 - `isList`: Whether multiple records can be linked to one record of the other data model.
130
131 In this example, a record of `product` can be linked to more than one record of `post`.
132
133 ### Many-to-Many Module Link
134
135 Your module link can also establish a many-to-many relation between the linked data models. To do this, enable `isList` on both sides of the link.
136
137 For example:
138
139 ```ts
140 import BlogModule from "../modules/blog"
141 import ProductModule from "@medusajs/medusa/product"
142 import { defineLink } from "@medusajs/framework/utils"
143
144 export default defineLink(
145 {
146 linkable: ProductModule.linkable.product,
147 isList: true,
148 },
149 {
150 linkable: BlogModule.linkable.post,
151 isList: true,
152 }
153 )
154 ```
155
156 ---
157
158 ## Set Delete Cascades on Link
159
160 To enable delete cascade on a link so that when a record is deleted with [link.delete](./link/page.mdx#cascade-delete-linked-records), its linked records are also deleted, pass the `deleteCascade` property in the object passed to `defineLink`.
161
162 For example:
163
164 ```ts
165 import BlogModule from "../modules/blog"
166 import ProductModule from "@medusajs/medusa/product"
167 import { defineLink } from "@medusajs/framework/utils"
168
169 export default defineLink(
170 ProductModule.linkable.product,
171 {
172 linkable: BlogModule.linkable.post,
173 deleteCascade: true,
174 }
175 )
176 ```
177
178 In this example, when a product is deleted with [link.delete](./link/page.mdx#cascade-delete-linked-records), all linked blog posts are also deleted.
179
180 ---
181
182 ## Override Link Alias
183
184 When you define a link using the object form, Medusa derives the query alias for the linked data model from the data model's name. You can override this alias by passing the `alias` property.
185
186 <Note>
187
188 This option is available since Medusa [v2.17.2](https://github.com/medusajs/medusa/releases/tag/v2.17.2).
189
190 </Note>
191
192 This is useful when you link the same module twice in different link definitions and need to differentiate the two links when querying.
193
194 For example:
195
196 export const aliasHighlights = [
197 ["9", "alias", "Override the alias used when querying linked records."],
198 ]
199
200 ```ts highlights={aliasHighlights}
201 import BlogModule from "../modules/blog"
202 import ProductModule from "@medusajs/medusa/product"
203 import { defineLink } from "@medusajs/framework/utils"
204
205 export default defineLink(
206 ProductModule.linkable.product,
207 {
208 linkable: BlogModule.linkable.post,
209 alias: "featured_post",
210 }
211 )
212 ```
213
214 In this example, you override the alias of the `post` data model to `featured_post`. When you query linked records, use `featured_post` as the relation name:
215
216 ```ts
217 const { result } = await query.graph({
218 entity: "product",
219 fields: ["id", "featured_post.*"],
220 })
221 ```
222
223 ---
224
225 ## Renaming Participants in a Module Link
226
227 As mentioned in the [Module Link's Database Table](#module-links-database-table) section, the name of a link's table consists of the names of the modules and the data models' table names.
228
229 So, if you rename a module or a data model's table, then run the `db:sync-links` or `db:migrate` commands, you'll be asked to delete the old link table and create a new one.
230
231 <Note title="Tip">
232
233 A data model's table name is passed in the first parameter of `model.define`, and a module's name is passed in the first parameter of `Module` in the module's `index.ts` file.
234
235 </Note>
236
237 For example, if you have the link table `product_product_blog_post` and you rename the Blog Module from `blog` to `article`, Medusa considers the old link definition deleted. Then, when you run the `db:sync-links` or `db:migrate` command, Medusa will ask if you want to delete the old link table, and will create a new one with the new name `product_product_article_post`.
238
239 To resolve this, you can rename the link table in the link definition.
240
241 ### Rename Link Table
242
243 If you need to rename a module or its data model's table, you can persist the old name by passing a third parameter to `defineLink`. This parameter is an object of additional configurations. It accepts a `database` property that allows you to configure the link's table name.
244
245 For example, after renaming the Blog Module to `article`, you can persist the old name `blog` in the link table name:
246
247 export const renameHighlights = [
248 ["13", `"product_product_blog_post"`, "Specify the old link table name to persist when renaming a module or data model's table."]
249 ]
250
251 ```ts highlights={renameHighlights}
252 import ArticleModule from "../modules/article"
253 import ProductModule from "@medusajs/medusa/product"
254 import { defineLink } from "@medusajs/framework/utils"
255
256 export default defineLink(
257 ProductModule.linkable.product,
258 {
259 linkable: ArticleModule.linkable.post,
260 isList: true,
261 },
262 {
263 database: {
264 table: "product_product_blog_post",
265 },
266 }
267 )
268 ```
269
270 In this example, you set the `table` property in the `database` object to the old link table name `product_product_blog_post`, ensuring that the old link table is not deleted.
271
272 This is enough to rename the link table when you rename a module. If you renamed a data model's table, you need to also run the `db:sync-links` or `db:migrate` commands, which will update the column names in the link table automatically:
273
274 ```bash npx2yarn
275 npx medusa db:migrate
276 ```
277
278 ---
279
280 ## Delete Module Link Definition
281
282 To delete a module link definition, remove the link file from the `src/links` directory. Then, run the `db:sync-links` or `db:migrate` command to delete the link table from the database:
283
284 ```bash npx2yarn
285 npx medusa db:migrate
286 ```