docs: improvements to admin, query, and read-only link docs (#12239)
* improve admin page * improvemens to query and read-only link docs * fix build error
This commit is contained in:
@@ -175,7 +175,7 @@ In the object passed to the `graph` method:
|
||||
- `product.*` to retrieve the fields of a product record linked to a `Post` record.
|
||||
- `post.*` to retrieve the fields of a `Post` record linked to a product record.
|
||||
|
||||
You can then apply any [filters](#apply-filters) or [pagination configurations](#apply-pagination).
|
||||
You can then apply any [filters](#apply-filters) or [pagination configurations](#apply-pagination) on the module link's table. For example, you can apply filters on the `product_id`, `post_id`, and any other custom columns you defined in the link table.
|
||||
|
||||
The returned `data` is similar to the following:
|
||||
|
||||
@@ -238,7 +238,12 @@ Filters don't apply on fields of linked data models from other modules. Refer to
|
||||
|
||||
### Advanced Query Filters
|
||||
|
||||
Under the hood, Query uses the `listX` (`listPosts`) method of the data model's module's service to retrieve records. This method accepts a filter object that can be used to filter records.
|
||||
Under the hood, Query uses one of the following methods from the data model's module's service to retrieve records:
|
||||
|
||||
- `listX` if you don't pass [pagination parameters](#apply-pagination). For example, `listPosts`.
|
||||
- `listAndCountX` if you pass pagination parameters. For example, `listAndCountPosts`.
|
||||
|
||||
Both methods accepts a filter object that can be used to filter records.
|
||||
|
||||
Those filters don't just allow you to filter by exact values. You can also filter by properties that don't match a value, match multiple values, and other filter types.
|
||||
|
||||
|
||||
@@ -381,6 +381,7 @@ Read-only module links are most useful when working with data models that aren't
|
||||
To define the read-only module link to a virtual data model, you must:
|
||||
|
||||
1. Create a `list` method in the custom module's service. This method retrieves the linked records filtered by the ID(s) of the first data model.
|
||||
- You can also create a `listAndCount` method to retrieve the related records with pagination.
|
||||
2. Define the read-only module link from the first data model to the virtual data model.
|
||||
3. Use Query to retrieve the first data model and its linked records from the virtual data model.
|
||||
|
||||
@@ -395,6 +396,8 @@ Refer to the [Modules chapter](../../modules/page.mdx) to learn how to create a
|
||||
</Note>
|
||||
|
||||
```ts title="src/modules/cms/service.ts"
|
||||
import { FindConfig } from "@medusajs/framework/types"
|
||||
|
||||
type CmsModuleOptions = {
|
||||
apiKey: string
|
||||
}
|
||||
@@ -427,6 +430,36 @@ export default class CmsModuleService {
|
||||
* ]
|
||||
*/
|
||||
}
|
||||
|
||||
// To retrieve with pagination
|
||||
async listAndCount(
|
||||
filter: {
|
||||
id: string | string[]
|
||||
},
|
||||
config?: FindConfig<any> | undefined,
|
||||
) {
|
||||
return this.client.getPosts(filter, {
|
||||
limit: config?.take,
|
||||
offset: config?.skip,
|
||||
})
|
||||
/**
|
||||
* Example of returned data:
|
||||
*
|
||||
* {
|
||||
* count: 2,
|
||||
* data: [
|
||||
* {
|
||||
* "id": "post_123",
|
||||
* "product_id": "prod_321"
|
||||
* },
|
||||
* {
|
||||
* "id": "post_456",
|
||||
* "product_id": "prod_654"
|
||||
* }
|
||||
* ]
|
||||
* }
|
||||
*/
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -434,6 +467,8 @@ The above service initializes a client, assuming your CMS has an SDK that allows
|
||||
|
||||
The service must have a `list` method to be part of the read-only module link. This method accepts the ID(s) of the products to retrieve their associated posts. The posts must include the product's ID in a field, such as `product_id`.
|
||||
|
||||
You can also create a `listAndCount` method to retrieve the posts with pagination. This method is called if you pass [pagination parameters to Query](../query/page.mdx#apply-pagination).
|
||||
|
||||
Next, define a read-only module link from the Product Module to the CMS Module:
|
||||
|
||||
```ts title="src/links/product-cms.ts"
|
||||
|
||||
Reference in New Issue
Block a user