refactor: migrate sales-channel to DML (#10452)

Fixes: FRMW-2830
This commit is contained in:
Harminder Virk
2024-12-05 14:02:08 +00:00
committed by GitHub
parent 0a16efa426
commit be15240909
4 changed files with 20 additions and 72 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/sales-channel": patch
---
refactor: migrate sales-channel to DML
@@ -1,71 +1,11 @@
import {
DALUtils,
Searchable,
generateEntityId,
} from "@medusajs/framework/utils"
import { model } from "@medusajs/framework/utils"
import { DAL } from "@medusajs/framework/types"
import {
BeforeCreate,
Entity,
Filter,
Index,
OnInit,
OptionalProps,
PrimaryKey,
Property,
} from "@mikro-orm/core"
const SalesChannel = model.define("SalesChannel", {
id: model.id({ prefix: "sc" }).primaryKey(),
name: model.text().searchable(),
description: model.text().searchable().nullable(),
is_disabled: model.boolean().default(false),
metadata: model.json().nullable(),
})
type SalesChannelOptionalProps = "is_disabled" | DAL.ModelDateColumns
@Entity()
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class SalesChannel {
[OptionalProps]?: SalesChannelOptionalProps
@PrimaryKey({ columnType: "text" })
id!: string
@Searchable()
@Property({ columnType: "text" })
name!: string
@Searchable()
@Property({ columnType: "text", nullable: true })
description: string | null = null
@Property({ columnType: "boolean", default: false })
is_disabled: boolean = false
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
@Index({ name: "IDX_sales_channel_deleted_at" })
@Property({ columnType: "timestamptz", nullable: true })
deleted_at: Date | null = null
@BeforeCreate()
onCreate() {
this.id = generateEntityId(this.id, "sc")
}
@OnInit()
onInit() {
this.id = generateEntityId(this.id, "sc")
}
}
export default SalesChannel
@@ -3,6 +3,7 @@ import {
CreateSalesChannelDTO,
DAL,
FilterableSalesChannelProps,
InferEntityType,
InternalModuleDeclaration,
ISalesChannelModuleService,
ModuleJoinerConfig,
@@ -36,7 +37,9 @@ export default class SalesChannelModuleService
implements ISalesChannelModuleService
{
protected baseRepository_: DAL.RepositoryService
protected readonly salesChannelService_: ModulesSdkTypes.IMedusaInternalService<SalesChannel>
protected readonly salesChannelService_: ModulesSdkTypes.IMedusaInternalService<
InferEntityType<typeof SalesChannel>
>
constructor(
{ baseRepository, salesChannelService }: InjectedDependencies,
@@ -83,7 +86,7 @@ export default class SalesChannelModuleService
async createSalesChannels_(
data: CreateSalesChannelDTO[],
@MedusaContext() sharedContext: Context
): Promise<SalesChannel[]> {
): Promise<InferEntityType<typeof SalesChannel>[]> {
return await this.salesChannelService_.create(data, sharedContext)
}
@@ -163,7 +166,7 @@ export default class SalesChannelModuleService
(channel): channel is CreateSalesChannelDTO => !channel.id
)
const operations: Promise<SalesChannel[]>[] = []
const operations: Promise<InferEntityType<typeof SalesChannel>[]>[] = []
if (forCreate.length) {
operations.push(this.createSalesChannels_(forCreate, sharedContext))