diff --git a/www/apps/book/app/advanced-development/modules/module-link-directions/page.mdx b/www/apps/book/app/advanced-development/modules/module-link-directions/page.mdx new file mode 100644 index 0000000000..722abeda0e --- /dev/null +++ b/www/apps/book/app/advanced-development/modules/module-link-directions/page.mdx @@ -0,0 +1,61 @@ +export const metadata = { + title: `${pageNumber} Module Link Direction`, +} + +# {metadata.title} + +In this chapter, you'll learn about difference in module link directions, and which to use based on your use case. + +## Link Direction + +The module link's direction depends on the order you pass the data model configuration parameters to `defineLink`. + +For example, the following defines a link from the `helloModuleService`'s `myCustom` data model to the Product Module's `product` data model: + +```ts +export default defineLink( + HelloModule.linkable.myCustom, + ProductModule.linkable.product +) +``` + +Whereas the following defines a link from the Product Module's `product` data model to the `helloModuleService`'s `myCustom` data model: + +```ts +export default defineLink( + ProductModule.linkable.product, + HelloModule.linkable.myCustom +) +``` + +The above links are two different links that serve different purposes. + +--- + +## Which Link Direction to Use? + +### Extend Data Models + +If you're adding a link to a data model to extend it and add new fields, define the link from the main data model to the custom data model. + +For example, if the `myCustom` data model adds new fields to the `product` data model, define the link from `product` to `myCustom`: + +```ts +export default defineLink( + ProductModule.linkable.product, + HelloModule.linkable.myCustom +) +``` + +### Associate Data Models + +If you're linking data models to indicate an association between them, define the link from the custom data model to the main data model. + +For example, if the `myCustom` data model is associated to the `product` data model, define the link from `myCustom` to `product`: + +```ts +export default defineLink( + HelloModule.linkable.myCustom, + ProductModule.linkable.product +) +``` diff --git a/www/apps/book/app/advanced-development/modules/module-links/page.mdx b/www/apps/book/app/advanced-development/modules/module-links/page.mdx index 7f5ebb505f..e4601f0a77 100644 --- a/www/apps/book/app/advanced-development/modules/module-links/page.mdx +++ b/www/apps/book/app/advanced-development/modules/module-links/page.mdx @@ -51,8 +51,8 @@ import ProductModule from "@medusajs/product" import { defineLink } from "@medusajs/utils" export default defineLink( - HelloModule.linkable.myCustom, - ProductModule.linkable.product + ProductModule.linkable.product, + HelloModule.linkable.myCustom ) ``` @@ -86,11 +86,11 @@ import ProductModule from "@medusajs/product" import { defineLink } from "@medusajs/utils" export default defineLink( + ProductModule.linkable.product, { linkable: HelloModule.linkable.myCustom, isList: true, - }, - ProductModule.linkable.product + } ) ``` @@ -132,11 +132,11 @@ import ProductModule from "@medusajs/product" import { defineLink } from "@medusajs/utils" export default defineLink( + ProductModule.linkable.product, { linkable: HelloModule.linkable.myCustom, deleteCascades: true, - }, - ProductModule.linkable.product + } ) ``` diff --git a/www/apps/book/app/advanced-development/modules/remote-link/page.mdx b/www/apps/book/app/advanced-development/modules/remote-link/page.mdx index e86df78502..fc9ab69c01 100644 --- a/www/apps/book/app/advanced-development/modules/remote-link/page.mdx +++ b/www/apps/book/app/advanced-development/modules/remote-link/page.mdx @@ -61,7 +61,7 @@ await remoteLink.create({ [Modules.PRODUCT]: { product_id: "prod_123", }, - "hello": { + "helloModuleService": { my_custom_id: "mc_123", }, }) @@ -69,6 +69,12 @@ await remoteLink.create({ The `create` method accepts as a parameter an object. The object’s keys are the names of the linked modules. + + +The keys (names of linked modules) must be in the same direction of the link definition. + + + The value of each module’s property is an object, whose keys are of the format `{data_model_snake_name}_id`, and values are the IDs of the linked record. So, in the example above, you link a record of the `MyCustom` data model in a `hello` module to a `Product` record in the Product Module. @@ -90,7 +96,7 @@ await remoteLink.dismiss({ [Modules.PRODUCT]: { product_id: "prod_123", }, - "hello": { + "helloModuleService": { my_custom_id: "mc_123", }, }) @@ -98,6 +104,12 @@ await remoteLink.dismiss({ The `dismiss` method accepts the same parameter type as the [create method](#create-link). + + +The keys (names of linked modules) must be in the same direction of the link definition. + + + --- ## Cascade Delete Linked Records diff --git a/www/apps/book/sidebar.mjs b/www/apps/book/sidebar.mjs index 2da41c4aa1..3228846971 100644 --- a/www/apps/book/sidebar.mjs +++ b/www/apps/book/sidebar.mjs @@ -114,6 +114,10 @@ export const sidebar = sidebarAttachHrefCommonOptions( path: "/advanced-development/modules/module-links", title: "Module Links", }, + { + path: "/advanced-development/modules/module-link-directions", + title: "Module Link Direction", + }, { path: "/advanced-development/modules/remote-link", title: "Remote Link",