Custom Module
Last updated
Last updated
A module is a package of reusable functionalities. It can be integrated into your Medusa application without affecting the overall system. You can create a module as part of a plugin.
Learn more about modules in .
To create a module:
A data model represents a table in the database. You create a data model in a TypeScript or JavaScript file under the models
directory of a module.
For example, create the file src/modules/blog/models/post.ts
with the following content:
A module must define a service. A service is a TypeScript or JavaScript class holding methods related to a business logic or commerce functionality.
For example, create the file src/modules/blog/service.ts
with the following content:
A module must have an index.ts
file in its root directory that exports its definition. The definition specifies the main service of the module.
For example, create the file src/modules/blog/index.ts
with the following content:
To start using the module, add it to medusa-config.ts
:
To generate migrations for your module, run the following command:
Then, to run migrations, run the following command:
You can use the module in customizations within the Medusa application, such as workflows and API routes.
For example, to use the module in an API route: