feat(region): Add migration (#6320)
This commit is contained in:
@@ -63,11 +63,7 @@ describe("Region Module Service", () => {
|
|||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: createdRegion.id,
|
id: createdRegion.id,
|
||||||
name: "Europe",
|
name: "Europe",
|
||||||
currency_code: "EUR",
|
currency_code: "eur",
|
||||||
currency: expect.objectContaining({
|
|
||||||
code: "eur",
|
|
||||||
name: "Euro",
|
|
||||||
}),
|
|
||||||
countries: [],
|
countries: [],
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@@ -80,7 +76,7 @@ describe("Region Module Service", () => {
|
|||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: region.id,
|
id: region.id,
|
||||||
name: "Europe",
|
name: "Europe",
|
||||||
currency_code: "EUR",
|
currency_code: "eur",
|
||||||
currency: expect.objectContaining({
|
currency: expect.objectContaining({
|
||||||
code: "eur",
|
code: "eur",
|
||||||
name: "Euro",
|
name: "Euro",
|
||||||
|
|||||||
@@ -0,0 +1,287 @@
|
|||||||
|
{
|
||||||
|
"namespaces": [
|
||||||
|
"public"
|
||||||
|
],
|
||||||
|
"name": "public",
|
||||||
|
"tables": [
|
||||||
|
{
|
||||||
|
"columns": {
|
||||||
|
"code": {
|
||||||
|
"name": "code",
|
||||||
|
"type": "text",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "text"
|
||||||
|
},
|
||||||
|
"symbol": {
|
||||||
|
"name": "symbol",
|
||||||
|
"type": "text",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "text"
|
||||||
|
},
|
||||||
|
"symbol_native": {
|
||||||
|
"name": "symbol_native",
|
||||||
|
"type": "text",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "text"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"name": "name",
|
||||||
|
"type": "text",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "text"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"name": "region_currency",
|
||||||
|
"schema": "public",
|
||||||
|
"indexes": [
|
||||||
|
{
|
||||||
|
"keyName": "region_currency_pkey",
|
||||||
|
"columnNames": [
|
||||||
|
"code"
|
||||||
|
],
|
||||||
|
"composite": false,
|
||||||
|
"primary": true,
|
||||||
|
"unique": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"checks": [],
|
||||||
|
"foreignKeys": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "text",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "text"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"name": "name",
|
||||||
|
"type": "text",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "text"
|
||||||
|
},
|
||||||
|
"currency_code": {
|
||||||
|
"name": "currency_code",
|
||||||
|
"type": "text",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "text"
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"name": "metadata",
|
||||||
|
"type": "jsonb",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": true,
|
||||||
|
"mappedType": "json"
|
||||||
|
},
|
||||||
|
"created_at": {
|
||||||
|
"name": "created_at",
|
||||||
|
"type": "timestamptz",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"length": 6,
|
||||||
|
"default": "now()",
|
||||||
|
"mappedType": "datetime"
|
||||||
|
},
|
||||||
|
"updated_at": {
|
||||||
|
"name": "updated_at",
|
||||||
|
"type": "timestamptz",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"length": 6,
|
||||||
|
"default": "now()",
|
||||||
|
"mappedType": "datetime"
|
||||||
|
},
|
||||||
|
"deleted_at": {
|
||||||
|
"name": "deleted_at",
|
||||||
|
"type": "timestamptz",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": true,
|
||||||
|
"length": 6,
|
||||||
|
"mappedType": "datetime"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"name": "region",
|
||||||
|
"schema": "public",
|
||||||
|
"indexes": [
|
||||||
|
{
|
||||||
|
"columnNames": [
|
||||||
|
"currency_id"
|
||||||
|
],
|
||||||
|
"composite": false,
|
||||||
|
"keyName": "IDX_region_currency_code",
|
||||||
|
"primary": false,
|
||||||
|
"unique": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"columnNames": [
|
||||||
|
"deleted_at"
|
||||||
|
],
|
||||||
|
"composite": false,
|
||||||
|
"keyName": "IDX_region_deleted_at",
|
||||||
|
"primary": false,
|
||||||
|
"unique": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"keyName": "region_pkey",
|
||||||
|
"columnNames": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"composite": false,
|
||||||
|
"primary": true,
|
||||||
|
"unique": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"checks": [],
|
||||||
|
"foreignKeys": {
|
||||||
|
"region_currency_id_foreign": {
|
||||||
|
"constraintName": "region_currency_code_foreign",
|
||||||
|
"columnNames": [
|
||||||
|
"currency_code"
|
||||||
|
],
|
||||||
|
"localTableName": "public.region",
|
||||||
|
"referencedColumnNames": [
|
||||||
|
"code"
|
||||||
|
],
|
||||||
|
"referencedTableName": "public.region_currency",
|
||||||
|
"deleteRule": "cascade",
|
||||||
|
"updateRule": "cascade"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"columns": {
|
||||||
|
"id": {
|
||||||
|
"name": "id",
|
||||||
|
"type": "text",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "text"
|
||||||
|
},
|
||||||
|
"iso_2": {
|
||||||
|
"name": "iso_2",
|
||||||
|
"type": "text",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "text"
|
||||||
|
},
|
||||||
|
"iso_3": {
|
||||||
|
"name": "iso_3",
|
||||||
|
"type": "text",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "text"
|
||||||
|
},
|
||||||
|
"num_code": {
|
||||||
|
"name": "num_code",
|
||||||
|
"type": "int",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "integer"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"name": "name",
|
||||||
|
"type": "text",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "text"
|
||||||
|
},
|
||||||
|
"display_name": {
|
||||||
|
"name": "display_name",
|
||||||
|
"type": "text",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": false,
|
||||||
|
"mappedType": "text"
|
||||||
|
},
|
||||||
|
"region_id": {
|
||||||
|
"name": "region_id",
|
||||||
|
"type": "text",
|
||||||
|
"unsigned": false,
|
||||||
|
"autoincrement": false,
|
||||||
|
"primary": false,
|
||||||
|
"nullable": true,
|
||||||
|
"mappedType": "text"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"name": "region_country",
|
||||||
|
"schema": "public",
|
||||||
|
"indexes": [
|
||||||
|
{
|
||||||
|
"columnNames": [
|
||||||
|
"region_id"
|
||||||
|
],
|
||||||
|
"composite": false,
|
||||||
|
"keyName": "IDX_country_region_id",
|
||||||
|
"primary": false,
|
||||||
|
"unique": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"keyName": "region_country_pkey",
|
||||||
|
"columnNames": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"composite": false,
|
||||||
|
"primary": true,
|
||||||
|
"unique": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"checks": [],
|
||||||
|
"foreignKeys": {
|
||||||
|
"region_country_region_id_foreign": {
|
||||||
|
"constraintName": "region_country_region_id_foreign",
|
||||||
|
"columnNames": [
|
||||||
|
"region_id"
|
||||||
|
],
|
||||||
|
"localTableName": "public.region_country",
|
||||||
|
"referencedColumnNames": [
|
||||||
|
"id"
|
||||||
|
],
|
||||||
|
"referencedTableName": "public.region",
|
||||||
|
"deleteRule": "cascade",
|
||||||
|
"updateRule": "cascade"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { Migration } from "@mikro-orm/migrations"
|
||||||
|
|
||||||
|
export class RegionModuleSetup20240205173216 extends Migration {
|
||||||
|
async up(): Promise<void> {
|
||||||
|
this.addSql(`
|
||||||
|
-- Create or update "region" table
|
||||||
|
CREATE TABLE IF NOT EXISTS "region" (
|
||||||
|
"id" text NOT NULL,
|
||||||
|
"name" text NOT NULL,
|
||||||
|
"currency_code" text NOT NULL,
|
||||||
|
"metadata" jsonb NULL,
|
||||||
|
"created_at" timestamptz NOT NULL DEFAULT now(),
|
||||||
|
"updated_at" timestamptz NOT NULL DEFAULT now(),
|
||||||
|
"deleted_at" timestamptz NULL,
|
||||||
|
CONSTRAINT "region_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Create or update "region_currency" table
|
||||||
|
CREATE TABLE IF NOT EXISTS "region_currency" (
|
||||||
|
"code" text NOT NULL,
|
||||||
|
"symbol" text NOT NULL,
|
||||||
|
"symbol_native" text NOT NULL,
|
||||||
|
"name" text NOT NULL,
|
||||||
|
CONSTRAINT "region_currency_pkey" PRIMARY KEY ("code")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Adjust "region" table
|
||||||
|
ALTER TABLE "region" DROP CONSTRAINT IF EXISTS "FK_3bdd5896ec93be2f1c62a3309a5";
|
||||||
|
ALTER TABLE "region" DROP CONSTRAINT IF EXISTS "FK_91f88052197680f9790272aaf5b";
|
||||||
|
ALTER TABLE "region" ADD CONSTRAINT "region_currency_code_foreign" FOREIGN KEY ("currency_code") REFERENCES "region_currency" ("code") ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS "IDX_region_currency_code" ON "region" ("currency_code");
|
||||||
|
CREATE INDEX IF NOT EXISTS "IDX_region_deleted_at" ON "region" ("deleted_at") WHERE "deleted_at" IS NOT NULL;
|
||||||
|
|
||||||
|
-- Create or update "region_country" table
|
||||||
|
CREATE TABLE IF NOT EXISTS "region_country" (
|
||||||
|
"id" text NOT NULL,
|
||||||
|
"iso_2" text NOT NULL,
|
||||||
|
"iso_3" text NOT NULL,
|
||||||
|
"num_code" int NOT NULL,
|
||||||
|
"name" text NOT NULL,
|
||||||
|
"display_name" text NOT NULL,
|
||||||
|
"region_id" text NULL,
|
||||||
|
CONSTRAINT "region_country_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS "IDX_country_region_id" ON "region_country" ("region_id");
|
||||||
|
|
||||||
|
-- Adjust foreign keys for "region_country"
|
||||||
|
ALTER TABLE "region_country" DROP CONSTRAINT IF EXISTS "FK_91f88052197680f9790272aaf5b";
|
||||||
|
ALTER TABLE "region_country" ADD CONSTRAINT "region_country_region_id_foreign" FOREIGN KEY ("region_id") REFERENCES "region" ("id") ON UPDATE CASCADE;
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
BeforeCreate,
|
BeforeCreate,
|
||||||
Cascade,
|
|
||||||
Entity,
|
Entity,
|
||||||
ManyToOne,
|
ManyToOne,
|
||||||
OnInit,
|
OnInit,
|
||||||
@@ -31,16 +30,13 @@ export default class Country {
|
|||||||
@Property({ columnType: "text" })
|
@Property({ columnType: "text" })
|
||||||
display_name: string
|
display_name: string
|
||||||
|
|
||||||
@Property({ columnType: "text", nullable: true })
|
|
||||||
region_id: string | null = null
|
|
||||||
|
|
||||||
@ManyToOne({
|
@ManyToOne({
|
||||||
entity: () => Region,
|
entity: () => Region,
|
||||||
onDelete: "cascade",
|
fieldName: "region_id",
|
||||||
index: "IDX_country_region_id",
|
index: "IDX_country_region_id",
|
||||||
cascade: [Cascade.REMOVE, Cascade.PERSIST],
|
nullable: true,
|
||||||
})
|
})
|
||||||
region: Region
|
region: Region | null = null
|
||||||
|
|
||||||
@BeforeCreate()
|
@BeforeCreate()
|
||||||
onCreate() {
|
onCreate() {
|
||||||
|
|||||||
@@ -1,18 +1,8 @@
|
|||||||
import { generateEntityId } from "@medusajs/utils"
|
import { Entity, PrimaryKey, Property } from "@mikro-orm/core"
|
||||||
import {
|
|
||||||
BeforeCreate,
|
|
||||||
Entity,
|
|
||||||
OnInit,
|
|
||||||
PrimaryKey,
|
|
||||||
Property,
|
|
||||||
} from "@mikro-orm/core"
|
|
||||||
|
|
||||||
@Entity({ tableName: "region_currency" })
|
@Entity({ tableName: "region_currency" })
|
||||||
export default class Currency {
|
export default class Currency {
|
||||||
@PrimaryKey({ columnType: "text" })
|
@PrimaryKey({ columnType: "text" })
|
||||||
id: string
|
|
||||||
|
|
||||||
@Property({ columnType: "text" })
|
|
||||||
code: string
|
code: string
|
||||||
|
|
||||||
@Property({ columnType: "text" })
|
@Property({ columnType: "text" })
|
||||||
@@ -23,14 +13,4 @@ export default class Currency {
|
|||||||
|
|
||||||
@Property({ columnType: "text" })
|
@Property({ columnType: "text" })
|
||||||
name: string
|
name: string
|
||||||
|
|
||||||
@BeforeCreate()
|
|
||||||
onCreate() {
|
|
||||||
this.id = generateEntityId(this.id, "reg_curr")
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnInit()
|
|
||||||
onInit() {
|
|
||||||
this.id = generateEntityId(this.id, "reg_curr")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { DAL } from "@medusajs/types"
|
|||||||
import { DALUtils, generateEntityId } from "@medusajs/utils"
|
import { DALUtils, generateEntityId } from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
BeforeCreate,
|
BeforeCreate,
|
||||||
Cascade,
|
|
||||||
Collection,
|
Collection,
|
||||||
Entity,
|
Entity,
|
||||||
Filter,
|
Filter,
|
||||||
@@ -37,15 +36,12 @@ export default class Region {
|
|||||||
|
|
||||||
@ManyToOne({
|
@ManyToOne({
|
||||||
entity: () => Currency,
|
entity: () => Currency,
|
||||||
onDelete: "cascade",
|
|
||||||
index: "IDX_region_currency_code",
|
index: "IDX_region_currency_code",
|
||||||
cascade: [Cascade.PERSIST],
|
nullable: true,
|
||||||
})
|
})
|
||||||
currency: Currency
|
currency?: Currency
|
||||||
|
|
||||||
@OneToMany(() => Country, (country) => country.region, {
|
@OneToMany(() => Country, (country) => country.region)
|
||||||
cascade: [Cascade.REMOVE],
|
|
||||||
})
|
|
||||||
countries = new Collection<Country>(this)
|
countries = new Collection<Country>(this)
|
||||||
|
|
||||||
@Property({ columnType: "jsonb", nullable: true })
|
@Property({ columnType: "jsonb", nullable: true })
|
||||||
|
|||||||
@@ -100,10 +100,7 @@ export default class RegionModuleService<
|
|||||||
const result = await this.create_(input, sharedContext)
|
const result = await this.create_(input, sharedContext)
|
||||||
|
|
||||||
return await this.baseRepository_.serialize<RegionDTO[]>(
|
return await this.baseRepository_.serialize<RegionDTO[]>(
|
||||||
Array.isArray(data) ? result : result[0],
|
Array.isArray(data) ? result : result[0]
|
||||||
{
|
|
||||||
populate: true,
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,7 +125,7 @@ export default class RegionModuleService<
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
reg.currency = currencyMap.get(lowerCasedCurrency) as RegionCurrencyDTO
|
reg.currency_code = lowerCasedCurrency
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await this.regionService_.create(data, sharedContext)
|
const result = await this.regionService_.create(data, sharedContext)
|
||||||
@@ -207,7 +204,7 @@ export default class RegionModuleService<
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const [currency] = await this.currencyService_.list(
|
const [currency] = await this.currencyService_.list(
|
||||||
{},
|
{},
|
||||||
{ select: ["id"], take: 1 },
|
{ select: ["code"], take: 1 },
|
||||||
sharedContext
|
sharedContext
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ export interface RegionCountryDTO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface RegionCurrencyDTO {
|
export interface RegionCurrencyDTO {
|
||||||
id: string
|
|
||||||
code: string
|
code: string
|
||||||
symbol: string
|
symbol: string
|
||||||
name: string
|
name: string
|
||||||
|
|||||||
Reference in New Issue
Block a user