Module Directory Structure

In this chapter, you'll learn about the files and directories you can create in a module's directory.

A module is a Go package, so MedusaGo doesn't require a fixed file layout: the compiler resolves your code through imports. The layout below is the one the chapters of this documentation use, and it mirrors the layout of a Medusa module.

Code
src/modules/blog
├── models.go
├── service.go
├── index.go
├── loaders.go
├── options.go
└── migrations
    ├── .snapshot-blog.json
    ├── Migration20261114134517.go
    └── index.go

index.go#

The index.go file holds the module's definition: the call to Module with the module's data models, migrations, loaders, and the function that builds its service. It also exports the value you register in medusa-config.go.

src/modules/blog/index.go
// Module is the definition of the blog module.
var Module = utilsmodulessdk.Module(Name, utilsmodulessdk.ModuleDefinition{
	Models:     Models,
	Migrations: migrations.Migrations,
	Loaders:    []typesmodulessdk.Loader{SeedTags},
	Service: func(opts typesmodulessdk.ServiceOptions, base *utilsmodulessdk.MedusaService) (any, error) {
		options, err := ParseOptions(opts.Options)
		if err != nil {
			return nil, err
		}
		return &BlogModuleService{MedusaService: base, options: options}, nil
	},
})

Learn more in the Modules chapter.

service.go#

The service.go file holds the module's main service: the struct that embeds *MedusaService and the methods you add to it.

src/modules/blog/service.go
// BlogModuleService is the service of the blog module. Embedding
// *MedusaService gives it the generated methods (listPosts, createPosts, ...)
// and the internal service of each model.
type BlogModuleService struct {
	*utilsmodulessdk.MedusaService
	options Options
}

Learn more in the Service Factory chapter.

Other Files and Directories#

You can add the following to the module:

| File or directory | Description | | --- | --- | | models.go | The module's data models. A module with many models can group them in a models package, with one file per model. | | loaders.go | The module's loaders, the functions that run when the module is loaded. | | options.go | The type and the validation of the module's options. | | migrations | The module's migrations. db:generate writes it in the directory of the module, as a Go package named migrations, with an index.go file that lists the migrations and a .snapshot-<module>.json file with the schema snapshot. |

Note: A module can't import the packages of another module, as it must remain isolated. Use module links to relate the data models of different modules, and Query to retrieve them together.
Found a problem with this page?Report Issue
Edited Nov 14·Edit this page