- Split Module and Module Links to their own chapters - Add new docs on db operations and transactions in modules, multiple services, links with custom columns, etc... - Added a list of registered dependencies in a module container
41 lines
1.1 KiB
Plaintext
41 lines
1.1 KiB
Plaintext
export const metadata = {
|
|
title: `${pageNumber} Service Constraints`,
|
|
}
|
|
|
|
# {metadata.title}
|
|
|
|
This chapter lists constraints to keep in mind when creating a service.
|
|
|
|
## Use Async Methods
|
|
|
|
Medusa wraps service method executions to inject useful context or transactions. However, since Medusa can't detect whether the method is asynchronus, it always executes methods in the wrapper with the `await` keyword.
|
|
|
|
For example, if you have a synchronous `getMessage` method, and you use it other resources like workflows, Medusa executes it as an async method:
|
|
|
|
```ts
|
|
await helloModuleService.getMessage()
|
|
```
|
|
|
|
So, make sure your service's methods are always async to avoid unexpected errors or behavior.
|
|
|
|
```ts highlights={[["8", "", "Method must be async."], ["13", "async", "Correct way of defining the method."]]}
|
|
import { MedusaService } from "@medusajs/framework/utils"
|
|
import MyCustom from "./models/my-custom"
|
|
|
|
class HelloModuleService extends MedusaService({
|
|
MyCustom,
|
|
}){
|
|
// Don't
|
|
getMessage(): string {
|
|
return "Hello, World!"
|
|
}
|
|
|
|
// Do
|
|
async getMessage(): Promise<string> {
|
|
return "Hello, World!"
|
|
}
|
|
}
|
|
|
|
export default HelloModuleService
|
|
```
|