www/apps/resources/app/commerce-modules/inventory/links-to-other-modules/page.mdx
import { CodeTabs, CodeTab, Table } from "docs-ui"
export const metadata = {
title: Links between Inventory Module and Other Modules,
}
This document showcases the module links defined between the Inventory Module and other Commerce Modules.
The Inventory Module has the following links to other modules:
<Note title="Tip">Read-only links are used to query data across modules, but the relations aren't stored in a pivot table in the database.
</Note> <Table> <Table.Header> <Table.Row> <Table.HeaderCell> First Data Model </Table.HeaderCell> <Table.HeaderCell> Second Data Model </Table.HeaderCell> <Table.HeaderCell> Type </Table.HeaderCell> <Table.HeaderCell> Description </Table.HeaderCell> </Table.Row> </Table.Header> <Table.Body> <Table.Row> <Table.Cell> [ProductVariant](/references/product/models/ProductVariant) in [Product Module](../../product/page.mdx) </Table.Cell> <Table.Cell> [InventoryItem](/references/inventory-next/models/InventoryItem) </Table.Cell> <Table.Cell> Stored - many-to-many </Table.Cell> <Table.Cell> [Learn more](#product-module) </Table.Cell> </Table.Row> <Table.Row> <Table.Cell> [InventoryLevel](/references/inventory-next/models/InventoryLevel) </Table.Cell> <Table.Cell> [StockLocation](/references/stock-location-next/models/StockLocation) in [Stock Location Module](../../stock-location/page.mdx) </Table.Cell> <Table.Cell> Read-only - has many </Table.Cell> <Table.Cell> [Learn more](#stock-location-module) </Table.Cell> </Table.Row> </Table.Body> </Table>Each product variant has different inventory details. Medusa defines a link between the ProductVariant and InventoryItem data models.
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.
Learn more about product variant's inventory management in this guide.
</Note>To retrieve the product variants of an inventory item with Query, pass variants.* in fields:
const { data: inventoryItems } = await query.graph({
entity: "inventory_item",
fields: [
"variants.*",
],
})
// inventoryItems[0].variants
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: inventoryItems } = useQueryGraphStep({
entity: "inventory_item",
fields: [
"variants.*",
],
})
// inventoryItems[0].variants
To manage the variants of an inventory item, use Link:
<CodeTabs group="relation-link"> <CodeTab label="link.create" value="method">import { Modules } from "@medusajs/framework/utils"
// ...
await link.create({
[Modules.PRODUCT]: {
variant_id: "variant_123",
},
[Modules.INVENTORY]: {
inventory_item_id: "iitem_123",
},
})
import { Modules } from "@medusajs/framework/utils"
import { createRemoteLinkStep } from "@medusajs/medusa/core-flows"
// ...
createRemoteLinkStep({
[Modules.PRODUCT]: {
variant_id: "variant_123",
},
[Modules.INVENTORY]: {
inventory_item_id: "iitem_123",
},
})
Medusa defines a read-only link between the InventoryLevel data model and the Stock Location Module'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.
To retrieve the stock locations of an inventory level with Query, pass stock_locations.* in fields:
const { data: inventoryLevels } = await query.graph({
entity: "inventory_level",
fields: [
"stock_locations.*",
],
})
// inventoryLevels[0].stock_locations
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
// ...
const { data: inventoryLevels } = useQueryGraphStep({
entity: "inventory_level",
fields: [
"stock_locations.*",
],
})
// inventoryLevels[0].stock_locations