chore: Migrate region module to use DML (#7837)

This commit is contained in:
Oli Juhl
2024-06-29 13:14:52 +00:00
committed by GitHub
parent 7c3f5cc334
commit fa6cd84049
10 changed files with 173 additions and 155 deletions
+25 -41
View File
@@ -1,44 +1,28 @@
import { Entity, ManyToOne, PrimaryKey, Property } from "@mikro-orm/core"
import { createPsqlIndexStatementHelper } from "@medusajs/utils"
import { model } from "@medusajs/utils"
import Region from "./region"
// We don't need a partial index on deleted_at here since we don't support soft deletes on countries
const regionIdIsoIndexName = "IDX_region_country_region_id_iso_2_unique"
const RegionIdIsoIndexStatement = createPsqlIndexStatementHelper({
name: regionIdIsoIndexName,
tableName: "region_country",
columns: ["region_id", "iso_2"],
unique: true,
})
const Country = model
.define(
{ name: "Country", tableName: "region_country" },
{
iso_2: model.text().searchable().primaryKey(),
iso_3: model.text(),
num_code: model.text(),
name: model.text().searchable(),
display_name: model.text(),
region: model
.belongsTo(() => Region, { mappedBy: "countries" })
.nullable(),
metadata: model.json().nullable(),
}
)
.indexes([
{
// TODO: Remove ts-ignore when field inference takes into account the nullable property
// @ts-ignore
on: ["region_id", "iso_2"],
unique: true,
},
])
RegionIdIsoIndexStatement.MikroORMIndex()
@Entity({ tableName: "region_country" })
export default class Country {
@PrimaryKey({ columnType: "text" })
@Property({ columnType: "text" })
iso_2: string
@Property({ columnType: "text" })
iso_3: string
@Property({ columnType: "text" })
num_code: string
@Property({ columnType: "text" })
name: string
@Property({ columnType: "text" })
display_name: string
@Property({ columnType: "text", nullable: true })
region_id?: string | null = null
@ManyToOne({
entity: () => Region,
fieldName: "region_id",
nullable: true,
onDelete: "set null",
})
region?: Region | null
}
export default Country
+2 -1
View File
@@ -1,2 +1,3 @@
export { default as Country } from "./country"
export { default as RegionCountry } from "./country"
export { default as Region } from "./region"
+11 -70
View File
@@ -1,72 +1,13 @@
import { DAL } from "@medusajs/types"
import { DALUtils, Searchable, generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Collection,
Entity,
Filter,
Index,
OnInit,
OneToMany,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import Country from "./country"
import { model } from "@medusajs/utils"
import RegionCountry from "./country"
type RegionOptionalProps = "countries" | DAL.SoftDeletableEntityDateColumns
const Region = model.define("region", {
id: model.id({ prefix: "reg" }),
name: model.text().searchable(),
currency_code: model.text().searchable(),
automatic_taxes: model.boolean().default(true),
countries: model.hasMany(() => RegionCountry),
metadata: model.json().nullable(),
})
@Entity({ tableName: "region" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Region {
[OptionalProps]?: RegionOptionalProps
@PrimaryKey({ columnType: "text" })
id: string
@Searchable()
@Property({ columnType: "text" })
name: string
@Searchable()
@Property({ columnType: "text" })
currency_code: string
@Property({ columnType: "boolean" })
automatic_taxes: boolean = true
@OneToMany(() => Country, (country) => country.region)
countries = new Collection<Country>(this)
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@Index({ name: "IDX_region_deleted_at" })
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "reg")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "reg")
}
}
export default Region