- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
4.2.2. Create Links between Brand and Product Records
What is the Remote Link?#
The remote link is a class with utility methods to manage links between data models' records.
It’s registered in the Medusa container under the ContainerRegistrationKeys.REMOTE_LINK
(remoteLink
) registration name.
Example: Create Link with Remote Link#
For example, consider the following step:
9import { BRAND_MODULE } from "../../modules/brand"10 11type LinkProductToBrandStepInput = {12 productId: string13 brandId: string14}15 16export const linkProductToBrandStep = createStep(17 "link-product-to-brand",18 async ({ productId, brandId }: LinkProductToBrandStepInput, { container }) => {19 const remoteLink = container.resolve(20 ContainerRegistrationKeys.REMOTE_LINK21 )22 23 remoteLink.create({24 [Modules.PRODUCT]: {25 product_id: productId,26 },27 [BRAND_MODULE]: {28 brand_id: brandId,29 },30 })31 32 return new StepResponse(undefined, {33 productId,34 brandId,35 })36 }37)
In this step, you resolve the remote link, then use its create
method to create a link between product and brand records.
The create
method accepts as a parameter an object whose properties are the names of each module, and the value is an object.
The value object has a property, which is the name of the data model (as specified in model.define
's first parameter) followed by _id
, and its value is the ID of the record to link.
Dismiss Link in Compensation#
The above step can have the following compensation function that dismisses the link between the records:
1export const linkProductToBrandStep = createStep(2 // ...3 async ({ productId, brandId }, { container }) => {4 const remoteLink = container.resolve(5 ContainerRegistrationKeys.REMOTE_LINK6 )7 8 remoteLink.dismiss({9 [Modules.PRODUCT]: {10 product_id: productId,11 },12 [BRAND_MODULE]: {13 brand_id: brandId,14 },15 })16 }17)
The dismiss
method removes the link to dismiss between two records. Its parameter is the same as that of the create
method.
Next Step: Extend Create Product API Route#
In the next step, you'll extend the Create Product API route to allow passing a brand ID, and link a product to a brand.