What is a Module?
A module in Medusa is a package that:- Encapsulates specific domain logic (e.g., product management, inventory)
- Manages its own data models and database schema
- Exposes a service interface for other parts of the application
- Can be easily replaced or extended
Creating a Basic Module
Define the Module Entry Point
Create an
index.ts file that exports your module using the Module utility:src/modules/brand/index.ts
Create Data Models
Define your entities using MikroORM decorators:Export your models:
src/modules/brand/models/brand.ts
src/modules/brand/models/index.ts
Create the Module Service
Extend The
MedusaService to create your module’s main service:src/modules/brand/services/brand-module-service.ts
MedusaService base class automatically provides:createBrands()- Create one or more brandsupdateBrands()- Update brandslistBrands()- List brands with filteringlistAndCountBrands()- List with paginationretrieveBrand()- Get a single brand by IDdeleteBrands()- Delete brandssoftDeleteBrands()- Soft delete brandsrestoreBrands()- Restore soft-deleted brands
Using Service Decorators
Medusa provides decorators for common service patterns:@InjectManager
Injects the database entity manager into the method context. Use this for public methods:@InjectTransactionManager
Injects a transactional entity manager. Use this for protected methods that modify data:@MedusaContext
Marks a parameter as the shared context, which contains the transaction manager and metadata:Module Configuration
You can define configuration options for your module:src/modules/brand/types/index.ts
Best Practices
- Keep modules focused on a single domain
- Use the base
MedusaServicemethods instead of reimplementing CRUD operations - Apply
@InjectManager()to public methods and@InjectTransactionManager()to protected methods - Always include
@MedusaContext()parameter for database operations - Define clear DTO interfaces for type safety
- Use soft deletes for data that may need to be restored
Next Steps
Create Workflows
Compose multi-step business processes
Create Services
Build internal services for module logic