Files
medusa-store/www/apps/book/app/basics/data-models/page.mdx
Shahed Nasser 2e16949979 docs: update imports and package names across docs (#9375)
* docs: update imports and package names across docs
+ reference configs

* generate files

* fix import

* change preview to rc
2024-10-01 11:03:42 +02:00

106 lines
3.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export const metadata = {
title: `${pageNumber} Data Models`,
}
# {metadata.title}
In this chapter, youll learn what data models are and how to create a data model.
## What is a Data Model?
A data model is a class that represents a table in the database.
A data model is created in a module, and its record are managed in the database using the module's service.
---
## How to Create a Data Model?
A data model is created in a TypeScript or JavaScript file under a module's `models` directory. It's defined using the `model` utility imported from `@medusajs/framework/utils`.
For example, create the file `src/modules/hello/models/my-custom.ts` with the following content:
```ts title="src/modules/hello/models/my-custom.ts"
import { model } from "@medusajs/framework/utils"
const MyCustom = model.define("my_custom", {
id: model.id().primaryKey(),
name: model.text(),
})
export default MyCustom
```
You define a data model using the `model`'s `define` method. It accepts two parameters:
1. The first one is the name of the data model's table in the database. It should be in snake-case form.
2. The second is an object, which is the data model's schema. The schema's properties are defined using the `model`'s methods.
The example above defines the data model `MyCustom` with the properties `id` and `name`.
### Generate a Migration
A migration is a TypeScript or JavaScript file that defines changes to be made in the database, such as creating a new table or updating it.
To generate a migration for the data models in your module, run the following command:
```bash
npx medusa db:generate helloModuleService
```
The `db:generate` command of the Medusa CLI accepts one or more module names to generate the migration for.
The module name (for example, `helloModuleService`) is the key used when registering the module in the `modules` configuration in `medusa-config.js`.
The above command creates a migration file at the directory `src/modules/hello/migrations` similar to the following:
```ts
import { Migration } from "@mikro-orm/migrations"
export class Migration20240702105919 extends Migration {
async up(): Promise<void> {
this.addSql("create table if not exists \"my_custom\" (\"id\" text not null, \"name\" text not null, \"created_at\" timestamptz not null default now(), \"updated_at\" timestamptz not null default now(), \"deleted_at\" timestamptz null, constraint \"my_custom_pkey\" primary key (\"id\"));")
}
async down(): Promise<void> {
this.addSql("drop table if exists \"my_custom\" cascade;")
}
}
```
In the migration class, the `up` method creates the table `my_custom` and defines its columns. The `down` method drops the table.
<Note>
Data models automatically have the date properties `created_at`, `updated_at`, and `deleted_at`.
</Note>
### Run Migration
To reflect the changes in the generated migration file, run the `migration` command:
```bash
npx medusa db:migrate
```
If ran successfully, the `my_custom` table will be created in the database.
---
## When to Use Data Models
<Note title="Use data models when" type="success">
You want to store data related to your customization in the database.
</Note>
<Note title="Don't use data models if" type="error">
You want to store simple key-value pairs related to an existing data model. Instead, use the `metadata` field that most existing models have, which is an object of custom key-value pairs.
</Note>