feat(medusa): Added models + repo for products in multiple categories (#3083)
* chore: Added models + repo for products in multiple categories * chore: remove join table model + repo * chore: add foreign key constraints * Update packages/medusa/src/migrations/1674455083104-product_category_product.ts Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> * chore: cascade on delete Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
1 parent
d25a531045
commit
2e7e16b917
4 files changed
+82
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { generateEntityId } from "../utils/generate-entity-id"
|
||||
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
|
||||
import { kebabCase } from "lodash"
|
||||
import { Product } from "."
|
||||
import {
|
||||
BeforeInsert,
|
||||
Index,
|
||||
@@ -12,6 +13,8 @@ import {
|
||||
TreeParent,
|
||||
TreeLevelColumn,
|
||||
JoinColumn,
|
||||
ManyToMany,
|
||||
JoinTable,
|
||||
} from "typeorm"
|
||||
|
||||
@Entity()
|
||||
@@ -49,6 +52,20 @@ export class ProductCategory extends SoftDeletableEntity {
|
||||
@TreeChildren({ cascade: true })
|
||||
category_children: ProductCategory[]
|
||||
|
||||
@ManyToMany(() => Product, { cascade: ["remove", "soft-remove"] })
|
||||
@JoinTable({
|
||||
name: "product_category_product",
|
||||
joinColumn: {
|
||||
name: "product_id",
|
||||
referencedColumnName: "id",
|
||||
},
|
||||
inverseJoinColumn: {
|
||||
name: "product_category_id",
|
||||
referencedColumnName: "id",
|
||||
},
|
||||
})
|
||||
products: Product[]
|
||||
|
||||
@BeforeInsert()
|
||||
private beforeInsert(): void {
|
||||
this.id = generateEntityId(this.id, "pcat")
|
||||
|
||||
@@ -17,6 +17,7 @@ import { ProductCollection } from "./product-collection"
|
||||
import { ProductOption } from "./product-option"
|
||||
import { ProductTag } from "./product-tag"
|
||||
import { ProductType } from "./product-type"
|
||||
import { ProductCategory } from "./product-category"
|
||||
import { ProductVariant } from "./product-variant"
|
||||
import { SalesChannel } from "./sales-channel"
|
||||
import { ShippingProfile } from "./shipping-profile"
|
||||
@@ -77,6 +78,20 @@ export class Product extends SoftDeletableEntity {
|
||||
})
|
||||
variants: ProductVariant[]
|
||||
|
||||
@ManyToMany(() => ProductCategory, { cascade: ["remove", "soft-remove"] })
|
||||
@JoinTable({
|
||||
name: "product_category_product",
|
||||
joinColumn: {
|
||||
name: "product_category_id",
|
||||
referencedColumnName: "id",
|
||||
},
|
||||
inverseJoinColumn: {
|
||||
name: "product_id",
|
||||
referencedColumnName: "id",
|
||||
},
|
||||
})
|
||||
categories: ProductCategory[]
|
||||
|
||||
@Index()
|
||||
@Column()
|
||||
profile_id: string
|
||||
|
||||
Reference in new issue
Block a user