From 2e7e16b9173e2779946776b9b07ce7232c683f36 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Wed, 25 Jan 2023 07:24:39 +0100 Subject: [PATCH] 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> --- .changeset/rare-trees-sin.md | 5 +++ .../1674455083104-product_category_product.ts | 45 +++++++++++++++++++ .../medusa/src/models/product-category.ts | 17 +++++++ packages/medusa/src/models/product.ts | 15 +++++++ 4 files changed, 82 insertions(+) create mode 100644 .changeset/rare-trees-sin.md create mode 100644 packages/medusa/src/migrations/1674455083104-product_category_product.ts diff --git a/.changeset/rare-trees-sin.md b/.changeset/rare-trees-sin.md new file mode 100644 index 0000000000..dadfaa1e1d --- /dev/null +++ b/.changeset/rare-trees-sin.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +feat(medusa): Added models + repo for products in multiple categories diff --git a/packages/medusa/src/migrations/1674455083104-product_category_product.ts b/packages/medusa/src/migrations/1674455083104-product_category_product.ts new file mode 100644 index 0000000000..2227cecb6c --- /dev/null +++ b/packages/medusa/src/migrations/1674455083104-product_category_product.ts @@ -0,0 +1,45 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class productCategoryProduct1674455083104 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + ` + CREATE TABLE "product_category_product" ( + "product_category_id" character varying NOT NULL, + "product_id" character varying NOT NULL, + CONSTRAINT "FK_product_category_id" FOREIGN KEY ("product_category_id") REFERENCES product_category("id") ON DELETE CASCADE ON UPDATE NO ACTION, + CONSTRAINT "FK_product_id" FOREIGN KEY ("product_id") REFERENCES product("id") ON DELETE CASCADE ON UPDATE NO ACTION + ) + ` + ) + + await queryRunner.query( + ` + CREATE UNIQUE INDEX "IDX_upcp_product_id_product_category_id" + ON "product_category_product" ("product_category_id", "product_id") + ` + ) + + await queryRunner.query( + ` + CREATE INDEX "IDX_pcp_product_category_id" + ON "product_category_product" ("product_category_id") + ` + ) + + await queryRunner.query( + ` + CREATE INDEX "IDX_pcp_product_id" + ON "product_category_product" ("product_id") + ` + ) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX "IDX_upcp_product_id_product_category_id"`) + await queryRunner.query(`DROP INDEX "IDX_pcp_product_category_id"`) + await queryRunner.query(`DROP INDEX "IDX_pcp_product_id"`) + + await queryRunner.query(`DROP TABLE "product_category_product"`) + } +} diff --git a/packages/medusa/src/models/product-category.ts b/packages/medusa/src/models/product-category.ts index d06aaa8847..b42f6c7863 100644 --- a/packages/medusa/src/models/product-category.ts +++ b/packages/medusa/src/models/product-category.ts @@ -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") diff --git a/packages/medusa/src/models/product.ts b/packages/medusa/src/models/product.ts index be26b32791..42ef75f03f 100644 --- a/packages/medusa/src/models/product.ts +++ b/packages/medusa/src/models/product.ts @@ -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