diff --git a/src/models/blog-category.ts b/src/models/blog-category.ts index 03fb2e3..b6433ce 100644 --- a/src/models/blog-category.ts +++ b/src/models/blog-category.ts @@ -1,14 +1,28 @@ -import { Product, Customer } from "@medusajs/medusa" import { BaseEntity, generateEntityId } from "@medusajs/utils" -import { BeforeInsert, Column, Entity, Index, JoinColumn, ManyToOne, OneToMany } from "typeorm" +import { BeforeInsert, Column, Entity, Index, OneToMany } from "typeorm" +import { BlogPost } from "./blog-post" @Entity() export class BlogCategory extends BaseEntity { + @Index({ unique: true }) + @Column({ type: "varchar", nullable: true }) + handle: string | null + @Column({ type: "varchar", nullable: false }) + title: string + @Column({ type: "text", nullable: true }) + description: string | null + @Column({ type: "text", nullable: true, array: true }) + keywords: string | null + @Column({ type: "jsonb", nullable: true }) + metadata: Record | null + + @OneToMany(() => BlogPost, post => post.category) + posts: BlogPost[] @BeforeInsert() private beforeInsert(): void { diff --git a/src/models/blog-post.ts b/src/models/blog-post.ts index 74714c1..872d847 100644 --- a/src/models/blog-post.ts +++ b/src/models/blog-post.ts @@ -1,6 +1,6 @@ -import { Product, Customer } from "@medusajs/medusa" +import { Product, ProductCollection } from "@medusajs/medusa" import { BaseEntity, generateEntityId } from "@medusajs/utils" -import { BeforeInsert, Column, Entity, Index, JoinColumn, ManyToOne, OneToMany } from "typeorm" +import { BeforeInsert, Column, Entity, Index, JoinColumn, ManyToOne, ManyToMany, JoinTable } from "typeorm" import { BlogCategory } from "./blog-category" import { BlogTag } from "./blog-tag" @@ -8,49 +8,78 @@ import { BlogTag } from "./blog-tag" export class BlogPost extends BaseEntity { @Index({ unique: true }) - @Column({ type: "text", nullable: true }) + @Column({ type: "varchar", nullable: true }) handle: string | null - @Column() + @Column({ type: "varchar", nullable: false }) title: string + @Column({ type: "varchar", nullable: true }) + author: string | null + + @Column({ type: "boolean", nullable: false }) + published: boolean + @Column({ type: "text", nullable: true }) content: string | null - - @Column({ type: "text", nullable: true }) - author: string | null - + @Column({ type: "text", nullable: true }) description: string | null @Column({ type: "text", nullable: true, array: true }) keywords: string | null - @Column({ type: "jsonb", nullable: true }) - tags: string | null + @Column({ type: "varchar", nullable: false }) + category_id: string @ManyToOne(() => BlogCategory) @JoinColumn({ name: "category_id" }) category: BlogCategory + + @ManyToMany(() => BlogTag) + @JoinTable({ + name: "blog_tags", + joinColumn: { + name: "post_id", + referencedColumnName: "id", + }, + inverseJoinColumn: { + name: "tag_id", + referencedColumnName: "id", + }, + }) + tags: BlogTag[] - @Column({ type: "boolean", nullable: false }) - published: boolean - - - - @ManyToOne(() => BlogCategory) - @JoinColumn({ name: "product_id" }) - product: Product - - @Column({ type: "varchar", nullable: false }) - customer_id: string - - @ManyToOne(() => Product) - @JoinColumn({ name: "customer_id" }) - customer: Customer - + @ManyToMany(() => Product) + @JoinTable({ + name: "blog_product_tags", + joinColumn: { + name: "post_id", + referencedColumnName: "id", + }, + inverseJoinColumn: { + name: "product_id", + referencedColumnName: "id", + }, + }) + products: Product[] + @ManyToMany(() => ProductCollection) + @JoinTable({ + name: "blog_collection_tags", + joinColumn: { + name: "post_id", + referencedColumnName: "id", + }, + inverseJoinColumn: { + name: "collection_id", + referencedColumnName: "id", + }, + }) + collections: ProductCollection[] + @Column({ type: "jsonb", nullable: true }) + metadata: Record | null @BeforeInsert() private beforeInsert(): void { diff --git a/src/models/blog-tag.ts b/src/models/blog-tag.ts index 39d3f7f..8c90906 100644 --- a/src/models/blog-tag.ts +++ b/src/models/blog-tag.ts @@ -1,14 +1,11 @@ -import { Product, Customer } from "@medusajs/medusa" import { BaseEntity, generateEntityId } from "@medusajs/utils" -import { BeforeInsert, Column, Entity, Index, JoinColumn, ManyToOne, OneToMany } from "typeorm" +import { BeforeInsert, Column, Entity } from "typeorm" @Entity() export class BlogTag extends BaseEntity { - - - - + @Column({ type: "varchar", nullable: false }) + title: string @BeforeInsert() private beforeInsert(): void {