5.3.7. Multiple Services in a Module

In this chapter, you'll learn how to use multiple services in a module.

Module's Main and Internal Services#

A module has one main service only, which is the service exported in the module's definition.

However, you may use other services in your module to better organize your code or split functionalities. These are called internal services that can be resolved within your module, but not in external resources.


How to Add an Internal Service#

1. Create Service#

To add an internal service, create it in the services directory of your module.

For example, create the file src/modules/hello/services/client.ts with the following content:

src/modules/hello/services/client.ts
1export class ClientService {2  async getMessage(): Promise<string> {3    return "Hello, World!"4  }5}

2. Export Service in Index#

Next, create an index.ts file under the services directory of the module that exports your internal services.

For example, create the file src/modules/hello/services/index.ts with the following content:

src/modules/hello/services/index.ts
export * from "./client"

This exports the ClientService.

3. Resolve Internal Service#

Internal services exported in the services/index.ts file of your module are now registered in the container and can be resolved in other services in the module as well as loaders.

For example, in your main service:

src/modules/hello/service.ts
1// other imports...2import { ClientService } from "./services"3
4type InjectedDependencies = {5  clientService: ClientService6}7
8class HelloModuleService extends MedusaService({9  MyCustom,10}){11  protected clientService_: ClientService12
13  constructor({ clientService }: InjectedDependencies) {14    super(...arguments)15    this.clientService_ = clientService16  }17}

You can now use your internal service in your main service.


Resolve Resources in Internal Service#

Resolve dependencies from your module's container in the constructor of your internal service.

For example:

Code
1import { Logger } from "@medusajs/framework/types"2
3type InjectedDependencies = {4  logger: Logger5}6
7export class ClientService {8  protected logger_: Logger9
10  constructor({ logger }: InjectedDependencies) {11    this.logger_ = logger12  }13}

Access Module Options#

Your internal service can't access the module's options.

To retrieve the module's options, use the configModule registered in the module's container, which is the configurations in medusa-config.ts.

For example:

Code
1import { ConfigModule } from "@medusajs/framework/types"2import { HELLO_MODULE } from ".."3
4export type InjectedDependencies = {5  configModule: ConfigModule6}7
8export class ClientService {9  protected options: Record<string, any>10
11  constructor({ configModule }: InjectedDependencies) {12    const moduleDef = configModule.modules[HELLO_MODULE]13
14    if (typeof moduleDef !== "boolean") {15      this.options = moduleDef.options16    }17  }18}

The configModule has a modules property that includes all registered modules. Retrieve the module's configuration using its registration key.

If its value is not a boolean, set the service's options to the module configuration's options property.

Was this chapter helpful?
Edit this page