Raw
1 import { CodeTab, CodeTabs } from "docs-ui"
2
3 export const metadata = {
4 title: `${pageNumber} Add Columns to a Link Table`,
5 }
6
7 # {metadata.title}
8
9 In this chapter, you'll learn how to add custom columns to a link definition's table and manage them.
10
11 ## Link Table's Default Columns
12
13 When you define a link between two data models, Medusa creates a link table in the database to store the IDs of the linked records. You can learn more about the created table in the [Module Links chapter](../page.mdx).
14
15 In various cases, you might need to store additional data in the link table. For example, if you define a link between a `product` and a `post`, you might want to store the publish date of the product's post in the link table.
16
17 In those cases, you can add a custom column to a link's table in the link definition. You can later set that column whenever you create or update a link between the linked records.
18
19 ---
20
21 ## How to Add Custom Columns to a Link's Table?
22
23 The `defineLink` function used to define a link accepts a third parameter, which is an object of options.
24
25 To add custom columns to a link's table, pass in the third parameter of `defineLink` a `database` property:
26
27 export const linkHighlights = [
28 ["10", "extraColumns", "Custom columns to add to the created link's table."],
29 ["11", "metadata", "The column's name."],
30 ["12", "type", "The column's type."]
31 ]
32
33 ```ts highlights={linkHighlights}
34 import BlogModule from "../modules/blog"
35 import ProductModule from "@medusajs/medusa/product"
36 import { defineLink } from "@medusajs/framework/utils"
37
38 export default defineLink(
39 ProductModule.linkable.product,
40 BlogModule.linkable.blog,
41 {
42 database: {
43 extraColumns: {
44 metadata: {
45 type: "json",
46 },
47 },
48 },
49 }
50 )
51 ```
52
53 This adds to the table created for the link between `product` and `blog` a `metadata` column of type `json`.
54
55 ### Database Options
56
57 The `database` property defines configuration for the table created in the database.
58
59 Its `extraColumns` property defines custom columns to create in the link's table.
60
61 `extraColumns`'s value is an object whose keys are the names of the columns, and values are the column's configurations as an object.
62
63 ### Column Configurations
64
65 The column's configurations object accepts the following properties:
66
67 - `type`: The column's type. Possible values are:
68 - `string`
69 - `text`
70 - `integer`
71 - `boolean`
72 - `date`
73 - `time`
74 - `datetime`
75 - `enum`
76 - `json`
77 - `array`
78 - `enumArray`
79 - `float`
80 - `double`
81 - `decimal`
82 - `bigint`
83 - `mediumint`
84 - `smallint`
85 - `tinyint`
86 - `blob`
87 - `uuid`
88 - `uint8array`
89 - `defaultValue`: The column's default value.
90 - `nullable`: Whether the column can have `null` values.
91
92 ---
93
94 ## Set Custom Column when Creating Link
95
96 The object you pass to Link's `create` method accepts a `data` property. Its value is an object whose keys are custom column names, and values are the value of the custom column for this link.
97
98 For example:
99
100 <Note>
101
102 Refer to the [Link](../link/page.mdx) chapter to learn how to import and use the `link` instance.
103
104 </Note>
105
106 <CodeTabs group="link">
107 <CodeTab label="Link" value="link">
108
109 ```ts
110 await link.create({
111 [Modules.PRODUCT]: {
112 product_id: "123",
113 },
114 [BLOG_MODULE]: {
115 post_id: "321",
116 },
117 data: {
118 metadata: {
119 test: true,
120 },
121 },
122 })
123 ```
124
125 </CodeTab>
126 <CodeTab label="createRemoteLinksStep" value="createRemoteLinksStep">
127
128 ```ts
129 import { Modules } from "@medusajs/framework/utils"
130 import { BLOG_MODULE } from "../modules/blog"
131 import { createRemoteLinksStep } from "@medusajs/medusa/core-flows"
132 import {
133 createWorkflow,
134 transform,
135 } from "@medusajs/framework/workflows-sdk"
136
137 export const myWorkflow = createWorkflow(
138 "my-workflow",
139 () => {
140 // ...
141 const linkData = transform({
142 // TODO pass input data
143 }, () => {
144 return [
145 {
146 [Modules.PRODUCT]: {
147 product_id: "123",
148 },
149 [BLOG_MODULE]: {
150 post_id: "321",
151 },
152 data: {
153 metadata: {
154 test: true,
155 },
156 },
157 },
158 ]
159 })
160 createRemoteLinksStep(linkData)
161 // ...
162 }
163 )
164 ```
165
166 </CodeTab>
167 </CodeTabs>
168
169 ---
170
171 ## Retrieve Custom Column with Link
172
173 To retrieve linked records with their custom columns, use [Query](../../query/page.mdx). This section explores two methods to retrieve a link's custom columns.
174
175 ### Method 1: Using Special Link Field
176
177 A data model will have a special field for all its link tables. The field's name is in the `{camel_case_data_model_name}_link` format, where `{camel_case_data_model_name}` is the name of the linked data model in camel case.
178
179 For example:
180
181 export const method1Highlights = [
182 ["3", `"post_link.metadata"`, "Retrieve the `metadata` custom column."]
183 ]
184
185 ```ts highlights={method1Highlights}
186 const { data } = await query.graph({
187 entity: "product",
188 fields: ["id", "title", "post_link.metadata", "post.*"],
189 filters: {
190 id: "prod_123",
191 },
192 })
193 ```
194
195 In this example, you retrieve a product and the `metadata` custom column in the link table between `product` and `post` using the `post_link.metadata` field.
196
197 You can also retrieve all custom columns in the link table using the `post_link.*` field.
198
199 ### Method 2: Using Entry Point
200
201 A module link's definition, exported by a file under `src/links`, has a special `entryPoint` property. Use this property when specifying the `entity` property in Query's `graph` method.
202
203 For example:
204
205 export const retrieveHighlights = [
206 ["1", "productPostLink", "Import the exported link definition."],
207 ["6", "entity", "Pass the link definition to retrieve its data."],
208 ["7", `"metadata"`, "Retrieve the `metadata` column."],
209 ["7", `"product.*"`, "Retrieve the linked product's details."],
210 ["7", `"post.*"`, "Retrieve the linked `post` record's details."],
211 ]
212
213 ```ts highlights={retrieveHighlights}
214 import productPostLink from "../links/product-post"
215
216 // ...
217
218 const { data } = await query.graph({
219 entity: productPostLink.entryPoint,
220 fields: ["metadata", "product.*", "post.*"],
221 filters: {
222 product_id: "prod_123",
223 },
224 })
225 ```
226
227 This retrieves the product of id `prod_123` and its linked `post` records.
228
229 In the `fields` array you pass `metadata`, which is the custom column to retrieve of the link.
230
231 ---
232
233 ## Update Custom Column's Value
234
235 Link's `create` method updates a link's data if the link between the specified records already exists.
236
237 So, to update the value of a custom column in a created link, use the `create` method again passing it a new value for the custom column.
238
239 For example:
240
241 <CodeTabs group="link">
242 <CodeTab label="Link" value="link">
243
244 ```ts
245 await link.create({
246 [Modules.PRODUCT]: {
247 product_id: "123",
248 },
249 [BLOG_MODULE]: {
250 post_id: "321",
251 },
252 data: {
253 metadata: {
254 test: false,
255 },
256 },
257 })
258 ```
259
260 </CodeTab>
261 <CodeTab label="updateRemoteLinksStep" value="updateRemoteLinksStep">
262
263 ```ts
264 import { Modules } from "@medusajs/framework/utils"
265 import { BLOG_MODULE } from "../modules/blog"
266 import { updateRemoteLinksStep } from "@medusajs/medusa/core-flows"
267 import {
268 createWorkflow,
269 transform,
270 } from "@medusajs/framework/workflows-sdk"
271
272 export const myWorkflow = createWorkflow(
273 "my-workflow",
274 () => {
275 // ...
276 const linkData = transform({
277 // TODO pass input data
278 }, () => {
279 return [
280 {
281 [Modules.PRODUCT]: {
282 product_id: "123",
283 },
284 [BLOG_MODULE]: {
285 post_id: "321",
286 },
287 data: {
288 metadata: {
289 test: false,
290 },
291 },
292 },
293 ]
294 })
295 updateRemoteLinksStep(linkData)
296 // ...
297 }
298 )
299 ```
300
301 </CodeTab>
302 </CodeTabs>