diff --git a/www/apps/book/app/learn/fundamentals/module-links/read-only/page.mdx b/www/apps/book/app/learn/fundamentals/module-links/read-only/page.mdx index f817f12113..8710604ebc 100644 --- a/www/apps/book/app/learn/fundamentals/module-links/read-only/page.mdx +++ b/www/apps/book/app/learn/fundamentals/module-links/read-only/page.mdx @@ -372,6 +372,66 @@ In this case, the relation would always be one-to-many, even if only one post is ] ``` +### Relation Name for Inverse Read-Only Links + +Whether you're using a one-to-one or one-to-many relation for an inverse read-only module link, the relation name is always the alias of the second data model. It's not changed to its plural form for one-to-many relations. + +For example, looking again at the forced one-to-many relation example: + +```ts +import BlogModule from "../modules/blog" +import ProductModule from "@medusajs/medusa/product" +import { defineLink } from "@medusajs/framework/utils" + +export default defineLink( + { + linkable: ProductModule.linkable.product, + field: "id", + isList: true, + }, + { + ...BlogModule.linkable.post.id, + primaryKey: "product_id", + }, + { + readOnly: true, + } +) +``` + +When querying a product, the relation name is still `post`, not `posts`: + +export const relationNameHighlights = [ + ["3", `"post.*"`, "The relation name is the alias of the second data model."], +] + +```ts highlights={relationNameHighlights} +const { result } = await query.graph({ + entity: "product", + fields: ["id", "post.*"], + filters: { + id: "prod_123", + }, +}) +``` + +Consequently, the result will include a `post` property, even though it's an array of posts: + +```json title="Example Data" +[ + { + "id": "prod_123", + "post": [ + { + "id": "post_123", + "product_id": "prod_123" + // ... + } + ] + } +] +``` + --- ## Example: Read-Only Module Link for Virtual Data Models diff --git a/www/apps/book/generated/edit-dates.mjs b/www/apps/book/generated/edit-dates.mjs index 32d6344afc..0e573c01df 100644 --- a/www/apps/book/generated/edit-dates.mjs +++ b/www/apps/book/generated/edit-dates.mjs @@ -114,7 +114,7 @@ export const generatedEditDates = { "app/learn/configurations/medusa-config/page.mdx": "2025-09-30T06:04:15.705Z", "app/learn/configurations/ts-aliases/page.mdx": "2025-07-23T15:32:18.008Z", "app/learn/production/worker-mode/page.mdx": "2025-07-18T15:19:45.352Z", - "app/learn/fundamentals/module-links/read-only/page.mdx": "2025-08-15T11:52:13.403Z", + "app/learn/fundamentals/module-links/read-only/page.mdx": "2025-10-15T15:42:22.610Z", "app/learn/fundamentals/data-models/properties/page.mdx": "2025-10-15T05:36:40.576Z", "app/learn/fundamentals/framework/page.mdx": "2025-06-26T14:26:22.120Z", "app/learn/fundamentals/api-routes/retrieve-custom-links/page.mdx": "2025-07-14T10:24:32.582Z", diff --git a/www/apps/book/public/llms-full.txt b/www/apps/book/public/llms-full.txt index f24d527564..e4357f2764 100644 --- a/www/apps/book/public/llms-full.txt +++ b/www/apps/book/public/llms-full.txt @@ -15402,6 +15402,62 @@ In this case, the relation would always be one-to-many, even if only one post is ] ``` +### Relation Name for Inverse Read-Only Links + +Whether you're using a one-to-one or one-to-many relation for an inverse read-only module link, the relation name is always the alias of the second data model. It's not changed to its plural form for one-to-many relations. + +For example, looking again at the the forced one-to-many relation example: + +```ts +import BlogModule from "../modules/blog" +import ProductModule from "@medusajs/medusa/product" +import { defineLink } from "@medusajs/framework/utils" + +export default defineLink( + { + linkable: ProductModule.linkable.product, + field: "id", + isList: true, + }, + { + ...BlogModule.linkable.post.id, + primaryKey: "product_id", + }, + { + readOnly: true, + } +) +``` + +When querying a product, the relation name is still `post`, not `posts`: + +```ts highlights={relationNameHighlights} +const { result } = await query.graph({ + entity: "product", + fields: ["id", "post.*"], + filters: { + id: "prod_123", + }, +}) +``` + +Consequently, the result will include a `post` property, even though it's an array of posts: + +```json title="Example Data" +[ + { + "id": "prod_123", + "post": [ + { + "id": "post_123", + "product_id": "prod_123" + // ... + } + ] + } +] +``` + *** ## Example: Read-Only Module Link for Virtual Data Models