|
1
|
--- |
|
2
|
sidebar_label: "use-inject-manager-on-public-methods" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `use-inject-manager-on-public-methods - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule requires that service methods accepting a `Context` parameter are decorated with `@InjectManager()` (public) or `@InjectTransactionManager()` (protected or private). |
|
12
|
|
|
13
|
## Severity |
|
14
|
|
|
15
|
`warn`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset. |
|
16
|
|
|
17
|
## What it Targets |
|
18
|
|
|
19
|
This rule targets service files in your project's `src/modules` directory. It reports a method on a service class that accepts a `Context` parameter but isn't decorated with the appropriate manager decorator. |
|
20
|
|
|
21
|
The following code is reported by the rule: |
|
22
|
|
|
23
|
```ts title="src/modules/blog/service.ts" |
|
24
|
import { MedusaService } from "@medusajs/framework/utils" |
|
25
|
|
|
26
|
class BlogModuleService extends MedusaService({}) { |
|
27
|
async list(sharedContext: Context = {}) {} |
|
28
|
} |
|
29
|
``` |
|
30
|
|
|
31
|
Instead, decorate a public method with `@InjectManager()`: |
|
32
|
|
|
33
|
```ts title="src/modules/blog/service.ts" |
|
34
|
import { |
|
35
|
MedusaService, |
|
36
|
InjectManager, |
|
37
|
} from "@medusajs/framework/utils" |
|
38
|
|
|
39
|
class BlogModuleService extends MedusaService({}) { |
|
40
|
@InjectManager() |
|
41
|
async list(sharedContext: Context = {}) {} |
|
42
|
} |
|
43
|
``` |
|
44
|
|
|
45
|
## Why it's Important |
|
46
|
|
|
47
|
The `@InjectManager()` and `@InjectTransactionManager()` decorators inject the entity manager into the method's context. Without them, the method runs without a managed transaction and database operations can fail. |
|
48
|
|
|
49
|
Learn more in the [Database Operations in Services documentation](!docs!/learn/fundamentals/modules/db-operations). |
|
50
|
|
|
51
|
## Fixable |
|
52
|
|
|
53
|
This rule is auto-fixable. Run the linter with the `--fix` option to apply the fix automatically. |
|
54
|
|
|
55
|
## Turn it Off |
|
56
|
|
|
57
|
To turn off this rule, set it to `off` in your ESLint configuration: |
|
58
|
|
|
59
|
```ts title="eslint.config.ts" |
|
60
|
import { defineConfig } from "eslint/config" |
|
61
|
import medusa from "@medusajs/eslint-plugin" |
|
62
|
|
|
63
|
export default defineConfig([ |
|
64
|
...medusa.configs.recommended, |
|
65
|
{ |
|
66
|
rules: { |
|
67
|
"@medusajs/use-inject-manager-on-public-methods": "off", |
|
68
|
}, |
|
69
|
}, |
|
70
|
]) |
|
71
|
``` |
|
72
|
|
|
73
|
Or disable it for a single line using an inline comment: |
|
74
|
|
|
75
|
```ts |
|
76
|
// eslint-disable-next-line @medusajs/use-inject-manager-on-public-methods |
|
77
|
async list(sharedContext: Context = {}) {} |
|
78
|
``` |