Files
medusa-store/docs/content/advanced/backend/services/create-service.md
Shahed Nasser d1b4b11ff6 chore(docs): added eslint to lint documentation code blocks (#2920)
* docs: added rule for code length

* chore: fixes based on vale errors

* changed to use eslint

* fixes using eslint

* added github action for documentation eslint

* changed allowed max-length

* fixed incorrect heading level

* removed comment
2022-12-30 18:44:46 +02:00

118 lines
3.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Create a Service
In this document, youll learn how you can create a [Service](./overview.md) and use it across your Medusa server just like any of the core services.
## Implementation
To create a service, create a TypeScript or JavaScript file in `src/services` to hold the service. The name of the file should be the registration name of the service without `Service` as it will be appended to it by default.
For example, if you want to create a service `helloService`, create the file `hello.ts` in `src/services` with the following content:
```ts title=/src/services/hello.ts
import { TransactionBaseService } from "@medusajs/medusa"
import { EntityManager } from "typeorm"
class HelloService extends TransactionBaseService {
protected manager_: EntityManager
protected transactionManager_: EntityManager
getMessage() {
return `Welcome to My Store!`
}
}
export default HelloService
```
---
## Service Constructor
As the service extends the `TransactionBaseService` class, all services in Medusas core, as well as all your custom services, will be available in your services constructor using dependency injection.
So, if you want your service to use another service, simply add it as part of your constructors dependencies and set it to a field inside your services class:
```ts
class HelloService extends TransactionBaseService {
private productService: ProductService
constructor(container) {
super(container)
this.productService = container.productService
}
// ...
}
```
Then, you can use that service anywhere in your custom service:
```ts
class HelloService extends TransactionBaseService {
// ...
async getProductCount() {
return await this.productService.count()
}
}
```
---
## Use a Service
In this section, you'll learn how to use services throughout your Medusa server. This includes both Medusa's services and your custom services.
:::note
Before using your service, make sure you run the `build` command:
```bash npm2yarn
npm run build
```
:::
### In a Service
To use your custom service in another custom service, you can have easy access to it in the dependencies injected to the constructor of your service:
```ts
class MyService extends TransactionBaseService {
constructor(container) {
super(container)
this.helloService = container.helloService
}
// ...
}
```
### In an Endpoint
To use your custom service in an endpoint, you can use `req.scope.resolve` passing it the services registration name:
```ts
const helloService = req.scope.resolve("helloService")
res.json({
message: helloService.getMessage(),
})
```
### In a Subscriber
To use your custom service in a subscriber, you can have easy access to it in the subscribers dependencies injected to the constructor of your subscriber:
```ts
class MySubscriber {
constructor({ helloService, eventBusService }) {
this.helloService = helloService
}
// ...
}
```
---
## See Also
- [Services Reference](/references/services/classes/AuthService)
- [Create an Endpoint](../endpoints/add.md)