read-only-link-requires-field - ESLint plugin rules
This rule requires read-only defineLink calls to specify a field on the first linkable, and a primaryKey or alias on the inverse form.
Severity#
error. This rule is enabled in the recommended preset.
What it Targets#
This rule targets files in your project's src/links directory. It reports read-only defineLink calls (calls whose options object sets readOnly: true) whose first argument is missing a field property. For the inverse form, where the second argument spreads <Module>.linkable.<dataModel>.id, it also requires either a primaryKey property or an alias property.
The following code is reported by the rule:
Instead, specify the field that stores the linked record's ID on the first argument:
1import { defineLink } from "@medusajs/framework/utils"2import BlogModule from "../modules/blog"3import ProductModule from "@medusajs/medusa/product"4 5export default defineLink(6 {7 linkable: BlogModule.linkable.post,8 field: "product_id",9 },10 ProductModule.linkable.product,11 { readOnly: true }12)
For the inverse form, include a primaryKey on the second argument:
src/links/post-product.ts1export default defineLink(2 { linkable: ProductModule.linkable.product, field: "id" },3 {4 ...BlogModule.linkable.post.id,5 primaryKey: "product_id",6 },7 { readOnly: true }8)
Alternatively, you can use alias instead of primaryKey on the inverse form when re-aliasing a linkable. For example, when linking to draft orders using the order linkable re-aliased to draft_order:
1import { defineLink } from "@medusajs/framework/utils"2import QuoteModule from "../modules/quote"3import OrderModule from "@medusajs/medusa/order"4 5export default defineLink(6 { linkable: QuoteModule.linkable.quote, field: "draft_order_id" },7 { ...OrderModule.linkable.order.id, alias: "draft_order" },8 { readOnly: true }9)
Why it's Important#
A read-only link doesn't store its own data, so Medusa needs the field to know where the linked record's ID lives. Without it, the link can't resolve the related records.
Learn more in the Read-Only Module Links documentation.
Fixable#
This rule isn't auto-fixable.