|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Data Models`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this chapter, you'll learn what a data model is and how to create a data model. |
|
8
|
|
|
9
|
## What is a Data Model? |
|
10
|
|
|
11
|
A data model represents a table in the database. You create data models using Medusa's data modeling language (DML). It simplifies defining a table's columns, relations, and indexes with straightforward methods and configurations. |
|
12
|
|
|
13
|
You create a data model in a [module](../modules/page.mdx). The module's service provides the methods to store and manage those data models. Then, you can resolve the module's service in other customizations, such as a [workflow](../workflows/page.mdx), to manage the data models' records. |
|
14
|
|
|
15
|
--- |
|
16
|
|
|
17
|
## How to Create a Data Model |
|
18
|
|
|
19
|
In a module, you can create a data model in a TypeScript or JavaScript file under the module's `models` directory. |
|
20
|
|
|
21
|
So, for example, assuming you have a Blog Module at `src/modules/blog`, you can create a `Post` data model by creating the `src/modules/blog/models/post.ts` file with the following content: |
|
22
|
|
|
23
|
 |
|
24
|
|
|
25
|
```ts title="src/modules/blog/models/post.ts" |
|
26
|
import { model } from "@medusajs/framework/utils" |
|
27
|
|
|
28
|
const Post = model.define("post", { |
|
29
|
id: model.id().primaryKey(), |
|
30
|
title: model.text(), |
|
31
|
}) |
|
32
|
|
|
33
|
export default Post |
|
34
|
``` |
|
35
|
|
|
36
|
You define the data model using the `define` method of the DML. It accepts two parameters: |
|
37
|
|
|
38
|
1. The first one is the name of the data model's table in the database. Use snake-case names. |
|
39
|
2. The second is an object, which is the data model's schema. The schema's properties are defined using the `model`'s methods, such as `text` and `id`. |
|
40
|
- Data models automatically have the date properties `created_at`, `updated_at`, and `deleted_at`, so you don't need to add them manually. |
|
41
|
|
|
42
|
The code snippet above defines a `Post` data model with `id` and `title` properties. |
|
43
|
|
|
44
|
--- |
|
45
|
|
|
46
|
## Generate Migrations |
|
47
|
|
|
48
|
After you create a data model in a module, then [register that module in your Medusa configurations](../modules/page.mdx#4-add-module-to-medusas-configurations), you must generate a migration to create the data model's table in the database. |
|
49
|
|
|
50
|
A migration is a TypeScript or JavaScript file that defines database changes made by a module. Migrations are useful when you re-use a module or you're working in a team, so that when one member of a team makes a database change, everyone else can reflect it on their side by running the migrations. |
|
51
|
|
|
52
|
For example, to generate a migration for the Blog Module, run the following command in your Medusa application's directory: |
|
53
|
|
|
54
|
<Note> |
|
55
|
|
|
56
|
If you're creating the module in a plugin, use the [plugin\:db\:generate command](!resources!/medusa-cli/commands/plugin#plugindbgenerate) instead. |
|
57
|
|
|
58
|
</Note> |
|
59
|
|
|
60
|
```bash npx2yarn |
|
61
|
npx medusa db:generate blog |
|
62
|
``` |
|
63
|
|
|
64
|
The `db:generate` command of the Medusa CLI accepts one or more module names to generate the migration for. It will create a migration file for the Blog Module in the directory `src/modules/blog/migrations` similar to the following: |
|
65
|
|
|
66
|
<Note> |
|
67
|
|
|
68
|
As of [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/v2.11.0), MikroORM dependencies are included in the `@medusajs/framework` package. If you're using an older version of Medusa, change the import statement to `@mikro-orm/migrations`. |
|
69
|
|
|
70
|
</Note> |
|
71
|
|
|
72
|
```ts |
|
73
|
import { Migration } from "@medusajs/framework/mikro-orm/migrations" |
|
74
|
|
|
75
|
export class Migration20241121103722 extends Migration { |
|
76
|
|
|
77
|
async up(): Promise<void> { |
|
78
|
this.addSql("create table if not exists \"post\" (\"id\" text not null, \"title\" text not null, \"created_at\" timestamptz not null default now(), \"updated_at\" timestamptz not null default now(), \"deleted_at\" timestamptz null, constraint \"post_pkey\" primary key (\"id\"));") |
|
79
|
} |
|
80
|
|
|
81
|
async down(): Promise<void> { |
|
82
|
this.addSql("drop table if exists \"post\" cascade;") |
|
83
|
} |
|
84
|
|
|
85
|
} |
|
86
|
``` |
|
87
|
|
|
88
|
In the migration class, the `up` method creates the table `post` and defines its columns using PostgreSQL syntax. The `down` method drops the table. |
|
89
|
|
|
90
|
### Run Migrations |
|
91
|
|
|
92
|
To reflect the changes in the generated migration file on the database, run the `db:migrate` command: |
|
93
|
|
|
94
|
<Note> |
|
95
|
|
|
96
|
If you're creating the module in a plugin, run this command on the Medusa application that the plugin is installed in. |
|
97
|
|
|
98
|
</Note> |
|
99
|
|
|
100
|
```bash npx2yarn |
|
101
|
npx medusa db:migrate |
|
102
|
``` |
|
103
|
|
|
104
|
This creates the `post` table in the database. |
|
105
|
|
|
106
|
### Migrations on Data Model Changes |
|
107
|
|
|
108
|
Whenever you make a change to a data model, you must generate and run the migrations. |
|
109
|
|
|
110
|
For example, if you add a new column to the `Post` data model, you must generate a new migration and run it. |
|
111
|
|
|
112
|
--- |
|
113
|
|
|
114
|
## Manage Data Models |
|
115
|
|
|
116
|
Your module's service should extend the [service factory](../modules/service-factory/page.mdx), which generates data-management methods for your module's data models. |
|
117
|
|
|
118
|
For example, the Blog Module's service would have methods like `retrievePost` and `createPosts`. |
|
119
|
|
|
120
|
Refer to the [Service Factory](../modules/service-factory/page.mdx) chapter to learn more about how to extend the service factory and manage data models, and refer to the [Service Factory Reference](!resources!/service-factory-reference) for the full list of generated methods and how to use them. |