Subchapter 2.7
checkpoints/checkpoint-module.mdMarkdown4 KBView on GitHub
This checkpoint verifies that you’ve successfully created the Brand Module with its data model, service, and migrations.
Before proceeding, test your understanding:
What does MedusaService() do?
MedusaService() is a service factory provided by Medusa that generates a service with CRUD methods (create, update, retrieve, list, delete) for your data model. It saves you from writing boilerplate code.
Why is the module name “brand” and not “brand-module”?
Medusa uses camelCase naming for modules and automatically adds “Module” as a suffix when resolving dependencies. So “brand” becomes “brandModule” internally. Using “brand-module” would result in “brandModuleModule”.
What would happen if you forgot to run migrations?
The brand table wouldn’t exist in your database, and any attempt to create or retrieve brands would fail with database errors like “relation ‘brand’ does not exist”.
Why do we need to export both the service AND the module from index.ts?
Exporting the service makes it available for dependency injection in workflows and API routes.
Let me verify your implementation. Please share the following:
Run this command and share the output:
ls -R src/modules/brandExpected structure:
src/modules/brand:
index.ts models service.ts
src/modules/brand/models:
brand.tsShow me your src/modules/brand/models/brand.ts file.
Key things to check:
model.define() with “brand” as first argument (lowercase, snake-case)id: model.id().primaryKey()name: model.text()Show me your src/modules/brand/service.ts file.
Key things to check:
MedusaService(Brand) (capital B for the model import)Show me your src/modules/brand/index.ts file.
Key things to check:
BrandService from service.tsModule() with name “brand” (lowercase)Show me the modules section of your medusa-config.ts.
Key things to check:
resolve: "./modules/brand"options: {}Run this command and share the output:
npx medusa db:migrateExpected output: Should show migration succeeded without errors.
Run this command and share any errors:
npm run buildExpected output: Build should succeed. If there are TypeScript errors, share them with me so we can debug together.
Symptom: Error when running build or starting server
Cause: Module not registered in medusa-config.ts
Fix:
medusa-config.tsmodules array:
{
resolve: "./modules/brand",
options: {},
}Symptom: Error about module naming convention
Cause: Used “brand-module” or “brandModule” as module name
Fix:
Change module name to just “brand” in index.ts:
export default Module("brand", {
service: BrandService,
})Verify each of these steps:
medusa-config.ts modules arrayOnce this checkpoint passes:
The module provides the data layer. Now we’ll build the workflow to orchestrate brand creation with automatic rollback capabilities.
Ready to continue? Let me know when all checks pass, and we’ll move on to creating the workflow.