5.3.2. Module's Container

In this chapter, you'll learn about the module's container and how to resolve resources in that container.

Module's Container#

Since modules are isolated, 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.

List of Registered Resources#

Find a list of resources or dependencies registered in a module's container in this Development Resources reference.


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:

Code
1import { Logger } from "@medusajs/framework/types"2
3type InjectedDependencies = {4  logger: Logger5}6
7export default class HelloModuleService {8  protected logger_: Logger9
10  constructor({ logger }: InjectedDependencies) {11    this.logger_ = logger12
13    this.logger_.info("[HelloModuleService]: Hello World!")14  }15
16  // ...17}

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:

Code
1import {2  LoaderOptions,3} from "@medusajs/framework/types"4import { 5  ContainerRegistrationKeys6} from "@medusajs/framework/utils"7
8export default async function helloWorldLoader({9  container,10}: LoaderOptions) {11  const logger = container.resolve(ContainerRegistrationKeys.LOGGER)12
13  logger.info("[helloWorldLoader]: Hello, World!")14}
Was this chapter helpful?
Edit this page