docs: docs for next release (#14456)
This commit is contained in:
@@ -200,43 +200,43 @@ const Post = model.define("post", {
|
||||
id: model.id().primaryKey(),
|
||||
title: model.text(),
|
||||
author: model.belongsTo(() => Author, {
|
||||
mappedBy: "posts"
|
||||
})
|
||||
mappedBy: "posts",
|
||||
}),
|
||||
})
|
||||
|
||||
const Author = model.define("author", {
|
||||
id: model.id().primaryKey(),
|
||||
name: model.text(),
|
||||
posts: model.hasMany(() => Post, {
|
||||
mappedBy: "author"
|
||||
})
|
||||
mappedBy: "author",
|
||||
}),
|
||||
})
|
||||
```
|
||||
|
||||
To create a migration that reflects this relationship in the database, you can create a migration file as follows:
|
||||
|
||||
```ts title="src/modules/blog/migrations/Migration202507021200_create_author_and_post.ts"
|
||||
import { Migration } from "@medusajs/framework/mikro-orm/migrations";
|
||||
import { Migration } from "@medusajs/framework/mikro-orm/migrations"
|
||||
|
||||
export class Migration20251230112505 extends Migration {
|
||||
|
||||
override async up(): Promise<void> {
|
||||
this.addSql(`create table if not exists "author" ("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 "author_pkey" primary key ("id"));`);
|
||||
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_author_deleted_at" ON "author" ("deleted_at") WHERE deleted_at IS NULL;`);
|
||||
this.addSql(`create table if not exists "author" ("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 "author_pkey" primary key ("id"));`)
|
||||
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_author_deleted_at" ON "author" ("deleted_at") WHERE deleted_at IS NULL;`)
|
||||
|
||||
this.addSql(`create table if not exists "post" ("id" text not null, "title" text not null, "author_id" 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"));`);
|
||||
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_post_author_id" ON "post" ("author_id") WHERE deleted_at IS NULL;`);
|
||||
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_post_deleted_at" ON "post" ("deleted_at") WHERE deleted_at IS NULL;`);
|
||||
this.addSql(`create table if not exists "post" ("id" text not null, "title" text not null, "author_id" 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"));`)
|
||||
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_post_author_id" ON "post" ("author_id") WHERE deleted_at IS NULL;`)
|
||||
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_post_deleted_at" ON "post" ("deleted_at") WHERE deleted_at IS NULL;`)
|
||||
|
||||
this.addSql(`alter table if exists "post" add constraint "post_author_id_foreign" foreign key ("author_id") references "author" ("id") on update cascade;`);
|
||||
this.addSql(`alter table if exists "post" add constraint "post_author_id_foreign" foreign key ("author_id") references "author" ("id") on update cascade;`)
|
||||
}
|
||||
|
||||
override async down(): Promise<void> {
|
||||
this.addSql(`alter table if exists "post" drop constraint if exists "post_author_id_foreign";`);
|
||||
this.addSql(`alter table if exists "post" drop constraint if exists "post_author_id_foreign";`)
|
||||
|
||||
this.addSql(`drop table if exists "author" cascade;`);
|
||||
this.addSql(`drop table if exists "author" cascade;`)
|
||||
|
||||
this.addSql(`drop table if exists "post" cascade;`);
|
||||
this.addSql(`drop table if exists "post" cascade;`)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -251,16 +251,16 @@ Consider you have an existing `Post` data model, and you added a new `published_
|
||||
You can create a migration file to add this new column as follows:
|
||||
|
||||
```ts title="src/modules/blog/migrations/Migration202507021300_add_published_at_to_post.ts"
|
||||
import { Migration } from "@medusajs/framework/mikro-orm/migrations";
|
||||
import { Migration } from "@medusajs/framework/mikro-orm/migrations"
|
||||
|
||||
export class Migration20251230113025 extends Migration {
|
||||
|
||||
override async up(): Promise<void> {
|
||||
this.addSql(`alter table if exists "post" add column if not exists "published_at" timestamptz null;`);
|
||||
this.addSql(`alter table if exists "post" add column if not exists "published_at" timestamptz null;`)
|
||||
}
|
||||
|
||||
override async down(): Promise<void> {
|
||||
this.addSql(`alter table if exists "post" drop column if exists "published_at";`);
|
||||
this.addSql(`alter table if exists "post" drop column if exists "published_at";`)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -275,16 +275,16 @@ Consider you have an existing `Post` data model, and you removed the `published_
|
||||
You can create a migration file to remove this column as follows:
|
||||
|
||||
```ts title="src/modules/blog/migrations/Migration202507021400_remove_published_at_from_post.ts"
|
||||
import { Migration } from "@medusajs/framework/mikro-orm/migrations";
|
||||
import { Migration } from "@medusajs/framework/mikro-orm/migrations"
|
||||
|
||||
export class Migration20251230113125 extends Migration {
|
||||
|
||||
override async up(): Promise<void> {
|
||||
this.addSql(`alter table if exists "post" drop column if exists "published_at";`);
|
||||
this.addSql(`alter table if exists "post" drop column if exists "published_at";`)
|
||||
}
|
||||
|
||||
override async down(): Promise<void> {
|
||||
this.addSql(`alter table if exists "post" add column if not exists "published_at" timestamptz null;`);
|
||||
this.addSql(`alter table if exists "post" add column if not exists "published_at" timestamptz null;`)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -299,16 +299,16 @@ Consider you have an existing `Post` data model with a `title` column, and you r
|
||||
You can create a migration file to rename this column as follows:
|
||||
|
||||
```ts title="src/modules/blog/migrations/Migration202507021500_rename_title_to_headline_in_post.ts"
|
||||
import { Migration } from "@medusajs/framework/mikro-orm/migrations";
|
||||
import { Migration } from "@medusajs/framework/mikro-orm/migrations"
|
||||
|
||||
export class Migration20251230113214 extends Migration {
|
||||
|
||||
override async up(): Promise<void> {
|
||||
this.addSql(`alter table if exists "post" rename column "title" to "headline";`);
|
||||
this.addSql(`alter table if exists "post" rename column "title" to "headline";`)
|
||||
}
|
||||
|
||||
override async down(): Promise<void> {
|
||||
this.addSql(`alter table if exists "post" rename column "headline" to "title";`);
|
||||
this.addSql(`alter table if exists "post" rename column "headline" to "title";`)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -332,24 +332,24 @@ export const Post = model.define("post", {
|
||||
id: model.id().primaryKey(),
|
||||
headline: model.text().index(),
|
||||
author: model.belongsTo(() => Author, {
|
||||
mappedBy: "posts"
|
||||
})
|
||||
mappedBy: "posts",
|
||||
}),
|
||||
})
|
||||
```
|
||||
|
||||
You can create a migration file to create this index as follows:
|
||||
|
||||
```ts title="src/modules/blog/migrations/Migration202507021600_create_index_on_headline_in_post.ts"
|
||||
import { Migration } from "@medusajs/framework/mikro-orm/migrations";
|
||||
import { Migration } from "@medusajs/framework/mikro-orm/migrations"
|
||||
|
||||
export class Migration20251230113322 extends Migration {
|
||||
|
||||
override async up(): Promise<void> {
|
||||
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_post_headline" ON "post" ("headline") WHERE deleted_at IS NULL;`);
|
||||
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_post_headline" ON "post" ("headline") WHERE deleted_at IS NULL;`)
|
||||
}
|
||||
|
||||
override async down(): Promise<void> {
|
||||
this.addSql(`drop index if exists "IDX_post_headline";`);
|
||||
this.addSql(`drop index if exists "IDX_post_headline";`)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -364,16 +364,16 @@ Consider you have an existing `Post` data model with an index on the `headline`
|
||||
You can create a migration file to drop this index as follows:
|
||||
|
||||
```ts title="src/modules/blog/migrations/Migration202507021700_drop_index_on_headline_in_post.ts"
|
||||
import { Migration } from "@medusajs/framework/mikro-orm/migrations";
|
||||
import { Migration } from "@medusajs/framework/mikro-orm/migrations"
|
||||
|
||||
export class Migration20251230113350 extends Migration {
|
||||
|
||||
override async up(): Promise<void> {
|
||||
this.addSql(`drop index if exists "IDX_post_headline";`);
|
||||
this.addSql(`drop index if exists "IDX_post_headline";`)
|
||||
}
|
||||
|
||||
override async down(): Promise<void> {
|
||||
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_post_headline" ON "post" ("headline") WHERE deleted_at IS NULL;`);
|
||||
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_post_headline" ON "post" ("headline") WHERE deleted_at IS NULL;`)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user