Files
medusa-store/www/apps/book/app/advanced-development/modules/container/page.mdx
Shahed Nasser 964927b597 docs: general fixes and improvements (#7918)
* docs improvements and changes

* updated module definition

* modules + dml changes

* fix build

* fix vale error

* fix lint errors

* fixes to stripe docs

* fix condition

* fix condition

* fix module defintion

* fix checkout

* disable UI action

* change oas preview action

* flatten provider module options

* fix lint errors

* add module link docs

* pr comments fixes

* fix vale error

* change node engine version

* links -> linkable

* add note about database name

* small fixes

* link fixes

* fix response code in api reference

* added migrations step
2024-07-04 17:26:03 +03:00

67 lines
1.5 KiB
Plaintext

export const metadata = {
title: `${pageNumber} Module's Container`,
}
# {metadata.title}
In this chapter, you'll learn about the module's container and how to resolve resources in that container.
## Module's Container
Each module has a local container only used by the resources of that module.
So, resources in the module, such as services or loaders, can only resolve other resources registered in the module's container, such as:
- `logger`: A utility to log message in the Medusa application's logs.
---
## Resolve Resources
### Services
A service's constructor accepts as a first parameter an object used to resolve resources registered in the module's container.
For example:
```ts highlights={[["4"], ["10"]]}
import { Logger } from "@medusajs/medusa"
type InjectedDependencies = {
logger: Logger
}
export default class HelloModuleService {
protected logger_: Logger
constructor({ logger }: InjectedDependencies) {
this.logger_ = logger
this.logger_.info("[HelloModuleService]: Hello World!")
}
// ...
}
```
### Loader
A loader function accepts as a parameter an object having the property `container`. Its value is the module's container used to resolve resources.
For example:
```ts highlights={[["9"]]}
import {
LoaderOptions,
} from "@medusajs/modules-sdk"
import { Logger } from "@medusajs/medusa"
export default function helloWorldLoader({
container,
}: LoaderOptions) {
const logger: Logger = container.resolve("logger")
logger.info("[helloWorldLoader]: Hello, World!")
}
```