- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
- Get Started
- Product
- Resources
- Tools & SDKs
- Framework
- Reference
2.1.1. Guide: Implement Brand Module
In this chapter, you'll build a Brand Module that adds a brand
table to the database and provides data-management features for it.
A module is a reusable package of functionalities related to a single domain or integration. Medusa comes with multiple pre-built modules for core commerce needs, such as the Cart Module that holds the data models and business logic for cart operations.
In a module, you create data models and business logic to manage them. In the next chapters, you'll see how you use the module to build commerce features.
1. Create Module Directory#
Modules are created in a sub-directory of src/modules
. So, start by creating the directory src/modules/brand
that will hold the Brand Module's files.
2. Create Data Model#
A data model represents a table in the database. You create data models using Medusa's Data Model Language (DML). It simplifies defining a table's columns, relations, and indexes with straightforward methods and configurations.
You create a data model in a TypeScript or JavaScript file under the models
directory of a module. So, to create a data model that represents a new brand
table in the database, create the file src/modules/brand/models/brand.ts
with the following content:
You create a Brand
data model which has an id
primary key property, and a name
text property.
You define the data model using the define
method of the DML. It accepts two parameters:
- The first one is the name of the data model's table in the database. Use snake-case names.
- The second is an object, which is the data model's schema.
3. Create Module Service#
You perform database operations on your data models in a service, which is a class exported by the module and acts like an interface to its functionalities.
In this step, you'll create the Brand Module's service that provides methods to manage the Brand
data model. In the next chapters, you'll use this service when exposing custom features that involve managing brands.
You define a service in a service.ts
or service.js
file at the root of your module's directory. So, create the file src/modules/brand/service.ts
with the following content:
The BrandModuleService
extends a class returned by MedusaService
from the Modules SDK. This function generates a class with data-management methods for your module's data models.
The MedusaService
function receives an object of the module's data models as a parameter, and generates methods to manage those data models. So, the BrandModuleService
now has methods like createBrands
and retrieveBrand
to manage the Brand
data model.
You'll use these methods in the next chapter.
4. Export Module Definition#
A module must export a definition that tells Medusa the name of the module and its main service. This definition is exported in an index.ts
file at the module's root directory.
So, to export the Brand Module's definition, create the file src/modules/brand/index.ts
with the following content:
You use Module
from the Modules SDK to create the module's definition. It accepts two parameters:
- The module's name (
brand
). You'll use this name when you use this module in other customizations. - An object with a required property
service
indicating the module's main service.
BRAND_MODULE
to reference the module's name more reliably in other customizations.5. Add Module to Medusa's Configurations#
To start using your module, you must add it to Medusa's configurations in medusa-config.ts
.
The object passed to defineConfig
in medusa-config.ts
accepts a modules
property, whose value is an array of modules to add to the application. So, add the following in medusa-config.ts
:
The Brand Module is now added to your Medusa application. You'll start using it in the next chapter.
6. Generate and Run Migrations#
A migration is a TypeScript or JavaScript file that defines database changes made by a module. Migrations ensure that your module is re-usable and removes friction when working in a team, making it easy to reflect changes across team members' databases.
Medusa's CLI tool allows you to generate migration files for your module, then run those migrations to reflect the changes in the database. So, run the following commands in your Medusa application's directory:
The db:generate
command accepts as an argument the name of the module to generate the migrations for, and the db:migrate
command runs all migrations that haven't been run yet in the Medusa application.
Next Step: Create Brand Workflow#
The Brand Module now creates a brand
table in the database and provides a class to manage its records.
In the next chapter, you'll implement the functionality to create a brand in a workflow. You'll then use that workflow in a later chapter to expose an endpoint that allows admin users to create a brand.