Raw
1 export const metadata = {
2 title: `${pageNumber} Guide: Implement Brand Module`,
3 }
4
5 # {metadata.title}
6
7 In this chapter, you'll build a Brand Module that adds a `brand` table to the database and provides data-management features for it.
8
9 A [module](../../../fundamentals/modules/page.mdx) 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](!resources!/commerce-modules/cart) that holds the data models and business logic for cart operations.
10
11 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.
12
13 ![Diagram showcasing an overview of the Brand Module](https://res.cloudinary.com/dza7lstvk/image/upload/v1746546820/Medusa%20Resources/brand-module_pg86gm.jpg)
14
15 ## 1. Create Module Directory
16
17 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.
18
19 ![Directory structure in Medusa project after adding the brand directory](https://res.cloudinary.com/dza7lstvk/image/upload/v1732868844/Medusa%20Book/brand-dir-overview-1_hxwvgx.jpg)
20
21 ---
22
23 ## 2. Create Data Model
24
25 A [data model](../../../fundamentals/modules/page.mdx#1-create-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.
26
27 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:
28
29 ![Directory structure in module after adding the brand data model](https://res.cloudinary.com/dza7lstvk/image/upload/v1732868920/Medusa%20Book/brand-dir-overview-2_lexhdl.jpg)
30
31 ```ts title="src/modules/brand/models/brand.ts"
32 import { model } from "@medusajs/framework/utils"
33
34 export const Brand = model.define("brand", {
35 id: model.id().primaryKey(),
36 name: model.text(),
37 })
38 ```
39
40 You create a `Brand` data model which has an `id` primary key property, and a `name` text property.
41
42 You define the data model using the `define` method of the DML. It accepts two parameters:
43
44 1. The first one is the name of the data model's table in the database. Use snake-case names.
45 2. The second is an object, which is the data model's schema.
46
47 <Note title="Tip">
48
49 Refer to the [Properties](../../../fundamentals/data-models/properties/page.mdx) chapter to learn more.
50
51 </Note>
52
53 ---
54
55 ## 3. Create Module Service
56
57 You perform database operations on your data models in a [service](../../../fundamentals/modules/page.mdx#2-create-service), which is a class exported by the module and acts like an interface to its functionalities.
58
59 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.
60
61 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:
62
63 ![Directory structure in module after adding the service](https://res.cloudinary.com/dza7lstvk/image/upload/v1732868984/Medusa%20Book/brand-dir-overview-3_jo7baj.jpg)
64
65 export const serviceHighlights = [
66 ["4", "MedusaService", "A service factory that generates data-management methods."]
67 ]
68
69 ```ts title="src/modules/brand/service.ts" highlights={serviceHighlights}
70 import { MedusaService } from "@medusajs/framework/utils"
71 import { Brand } from "./models/brand"
72
73 class BrandModuleService extends MedusaService({
74 Brand,
75 }) {
76
77 }
78
79 export default BrandModuleService
80 ```
81
82 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.
83
84 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.
85
86 You'll use these methods in the [next chapter](../workflow/page.mdx).
87
88 <Note title="Tip">
89
90 Refer to the [service factory reference](!resources!/service-factory-reference) to learn more about the generated methods.
91
92 </Note>
93
94 ---
95
96 ## 4. Export Module Definition
97
98 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.
99
100 So, to export the Brand Module's definition, create the file `src/modules/brand/index.ts` with the following content:
101
102 ![Directory structure in module after adding the definition file](https://res.cloudinary.com/dza7lstvk/image/upload/v1732869045/Medusa%20Book/brand-dir-overview-4_nf8ymw.jpg)
103
104 ```ts title="src/modules/brand/index.ts"
105 import { Module } from "@medusajs/framework/utils"
106 import BrandModuleService from "./service"
107
108 export const BRAND_MODULE = "brand"
109
110 export default Module(BRAND_MODULE, {
111 service: BrandModuleService,
112 })
113 ```
114
115 You use `Module` from the Modules SDK to create the module's definition. It accepts two parameters:
116
117 1. The module's name (`brand`). You'll use this name when you use this module in other customizations.
118 2. An object with a required property `service` indicating the module's main service.
119
120 <Note title="Tip">
121
122 You export `BRAND_MODULE` to reference the module's name more reliably in other customizations.
123
124 </Note>
125
126 ---
127
128 ## 5. Add Module to Medusa's Configurations
129
130 To start using your module, you must add it to Medusa's configurations in `medusa-config.ts`.
131
132 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`:
133
134 ```ts title="medusa-config.ts"
135 module.exports = defineConfig({
136 // ...
137 modules: [
138 {
139 resolve: "./src/modules/brand",
140 },
141 ],
142 })
143 ```
144
145 The Brand Module is now added to your Medusa application. You'll start using it in the [next chapter](../workflow/page.mdx).
146
147 ---
148
149 ## 6. Generate and Run Migrations
150
151 A [migration](../../../fundamentals/modules/page.mdx#5-generate-migrations) 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.
152
153 [Medusa's CLI tool](!resources!/medusa-cli) 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:
154
155 ```bash npx2yarn
156 npx medusa db:generate brand
157 npx medusa db:migrate
158 ```
159
160 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.
161
162 ---
163
164 ## Next Step: Create Brand Workflow
165
166 The Brand Module now creates a `brand` table in the database and provides a class to manage its records.
167
168 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.