* docs: changes based on DX changes * remove fields no longer needed * remove unnecessary parameters * fixes to authenticate middleware usage * add highlight to migrations config * change configuration to http * added missing remote link docs * fix name in sidebar * added notification module docs + updated file module docs * add vale exceptions * fix vale errors * added docs on custom cli scripts
69 lines
1.9 KiB
Plaintext
69 lines
1.9 KiB
Plaintext
export const metadata = {
|
||
title: `${pageNumber} Database Operations in Service Methods`,
|
||
}
|
||
|
||
# {metadata.title}
|
||
|
||
In this document, you’ll learn how to implement database operations, such as creating a record, in the main service.
|
||
|
||
## Use the Data Model's Generated Service
|
||
|
||
To perform database operations on a data model, use the model's generated service in the module's container.
|
||
|
||
For example:
|
||
|
||
export const highlights = [
|
||
["13", "", "Inject myCustomService, which is the generated service of the `MyCustom` data model."],
|
||
["22", "", "Add a new field for the generated service of the MyCustom data model."],
|
||
["29", "", "Set the class field to the injected dependency."],
|
||
["35", "create", "Use the `create` method of the generated service."]
|
||
]
|
||
|
||
```ts title="src/modules/hello/service.ts" highlights={highlights}
|
||
// other imports...
|
||
import { ModulesSdkTypes } from "@medusajs/types"
|
||
import { MyCustom } from "./models/my-custom"
|
||
|
||
// ...
|
||
|
||
// recommended to define type in another file
|
||
type CreateMyCustomDTO = {
|
||
name: string
|
||
}
|
||
|
||
type InjectedDependencies = {
|
||
myCustomService: ModulesSdkTypes.InternalModuleService<any>
|
||
}
|
||
|
||
class HelloModuleService extends ModulesSdkUtils
|
||
.abstractModuleServiceFactory<
|
||
// ...
|
||
>(
|
||
// ...
|
||
) {
|
||
protected myCustomService_: ModulesSdkTypes.InternalModuleService<MyCustom>
|
||
|
||
constructor(
|
||
{ myCustomService }: InjectedDependencies
|
||
) {
|
||
// @ts-ignore
|
||
super(...arguments)
|
||
this.myCustomService_ = myCustomService
|
||
}
|
||
|
||
async create(
|
||
data: CreateMyCustomDTO
|
||
): Promise<MyCustomDTO> {
|
||
const myCustom = await this.myCustomService_.create(
|
||
data
|
||
)
|
||
|
||
return myCustom
|
||
}
|
||
}
|
||
```
|
||
|
||
In the above example, you resolve `myCustomService` in the main service's constructor. The `myCustomService` is the generated service for the `myCustom` data model.
|
||
|
||
Then, in the `create` method of the main service, you use `myCustomService`'s `create` method to create the record.
|