You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: www/apps/book/app/learn/fundamentals/modules/container/page.mdx
+77-9Lines changed: 77 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,9 @@ export const metadata = {
4
4
5
5
# {metadata.title}
6
6
7
-
In this chapter, you'll learn about the module's container and how to resolve resources in that container.
7
+
In this chapter, you'll learn about the module container and how to resolve resources from it.
8
8
9
-
Since modules are [isolated](../isolation/page.mdx), each module has a local container only used by the resources of that module.
9
+
Since modules are [isolated](../isolation/page.mdx), each module has a local container used only by the resources of that module.
10
10
11
11
So, resources in the module, such as services or loaders, can only resolve other resources registered in the module's container, and some Framework tools that the Medusa application registers in the module's container.
12
12
@@ -16,13 +16,13 @@ Find a list of resources or dependencies registered in a module's container in [
16
16
17
17
---
18
18
19
-
## Resolve Resources
19
+
## Resolve Resources from the Module Container
20
20
21
-
### Services
21
+
### Resolve in Services
22
22
23
-
A service's constructor accepts as a first parameter an object used to resolve resources registered in the module's container.
23
+
A service's constructor accepts as a first parameter an object used to resolve resources registered in the module's container. To resolve a resource, add the resource's registration name as a property of the object.
24
24
25
-
For example:
25
+
For example, to resolve the [Logger](../../../debugging-and-testing/logging/page.mdx) from the container:
26
26
27
27
```ts highlights={[["4"], ["10"]]}
28
28
import { Logger } from"@medusajs/framework/types"
@@ -44,11 +44,13 @@ export default class BlogModuleService {
44
44
}
45
45
```
46
46
47
-
### Loader
47
+
You can then use the logger in the service's methods.
48
48
49
-
A loader function accepts as a parameter an object having the property `container`. Its value is the module's container used to resolve resources.
49
+
### Resolve in Loaders
50
50
51
-
For example:
51
+
[Loaders](../loaders/page.mdx) accept an object parameter with the property `container`. Its value is the module's container that can be used to resolve resources using its `resolve` method.
52
+
53
+
For example, to resolve the [Logger](../../../debugging-and-testing/logging/page.mdx) in a loader:
52
54
53
55
```ts highlights={[["9"]]}
54
56
import {
@@ -66,3 +68,69 @@ export default async function helloWorldLoader({
66
68
logger.info("[helloWorldLoader]: Hello, World!")
67
69
}
68
70
```
71
+
72
+
You can then use the logger in the loader's code.
73
+
74
+
---
75
+
76
+
## Caveat: Resolving Module Services in Loaders
77
+
78
+
Consider a module that has a main service `BrandModuleService`, and an internal service `CmsService`. Medusa will register both of these services in the module's container.
79
+
80
+
However, loaders are executed before any services are initialized and registered in the module's container. So, you can't resolve the `BrandModuleService` and `CmsService` in a loader.
81
+
82
+
Instead, if your main service extends the `MedusaService`[service factory](../service-factory/page.mdx), you can resolve the internal services generated for each data model passed to the `MedusaService` function.
83
+
84
+
For example, if the `BrandModuleService` is defined as follows:
Refer to the [Service Factory reference](!resources!/service-factory-reference) for details on the available methods in the generated services.
117
+
118
+
---
119
+
120
+
## Alternative to Resolving Other Modules' Services
121
+
122
+
Since modules are [isolated](../isolation/page.mdx), you can't resolve resources that belong to other modules from the module's container. For example, you can't resolve the Product Module's service in the Blog Module's service.
123
+
124
+
Instead, to build commerce features that span multiple modules, you can create [workflows](../../workflows/page.mdx). In those workflows, you can resolve services of all modules registered in the Medusa application, including the services of the Product and Blog modules.
125
+
126
+
Then, you can execute the workflows in [API routes](../../api-routes/page.mdx), [subscribers](../../events-and-subscribers/page.mdx), or [scheduled jobs](../../scheduled-jobs/page.mdx).
127
+
128
+
Learn more and find examples in the [Module Isolation](../isolation/page.mdx) chapter.
129
+
130
+
---
131
+
132
+
## Avoid Circular Dependencies
133
+
134
+
When resolving resources in a module's services, make sure you don't create circular dependencies. For example, if `BlogModuleService` resolves `CmsService`, and `CmsService` resolves `BlogModuleService`, it will cause a circular dependency error.
135
+
136
+
Instead, you should generally only resolve services within the main service. For example, `BlogModuleService` can resolve `CmsService`, but `CmsService` should not resolve `BlogModuleService`.
@@ -279,7 +279,7 @@ Find a complete reference of each of the methods in [this documentation](!resour
279
279
280
280
### Using a Constructor
281
281
282
-
If you implement the`constructor`of your service, make sure to call `super`passing it `...arguments`.
282
+
If you implement a`constructor`in your service, make sure to call `super`and pass it `...arguments`.
283
283
284
284
For example:
285
285
@@ -297,3 +297,45 @@ class BlogModuleService extends MedusaService({
297
297
298
298
exportdefaultBlogModuleService
299
299
```
300
+
301
+
---
302
+
303
+
## Generated Internal Services
304
+
305
+
The service factory also generates internal services for each data model passed to the `MedusaService` function. These services are registered in the module's container and can be resolved using their camel-cased names.
306
+
307
+
For example, if the `BlogModuleService` is defined as follows:
Then, you'll have a `postService` registered in the module's container that allows you to manage posts.
322
+
323
+
Generated internal services have the same methods as the `BlogModuleService`, such as `create`, `retrieve`, `update`, and `delete`, but without the data model name suffix.
324
+
325
+
These services are useful when you need to perform database operations in loaders, as they are executed before the module's services are registered. Learn more in the [Module Container](../container/page.mdx) documentation.
326
+
327
+
For example, you can create a loader that logs the number of posts in the database:
0 commit comments