docs: general fixes and improvements (#7918)
* docs improvements and changes * updated module definition * modules + dml changes * fix build * fix vale error * fix lint errors * fixes to stripe docs * fix condition * fix condition * fix module defintion * fix checkout * disable UI action * change oas preview action * flatten provider module options * fix lint errors * add module link docs * pr comments fixes * fix vale error * change node engine version * links -> linkable * add note about database name * small fixes * link fixes * fix response code in api reference * added migrations step
This commit is contained in:
@@ -12,17 +12,17 @@ A data model is a class that represents a table in the database. It's created in
|
||||
|
||||
---
|
||||
|
||||
## How to Create a Data Model
|
||||
## How to Create a Data Model?
|
||||
|
||||
<Note title="Steps Summary">
|
||||
|
||||
1. Create data model class in a module.
|
||||
2. Generate migration for the data model.
|
||||
4. Run migration to add table for data model in the database.
|
||||
1. Create a data model in a module.
|
||||
2. Create migration for the data model.
|
||||
4. Run migration to add the data model's table in the database.
|
||||
|
||||
</Note>
|
||||
|
||||
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 `@medjusajs/utils`.
|
||||
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/utils`.
|
||||
|
||||
For example, create the file `src/modules/hello/models/my-custom.ts` with the following content:
|
||||
|
||||
@@ -48,8 +48,6 @@ The example above defines the data model `MyCustom` with the properties `id` and
|
||||
|
||||
A migration defines changes to be made in the database, such as create or update tables.
|
||||
|
||||
So, you must create a migration that creates a table for your data model in the database.
|
||||
|
||||
A migration is a class created in a TypeScript or JavaScript file under a module's `migrations` directory. It has two methods:
|
||||
|
||||
- The `up` method reflects changes on the database.
|
||||
@@ -74,6 +72,15 @@ A migration is a class created in a TypeScript or JavaScript file under a module
|
||||
})
|
||||
```
|
||||
|
||||
<Note title="Important">
|
||||
|
||||
The `databaseName`'s value is only used to generate the migrations. So, make sure it:
|
||||
|
||||
1. Is unique to the module. Two modules shouldn't have the same database name, and it shouldn't be the same as the Medusa application.
|
||||
2. Doesn't change between migrations, as that causes inconsistencies between the migrations.
|
||||
|
||||
</Note>
|
||||
|
||||
2. Run the following command in the root directory of your Medusa application:
|
||||
|
||||
```bash
|
||||
@@ -95,7 +102,7 @@ For example:
|
||||
```ts title="src/modules/migrations/Migration20240429090012.ts"
|
||||
import { Migration } from "@mikro-orm/migrations"
|
||||
|
||||
export class Migration20240624145652 extends Migration {
|
||||
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\"));")
|
||||
@@ -120,7 +127,7 @@ The queries performed in each of the methods use PostgreSQL syntax.
|
||||
|
||||
To reflect the changes in the migration, run the `migration` command:
|
||||
|
||||
```bash npm2yarn
|
||||
```bash
|
||||
npx medusa migrations run
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user