|
1
|
--- |
|
2
|
sidebar_label: "link-no-cross-module-relationship" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `link-no-cross-module-relationship - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule requires that data model relationships (`hasOne`, `belongsTo`, `hasMany`, `manyToMany`) reference a data model defined in the same module. |
|
12
|
|
|
13
|
## Severity |
|
14
|
|
|
15
|
`error`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset. |
|
16
|
|
|
17
|
## What it Targets |
|
18
|
|
|
19
|
This rule targets data model files in your project's `src/modules` directory. It reports a relationship method that references a data model imported from another module. |
|
20
|
|
|
21
|
The following code is reported by the rule: |
|
22
|
|
|
23
|
```ts title="src/modules/blog/models/post.ts" |
|
24
|
import { model } from "@medusajs/framework/utils" |
|
25
|
import Product from "@medusajs/medusa/product/models/product" |
|
26
|
|
|
27
|
export const Post = model.define("post", { |
|
28
|
product: model.belongsTo(() => Product, {}), |
|
29
|
}) |
|
30
|
``` |
|
31
|
|
|
32
|
Instead, reference a same-module data model and use a [module link](!docs!/learn/fundamentals/module-links) for cross-module references: |
|
33
|
|
|
34
|
```ts title="src/modules/blog/models/post.ts" |
|
35
|
import { model } from "@medusajs/framework/utils" |
|
36
|
import Author from "./author" |
|
37
|
|
|
38
|
export const Post = model.define("post", { |
|
39
|
author: model.belongsTo(() => Author, {}), |
|
40
|
}) |
|
41
|
``` |
|
42
|
|
|
43
|
## Why it's Important |
|
44
|
|
|
45
|
Modules are isolated and can't have direct database relationships across module boundaries. Cross-module references are defined with module links instead, which keeps modules decoupled. |
|
46
|
|
|
47
|
Learn more in the [Module Links documentation](!docs!/learn/fundamentals/module-links). |
|
48
|
|
|
49
|
## Fixable |
|
50
|
|
|
51
|
This rule isn't auto-fixable. |