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
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(medusa): Added models + repo for products in multiple categories
|
||||||
@@ -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"`)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { generateEntityId } from "../utils/generate-entity-id"
|
import { generateEntityId } from "../utils/generate-entity-id"
|
||||||
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
|
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
|
||||||
import { kebabCase } from "lodash"
|
import { kebabCase } from "lodash"
|
||||||
|
import { Product } from "."
|
||||||
import {
|
import {
|
||||||
BeforeInsert,
|
BeforeInsert,
|
||||||
Index,
|
Index,
|
||||||
@@ -12,6 +13,8 @@ import {
|
|||||||
TreeParent,
|
TreeParent,
|
||||||
TreeLevelColumn,
|
TreeLevelColumn,
|
||||||
JoinColumn,
|
JoinColumn,
|
||||||
|
ManyToMany,
|
||||||
|
JoinTable,
|
||||||
} from "typeorm"
|
} from "typeorm"
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
@@ -49,6 +52,20 @@ export class ProductCategory extends SoftDeletableEntity {
|
|||||||
@TreeChildren({ cascade: true })
|
@TreeChildren({ cascade: true })
|
||||||
category_children: ProductCategory[]
|
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()
|
@BeforeInsert()
|
||||||
private beforeInsert(): void {
|
private beforeInsert(): void {
|
||||||
this.id = generateEntityId(this.id, "pcat")
|
this.id = generateEntityId(this.id, "pcat")
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import { ProductCollection } from "./product-collection"
|
|||||||
import { ProductOption } from "./product-option"
|
import { ProductOption } from "./product-option"
|
||||||
import { ProductTag } from "./product-tag"
|
import { ProductTag } from "./product-tag"
|
||||||
import { ProductType } from "./product-type"
|
import { ProductType } from "./product-type"
|
||||||
|
import { ProductCategory } from "./product-category"
|
||||||
import { ProductVariant } from "./product-variant"
|
import { ProductVariant } from "./product-variant"
|
||||||
import { SalesChannel } from "./sales-channel"
|
import { SalesChannel } from "./sales-channel"
|
||||||
import { ShippingProfile } from "./shipping-profile"
|
import { ShippingProfile } from "./shipping-profile"
|
||||||
@@ -77,6 +78,20 @@ export class Product extends SoftDeletableEntity {
|
|||||||
})
|
})
|
||||||
variants: ProductVariant[]
|
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()
|
@Index()
|
||||||
@Column()
|
@Column()
|
||||||
profile_id: string
|
profile_id: string
|
||||||
|
|||||||
Reference in new issue
Block a user