#Supported only in tree entities (extending ProductCategory)

2 messages · Page 1 of 1 (latest)

dull pine
#

ProductCategory Entity

import {
Column,
Entity,
JoinColumn,
JoinTable,
ManyToMany,
TreeChildren,
TreeParent,
} from "typeorm";
import { ProductCategory as MedusaProductCategory } from "@medusajs/medusa";
import { Inspiration } from "./inspiration";

@Entity()
export class ProductCategory extends MedusaProductCategory {
static productCategoryInspirationJoinTable = "product_category_inspiration";

@TreeParent()
@JoinColumn({ name: "parent_category_id" })
parent_category: ProductCategory | null;

@TreeChildren({ cascade: true })
category_children: ProductCategory[];

@ManyToMany(() => Inspiration, { cascade: ["remove", "soft-remove"] })
@JoinTable({
name: ProductCategory.productCategoryInspirationJoinTable,
joinColumn: {
name: "product_category_id",
referencedColumnName: "id",
},
inverseJoinColumn: {
name: "inspiration_id",
referencedColumnName: "id",
},
})
inspirations: Inspiration[];
}

#

@Entity()
export class Inspiration extends SoftDeletableEntity {
@Column()
title: string;

@Column({ type: "text", nullable: true })
subtitle: string | null;

@Column({ type: "text", nullable: true })
description: string | null;

@Column({ type: "text", nullable: true })
author: string | null;

@Index({ unique: true, where: "deleted_at IS NULL" })
@Column({ type: "text", nullable: true })
handle?: string | null;

@DbAwareColumn({ type: "enum", enum: InspirationStatus, default: "draft" })
status: InspirationStatus;

@DbAwareColumn({ type: "enum", enum: InspirationStyles, default: "none" })
styles: InspirationStyles;

@ManyToMany(() => Image, (image) => image, { cascade: ["insert"] })
@JoinTable({
name: "inspiration_images",
joinColumn: {
name: "inspiration_id",
referencedColumnName: "id",
},
inverseJoinColumn: {
name: "image_id",
referencedColumnName: "id",
},
})
images: Image[] | string[] | null;

@Column({ type: "text", nullable: true })
thumbnail: string | Image | null;

@ManyToMany(() => Product, { cascade: ["remove", "soft-remove"] })
@JoinTable({
name: "inspiration_products",
joinColumn: {
name: "inspiration_id",
referencedColumnName: "id",
},
inverseJoinColumn: {
name: "product_id",
referencedColumnName: "id",
},
})
products?: Product[];

@ManyToMany(() => ProductCategory, { cascade: ["remove", "soft-remove"] })
@JoinTable({
name: "product_category_inspiration",
joinColumn: {
name: "inspiration_id",
referencedColumnName: "id",
},
inverseJoinColumn: {
name: "product_category_id",
referencedColumnName: "id",
},
})
categories: ProductCategory[] | string[];

@BeforeInsert()
private beforeInsert(): void {
if (this.id) return;

this.id = generateEntityId(this.id, "insp");
if (!this.handle) {
  this.handle = _.kebabCase(this.title);
}

}
}