docs: small fixes and improvements to text (#14385)
This commit is contained in:
@@ -13,18 +13,12 @@ export const metadata = {
|
||||
|
||||
In this chapter, you'll learn how to define a module link between a brand defined in the [custom Brand Module](../../custom-features/module/page.mdx), and a product defined in the [Product Module](!resources!/commerce-modules/product) that's available in your Medusa application out-of-the-box.
|
||||
|
||||
Modules are [isolated](../../../fundamentals/modules/isolation/page.mdx) 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.
|
||||
Modules are [isolated](../../../fundamentals/modules/isolation/page.mdx) from other resources, ensuring that they're integrated into the Medusa application without side effects. However, you may need to build relations between data models of different modules, or you're trying to extend data models from [Commerce Modules](!resources!/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.
|
||||
A [module link](../../../fundamentals/module-links/page.mdx) 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.
|
||||
|
||||
<Note>
|
||||
|
||||
Learn more about module links in [this chapters](../../../fundamentals/module-links/page.mdx).
|
||||
|
||||
</Note>
|
||||
|
||||
<Prerequisites
|
||||
items={[
|
||||
{
|
||||
|
||||
+4
-16
@@ -22,7 +22,7 @@ So, in this chapter, to extend the create product flow and associate a brand wit
|
||||
|
||||
<Note>
|
||||
|
||||
To learn more about the `additional_data` property and the API routes that accept additional data, refer to [this chapter](../../../fundamentals/api-routes/additional-data/page.mdx).
|
||||
To learn more about the `additional_data` property and the API routes that accept additional data, refer to the [Additional Data](../../../fundamentals/api-routes/additional-data/page.mdx) chapter.
|
||||
|
||||
</Note>
|
||||
|
||||
@@ -43,13 +43,7 @@ To learn more about the `additional_data` property and the API routes that accep
|
||||
|
||||
## 1. Consume the productsCreated Hook
|
||||
|
||||
A workflow hook is a point in a workflow where you can inject a step to perform a custom functionality. Consuming a workflow hook allows you to extend the features of a workflow and, consequently, the API route that uses it.
|
||||
|
||||
<Note>
|
||||
|
||||
Learn more about the workflow hooks in [this chapter](../../../fundamentals/workflows/workflow-hooks/page.mdx).
|
||||
|
||||
</Note>
|
||||
A [workflow hook](../../../fundamentals/workflows/workflow-hooks/page.mdx) is a point in a workflow where you can inject a step to perform a custom functionality. Consuming a workflow hook allows you to extend the features of a workflow and, consequently, the API route that uses it.
|
||||
|
||||
The [createProductsWorkflow](!resources!/references/medusa-workflows/createProductsWorkflow) used in the [Create Product API route](!api!/admin#products_postproducts) has a `productsCreated` hook that runs after the product is created. You'll consume this hook to link the created product with the brand specified in the request parameters.
|
||||
|
||||
@@ -98,13 +92,7 @@ In the step, if a brand ID is passed in `additional_data`, you resolve the Brand
|
||||
|
||||
### Link Brand to Product
|
||||
|
||||
Next, you want to create a link between the created products and the brand. To do so, you use Link, which is a class from the Modules SDK that provides methods to manage linked records.
|
||||
|
||||
<Note>
|
||||
|
||||
Learn more about Link in [this chapter](../../../fundamentals/module-links/link/page.mdx).
|
||||
|
||||
</Note>
|
||||
Next, you want to create a link between the created products and the brand. To do so, you use [Link](../../../fundamentals/module-links/link/page.mdx), which is a class from the Modules SDK that provides methods to manage linked records.
|
||||
|
||||
To use Link in the `productsCreated` hook, replace the `TODO` with the following:
|
||||
|
||||
@@ -139,7 +127,7 @@ logger.info("Linked brand to products")
|
||||
return new StepResponse(links, links)
|
||||
```
|
||||
|
||||
You resolve Link from the container. Then you loop over the created products to assemble an array of links to be created. After that, you pass the array of links to Link's `create` method, which will link the product and brand records.
|
||||
You resolve Link from the container. Then, you loop over the created products to assemble an array of links to be created. After that, you pass the array of links to Link's `create` method, which will link the product and brand records.
|
||||
|
||||
Each property in the link object is the name of a module, and its value is an object having a `{model_name}_id` property, where `{model_name}` is the snake-case name of the module's data model. Its value is the ID of the record to be linked. The link object's properties must be set in the same order as the link configurations passed to `defineLink`.
|
||||
|
||||
|
||||
+4
-10
@@ -44,7 +44,7 @@ For example, send the following request to retrieve the list of products with th
|
||||
|
||||
```bash
|
||||
curl 'http://localhost:9000/admin/products?fields=+brand.*' \
|
||||
--header 'Authorization: Bearer {token}'
|
||||
-H 'Authorization: Bearer {token}'
|
||||
```
|
||||
|
||||
<Note title="Tip">
|
||||
@@ -75,19 +75,13 @@ By using the `fields` query parameter, you don't have to re-create existing API
|
||||
|
||||
While you can retrieve linked records using the `fields` query parameter of an existing API route, you can't filter by linked records.
|
||||
|
||||
Instead, you'll have to create a custom API route that uses Query to retrieve linked records with filters, as explained in the [Query documentation](../../../fundamentals/module-links/query/page.mdx#apply-filters-and-pagination-on-linked-records).
|
||||
Instead, you'll have to create a custom API route that uses [Query](../../../fundamentals/module-links/query/page.mdx#apply-filters-and-pagination-on-linked-records) to retrieve linked records with filters.
|
||||
|
||||
---
|
||||
|
||||
## Approach 2: Use Query to Retrieve Linked Records
|
||||
|
||||
You can also retrieve linked records using Query. Query allows you to retrieve data across modules with filters, pagination, and more. You can resolve Query from the Medusa container and use it in your API route or workflow.
|
||||
|
||||
<Note>
|
||||
|
||||
Learn more about Query in [this chapter](../../../fundamentals/module-links/query/page.mdx).
|
||||
|
||||
</Note>
|
||||
You can also retrieve linked records using [Query](../../../fundamentals/module-links/query/page.mdx). Query allows you to retrieve data across modules with filters, pagination, and more. You can resolve Query from the Medusa container and use it in your API route or workflow.
|
||||
|
||||
For example, you can create an API route that retrieves brands and their products. If you followed the [Create Brands API route chapter](../../custom-features/api-route/page.mdx), you'll have the file `src/api/admin/brands/route.ts` with a `POST` API route. Add a new `GET` function to the same file:
|
||||
|
||||
@@ -183,6 +177,6 @@ By following the examples of the previous chapters, you:
|
||||
|
||||
## Next Steps: Customize Medusa Admin
|
||||
|
||||
Clients, such as the Medusa Admin dashboard, can now use brand-related features, such as creating a brand or setting the brand of a product.
|
||||
Client applications, such as the Medusa Admin dashboard, can now use brand-related features, such as creating a brand or setting the brand of a product.
|
||||
|
||||
In the next chapters, you'll learn how to customize the Medusa Admin to show a product's brand on its details page, and to show a new page with the list of brands in your store.
|
||||
|
||||
Reference in New Issue
Block a user