2.2.1. Guide: Define Module Link Between Brand and Product

In this chapter, you'll learn how to define a module link between a brand defined in the custom Brand Module, and a product defined in the Product Module that's available in your Medusa application out-of-the-box.

Modules are isolated from other resources, ensuring that they're integrated into the Medusa application without side effects. However, you may need to associate data models of different modules, or you're trying to extend data models from commerce modules with custom properties. To do that, you define module links.

A module link 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.

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.

NoteLearn more about module links in this chapters.

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.

So, to define a link between the Product and Brand models, create the file src/links/product-brand.ts with the following content:

The directory structure of the Medusa application after adding the link.

src/links/product-brand.ts
1import BrandModule from "../modules/brand"2import ProductModule from "@medusajs/medusa/product"3import { defineLink } from "@medusajs/framework/utils"4
5export default defineLink(6  {7    linkable: ProductModule.linkable.product,8    isList: true,9  },10  BrandModule.linkable.brand11)

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.

The defineLink function accepts two parameters of the same type, which is either:

  • The data model's link configuration, which you access from the Module's linkable property;
  • Or an object that has two properties:
    • linkable: the data model's link configuration, which you access from the Module's linkable property.
    • isList: A boolean indicating whether many records of the data model can be linked to the other model.

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.


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:

Terminal
npx medusa db:migrate

This command reflects migrations on the database and syncs module links, which creates a table for the product-brand link.

TipYou can also run the npx medusa db:sync-links to just sync module links without running migrations.

Next Steps: Extend Create Product Flow#

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.

Was this chapter helpful?
Edit this page