refactor: migrate store module to DML (#10467)

This commit is contained in:
Harminder Virk
2024-12-06 17:38:15 +05:30
committed by GitHub
parent 7e04091b49
commit f3c91c908a
6 changed files with 79 additions and 175 deletions

View File

@@ -1,81 +1,15 @@
import {
DALUtils,
Searchable,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/framework/utils"
import {
BeforeCreate,
Entity,
OnInit,
PrimaryKey,
Property,
Filter,
ManyToOne,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import Store from "./store"
const StoreCurrencyDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "store_currency",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
const StoreCurrency = model.define("StoreCurrency", {
id: model.id({ prefix: "stocur" }).primaryKey(),
currency_code: model.text().searchable(),
is_default: model.boolean().default(false),
store: model
.belongsTo(() => Store, {
mappedBy: "supported_currencies",
})
.nullable(),
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class StoreCurrency {
@PrimaryKey({ columnType: "text" })
id: string
@Searchable()
@Property({ columnType: "text" })
currency_code: string
@Property({ columnType: "boolean", default: false })
is_default?: boolean
@ManyToOne(() => Store, {
columnType: "text",
fieldName: "store_id",
mapToPk: true,
nullable: true,
onDelete: "cascade",
})
store_id: string | null
@ManyToOne(() => Store, {
persist: false,
nullable: true,
})
store: Store | 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
@StoreCurrencyDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "stocur")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "stocur")
}
}
export default StoreCurrency

View File

@@ -1,89 +1,20 @@
import {
DALUtils,
Searchable,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/framework/utils"
import { DAL } from "@medusajs/framework/types"
import {
BeforeCreate,
Entity,
OnInit,
PrimaryKey,
Property,
Filter,
OptionalProps,
OneToMany,
Collection,
Cascade,
} from "@mikro-orm/core"
import { model } from "@medusajs/framework/utils"
import StoreCurrency from "./currency"
type StoreOptionalProps = DAL.SoftDeletableModelDateColumns
const StoreDeletedAtIndex = createPsqlIndexStatementHelper({
tableName: "store",
columns: "deleted_at",
where: "deleted_at IS NOT NULL",
})
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class Store {
[OptionalProps]?: StoreOptionalProps
@PrimaryKey({ columnType: "text" })
id: string
@Searchable()
@Property({ columnType: "text", default: "Medusa Store" })
name: string
@OneToMany(() => StoreCurrency, (o) => o.store, {
cascade: [Cascade.PERSIST, "soft-remove"] as any,
const Store = model
.define("Store", {
id: model.id({ prefix: "store" }).primaryKey(),
name: model.text().default("Medusa Store").searchable(),
default_sales_channel_id: model.text().nullable(),
default_region_id: model.text().nullable(),
default_location_id: model.text().nullable(),
metadata: model.json().nullable(),
supported_currencies: model.hasMany(() => StoreCurrency, {
mappedBy: "store",
}),
})
supported_currencies = new Collection<StoreCurrency>(this)
@Property({ columnType: "text", nullable: true })
default_sales_channel_id: string | null = null
@Property({ columnType: "text", nullable: true })
default_region_id: string | null = null
@Property({ columnType: "text", nullable: true })
default_location_id: string | null = null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
.cascades({
delete: ["supported_currencies"],
})
created_at: Date
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@StoreDeletedAtIndex.MikroORMIndex()
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "store")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "store")
}
}
export default Store