on GitHub" data-tooltip-id=":R1blcldtb:">v2.5.1·Edited Oct 21·
In this chapter, you’ll learn about what the service factory is and how to use it.
Medusa provides a service factory that your module’s main service can extend.
The service factory generates data management methods for your data models in the database, so you don't have to implement these methods manually.
Medusa provides the service factory as a MedusaService
function your service extends. The function creates and returns a service class with generated data-management methods.
For example, create the file src/modules/hello/service.ts
with the following content:
The MedusaService
function accepts one parameter, which is an object of data models to generate data-management methods for.
In the example above, since the HelloModuleService
extends MedusaService
, it has methods to manage the MyCustom
data model, such as createMyCustoms
.
The service factory generates methods to manage the records of each of the data models provided in the first parameter in the database.
The method's names are the operation's name, suffixed by the data model's key in the object parameter passed to MedusaService
.
For example, the following methods are generated for the service above:
This method retrieves an array of records based on filters and pagination configurations.
For example:
If you implement the constructor
of your service, make sure to call super
passing it ...arguments
.
For example: