Feat(medusa) - delete cascade modules associations (#3190)

* delete cascade sales channel x locations, variant x inventory item
This commit is contained in:
Carlos R. L. Rodrigues
2023-02-08 17:23:47 -03:00
committed by GitHub
parent 3b474ec35c
commit d859ccf551
18 changed files with 672 additions and 63 deletions
@@ -1,18 +1,29 @@
import { Index, Unique, BeforeInsert, Column, Entity } from "typeorm"
import { BaseEntity } from "../interfaces/models/base-entity"
import { DbAwareColumn, generateEntityId } from "../utils"
import {
Index,
BeforeInsert,
Column,
Entity,
ManyToOne,
JoinColumn,
} from "typeorm"
import { SoftDeletableEntity } from "../interfaces"
import { generateEntityId } from "../utils"
import { ProductVariant } from "./product-variant"
@Entity()
@Unique(["variant_id", "inventory_item_id"])
export class ProductVariantInventoryItem extends BaseEntity {
export class ProductVariantInventoryItem extends SoftDeletableEntity {
@Index()
@DbAwareColumn({ type: "text" })
@Column({ type: "text" })
inventory_item_id: string
@Index()
@DbAwareColumn({ type: "text" })
@Column({ type: "text" })
variant_id: string
@ManyToOne(() => ProductVariant, (variant) => variant.inventory_items)
@JoinColumn({ name: "variant_id" })
variant: ProductVariant
@Column({ type: "int", default: 1 })
required_quantity: number
@@ -14,6 +14,7 @@ import { Product } from "./product"
import { ProductOptionValue } from "./product-option-value"
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
import { generateEntityId } from "../utils/generate-entity-id"
import { ProductVariantInventoryItem } from "./product-variant-inventory-item"
@Entity()
export class ProductVariant extends SoftDeletableEntity {
@@ -91,6 +92,15 @@ export class ProductVariant extends SoftDeletableEntity {
})
options: ProductOptionValue[]
@OneToMany(
() => ProductVariantInventoryItem,
(inventoryItem) => inventoryItem.variant,
{
cascade: ["soft-remove", "remove"],
}
)
inventory_items: ProductVariantInventoryItem[]
@DbAwareColumn({ type: "jsonb", nullable: true })
metadata: Record<string, unknown>
@@ -199,6 +209,11 @@ export class ProductVariant extends SoftDeletableEntity {
* type: array
* items:
* $ref: "#/components/schemas/ProductOptionValue"
* inventory_items:
* description: The Inventory Items related to the product variant. Available if the relation `inventory_items` is expanded.
* type: array
* items:
* $ref: "#/components/schemas/ProductVariantInventoryItem"
* created_at:
* type: string
* description: "The date with timezone at which the resource was created."
@@ -1,11 +1,12 @@
import { BeforeInsert, Index, Column } from "typeorm"
import { BeforeInsert, Index, Column, ManyToOne, JoinColumn } from "typeorm"
import { FeatureFlagEntity } from "../utils/feature-flag-decorators"
import { BaseEntity } from "../interfaces"
import { SoftDeletableEntity } from "../interfaces"
import { generateEntityId } from "../utils"
import { SalesChannel } from "./sales-channel"
@FeatureFlagEntity("sales_channels")
export class SalesChannelLocation extends BaseEntity {
export class SalesChannelLocation extends SoftDeletableEntity {
@Index()
@Column({ type: "text" })
sales_channel_id: string
@@ -14,8 +15,42 @@ export class SalesChannelLocation extends BaseEntity {
@Column({ type: "text" })
location_id: string
@ManyToOne(() => SalesChannel, (sc) => sc.locations)
@JoinColumn({ name: "sales_channel_id" })
sales_channel: SalesChannel
@BeforeInsert()
private beforeInsert(): void {
this.id = generateEntityId(this.id, "scloc")
}
}
/**
* @schema SalesChannelLocation
* title: "Sales Channel Stock Location"
* description: "Sales Channel Stock Location link sales channels with stock locations."
* type: object
* properties:
* id:
* type: string
* description: The Sales Channel Stock Location's ID
* example: scloc_01G8X9A7ESKAJXG2H0E6F1MW7A
* sales_channel_id:
* description: "The id of the Sales Channel"
* type: string
* location_id:
* description: "The id of the Location Stock."
* type: string
* created_at:
* type: string
* description: "The date with timezone at which the resource was created."
* format: date-time
* updated_at:
* type: string
* description: "The date with timezone at which the resource was updated."
* format: date-time
* deleted_at:
* type: string
* description: "The date with timezone at which the resource was deleted."
* format: date-time
*/
+16 -1
View File
@@ -1,8 +1,9 @@
import { BeforeInsert, Column } from "typeorm"
import { BeforeInsert, Column, OneToMany } from "typeorm"
import { FeatureFlagEntity } from "../utils/feature-flag-decorators"
import { SoftDeletableEntity } from "../interfaces"
import { generateEntityId } from "../utils"
import { SalesChannelLocation } from "./sales-channel-location"
@FeatureFlagEntity("sales_channels")
export class SalesChannel extends SoftDeletableEntity {
@@ -15,6 +16,15 @@ export class SalesChannel extends SoftDeletableEntity {
@Column({ default: false })
is_disabled: boolean
@OneToMany(
() => SalesChannelLocation,
(scLocation) => scLocation.sales_channel,
{
cascade: ["soft-remove", "remove"],
}
)
locations: SalesChannelLocation[]
@BeforeInsert()
private beforeInsert(): void {
this.id = generateEntityId(this.id, "sc")
@@ -45,6 +55,11 @@ export class SalesChannel extends SoftDeletableEntity {
* description: "Specify if the sales channel is enabled or disabled."
* type: boolean
* default: false
* locations:
* description: The Stock Locations related to the sales channel. Available if the relation `locations` is expanded.
* type: array
* items:
* $ref: "#/components/schemas/SalesChannelLocation"
* created_at:
* type: string
* description: "The date with timezone at which the resource was created."