Skip to content

How to create custom singleton service and inject to other services? #2406

Answered by srindom
eric-makro asked this question in Q&A
Discussion options

You must be logged in to vote

We have creation of custom services documented here: https://docs.medusajs.com/advanced/backend/services/create-service

In your Medusa project you can place a file at /src/services/cool-stuff.js that default exports a service.

This service will automatically be registered in Medusa's dependency container as coolStuffService.

To use it in other services you would do something like:

class SomeService {
  // ...
  constructor({ coolStuffService }) {
    // ...
  }
}

To use it in an endpoint controller you can do:

const coolStuffService = req.scope.resolve("coolStuffService`)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@eric-makro
Comment options

Answer selected by srindom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants