1 import { CodeTabs, CodeTab, Table } from "docs-ui"
2
3 export const metadata = {
4 title: `Links between Inventory Module and Other Modules`,
5 }
6
7 # {metadata.title}
8
9 This document showcases the module links defined between the Inventory Module and other Commerce Modules.
10
11 ## Summary
12
13 The Inventory Module has the following links to other modules:
14
15 <Note title="Tip">
16
17 Read-only links are used to query data across modules, but the relations aren't stored in a pivot table in the database.
18
19 </Note>
20
21 <Table>
22 <Table.Header>
23 <Table.Row>
24 <Table.HeaderCell>
25 First Data Model
26 </Table.HeaderCell>
27 <Table.HeaderCell>
28 Second Data Model
29 </Table.HeaderCell>
30 <Table.HeaderCell>
31 Type
32 </Table.HeaderCell>
33 <Table.HeaderCell>
34 Description
35 </Table.HeaderCell>
36 </Table.Row>
37 </Table.Header>
38 <Table.Body>
39 <Table.Row>
40 <Table.Cell>
41 [ProductVariant](/references/product/models/ProductVariant) in [Product Module](../../product/page.mdx)
42 </Table.Cell>
43 <Table.Cell>
44 [InventoryItem](/references/inventory-next/models/InventoryItem)
45 </Table.Cell>
46 <Table.Cell>
47 Stored - many-to-many
48 </Table.Cell>
49 <Table.Cell>
50 [Learn more](#product-module)
51 </Table.Cell>
52 </Table.Row>
53 <Table.Row>
54 <Table.Cell>
55 [InventoryLevel](/references/inventory-next/models/InventoryLevel)
56 </Table.Cell>
57 <Table.Cell>
58 [StockLocation](/references/stock-location-next/models/StockLocation) in [Stock Location Module](../../stock-location/page.mdx)
59 </Table.Cell>
60 <Table.Cell>
61 Read-only - has many
62 </Table.Cell>
63 <Table.Cell>
64 [Learn more](#stock-location-module)
65 </Table.Cell>
66 </Table.Row>
67 </Table.Body>
68 </Table>
69
70 ---
71
72 ## Product Module
73
74 Each product variant has different inventory details. Medusa defines a link between the `ProductVariant` and `InventoryItem` data models.
75
76 ![A diagram showcasing an example of how data models from the Inventory and Product Module are linked.](https://res.cloudinary.com/dza7lstvk/image/upload/v1709658720/Medusa%20Resources/inventory-product_ejnray.jpg)
77
78 A product variant whose `manage_inventory` property is enabled has an associated inventory item. Through that inventory's items relations in the Inventory Module, you can manage and check the variant's inventory quantity.
79
80 <Note title="Tip">
81
82 Learn more about product variant's inventory management in [this guide](../../product/variant-inventory/page.mdx).
83
84 </Note>
85
86 ### Retrieve with Query
87
88 To retrieve the product variants of an inventory item with [Query](!docs!/learn/fundamentals/query), pass `variants.*` in `fields`:
89
90 <CodeTabs group="relation-query">
91 <CodeTab label="query.graph" value="method">
92
93 ```ts
94 const { data: inventoryItems } = await query.graph({
95 entity: "inventory_item",
96 fields: [
97 "variants.*",
98 ],
99 })
100
101 // inventoryItems[0].variants
102 ```
103
104 </CodeTab>
105 <CodeTab label="useQueryGraphStep" value="step">
106
107 ```ts
108 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
109
110 // ...
111
112 const { data: inventoryItems } = useQueryGraphStep({
113 entity: "inventory_item",
114 fields: [
115 "variants.*",
116 ],
117 })
118
119 // inventoryItems[0].variants
120 ```
121
122 </CodeTab>
123 </CodeTabs>
124
125 ### Manage with Link
126
127 To manage the variants of an inventory item, use [Link](!docs!/learn/fundamentals/module-links/link):
128
129 <CodeTabs group="relation-link">
130 <CodeTab label="link.create" value="method">
131
132 ```ts
133 import { Modules } from "@medusajs/framework/utils"
134
135 // ...
136
137 await link.create({
138 [Modules.PRODUCT]: {
139 variant_id: "variant_123",
140 },
141 [Modules.INVENTORY]: {
142 inventory_item_id: "iitem_123",
143 },
144 })
145 ```
146
147 </CodeTab>
148 <CodeTab label="createRemoteLinkStep" value="step">
149
150 ```ts
151 import { Modules } from "@medusajs/framework/utils"
152 import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
153
154 // ...
155
156 createRemoteLinkStep({
157 [Modules.PRODUCT]: {
158 variant_id: "variant_123",
159 },
160 [Modules.INVENTORY]: {
161 inventory_item_id: "iitem_123",
162 },
163 })
164 ```
165
166 </CodeTab>
167 </CodeTabs>
168
169 ---
170
171 ## Stock Location Module
172
173 Medusa defines a read-only link between the `InventoryLevel` data model and the [Stock Location Module](../../stock-location/page.mdx)'s `StockLocation` data model. This means you can retrieve the details of an inventory level's stock locations, but you don't manage the links in a pivot table in the database. The stock location of an inventory level is determined by the `location_id` property of the `InventoryLevel` data model.
174
175 ### Retrieve with Query
176
177 To retrieve the stock locations of an inventory level with [Query](!docs!/learn/fundamentals/query), pass `stock_locations.*` in `fields`:
178
179 <CodeTabs group="relation-query">
180 <CodeTab label="query.graph" value="method">
181
182 ```ts
183 const { data: inventoryLevels } = await query.graph({
184 entity: "inventory_level",
185 fields: [
186 "stock_locations.*",
187 ],
188 })
189
190 // inventoryLevels[0].stock_locations
191 ```
192
193 </CodeTab>
194 <CodeTab label="useQueryGraphStep" value="step">
195
196 ```ts
197 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
198
199 // ...
200
201 const { data: inventoryLevels } = useQueryGraphStep({
202 entity: "inventory_level",
203 fields: [
204 "stock_locations.*",
205 ],
206 })
207
208 // inventoryLevels[0].stock_locations
209 ```
210
211 </CodeTab>
212 </CodeTabs>