Skip to content

Commit

Permalink
feat(medusa): Added models + repo for products in multiple categories (
Browse files Browse the repository at this point in the history
…#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>
  • Loading branch information
riqwan and olivermrbl committed Jan 25, 2023
1 parent d25a531 commit 2e7e16b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-trees-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

feat(medusa): Added models + repo for products in multiple categories
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class productCategoryProduct1674455083104 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
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"`)
}
}
17 changes: 17 additions & 0 deletions packages/medusa/src/models/product-category.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -12,6 +13,8 @@ import {
TreeParent,
TreeLevelColumn,
JoinColumn,
ManyToMany,
JoinTable,
} from "typeorm"

@Entity()
Expand Down Expand Up @@ -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")
Expand Down
15 changes: 15 additions & 0 deletions packages/medusa/src/models/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2e7e16b

Please sign in to comment.