feat(medusa): Product category, type and tags

This commit is contained in:
Oliver Windall Juhl
2021-02-12 08:42:19 +01:00
committed by GitHub
parent 2a8b556256
commit c4d1203155
45 changed files with 1704 additions and 45 deletions
@@ -0,0 +1,57 @@
import {
Entity,
BeforeInsert,
DeleteDateColumn,
CreateDateColumn,
UpdateDateColumn,
Column,
PrimaryColumn,
ManyToMany,
Index,
OneToMany,
} from "typeorm"
import { ulid } from "ulid"
import { Product } from "./product"
import _ from "lodash"
@Entity()
export class ProductCollection {
@PrimaryColumn()
id: string
@Column()
title: string
@Index({ unique: true })
@Column({ nullable: true })
handle: string
@OneToMany(
() => Product,
product => product.collection
)
products: Product[]
@CreateDateColumn({ type: "timestamptz" })
created_at: Date
@UpdateDateColumn({ type: "timestamptz" })
updated_at: Date
@DeleteDateColumn({ type: "timestamptz" })
deleted_at: Date
@Column({ type: "jsonb", nullable: true })
metadata: any
@BeforeInsert()
private beforeInsert() {
if (this.id) return
const id = ulid()
this.id = `pcol_${id}`
if (!this.handle) {
this.handle = _.kebabCase(this.title)
}
}
}
@@ -29,6 +29,9 @@ export class ProductOption {
)
values: ProductOptionValue
@Column()
product_id: string
@ManyToOne(
() => Product,
product => product.options
+38
View File
@@ -0,0 +1,38 @@
import {
Entity,
BeforeInsert,
DeleteDateColumn,
CreateDateColumn,
UpdateDateColumn,
Column,
PrimaryColumn,
} from "typeorm"
import { ulid } from "ulid"
@Entity()
export class ProductTag {
@PrimaryColumn()
id: string
@Column()
value: string
@CreateDateColumn({ type: "timestamptz" })
created_at: Date
@UpdateDateColumn({ type: "timestamptz" })
updated_at: Date
@DeleteDateColumn({ type: "timestamptz" })
deleted_at: Date
@Column({ type: "jsonb", nullable: true })
metadata: any
@BeforeInsert()
private beforeInsert() {
if (this.id) return
const id = ulid()
this.id = `ptag_${id}`
}
}
@@ -0,0 +1,38 @@
import {
Entity,
BeforeInsert,
DeleteDateColumn,
CreateDateColumn,
UpdateDateColumn,
Column,
PrimaryColumn,
} from "typeorm"
import { ulid } from "ulid"
@Entity()
export class ProductType {
@PrimaryColumn()
id: string
@Column()
value: string
@CreateDateColumn({ type: "timestamptz" })
created_at: Date
@UpdateDateColumn({ type: "timestamptz" })
updated_at: Date
@DeleteDateColumn({ type: "timestamptz" })
deleted_at: Date
@Column({ type: "jsonb", nullable: true })
metadata: any
@BeforeInsert()
private beforeInsert() {
if (this.id) return
const id = ulid()
this.id = `ptyp_${id}`
}
}
+36 -3
View File
@@ -17,9 +17,13 @@ import {
import { ulid } from "ulid"
import { Image } from "./image"
import { ProductCollection } from "./product-collection"
import { ProductOption } from "./product-option"
import { ProductTag } from "./product-tag"
import { ProductType } from "./product-type"
import { ProductVariant } from "./product-variant"
import { ShippingProfile } from "./shipping-profile"
import _ from "lodash"
@Entity()
export class Product {
@@ -35,9 +39,6 @@ export class Product {
@Column({ nullable: true })
description: string
@Column({ nullable: true })
tags: string
@Index({ unique: true })
@Column({ nullable: true })
handle: string
@@ -107,6 +108,34 @@ export class Product {
@Column({ nullable: true })
material: string
@Column({ nullable: true })
collection_id: string
@ManyToOne(() => ProductCollection)
@JoinColumn({ name: "collection_id" })
collection: ProductCollection
@Column({ nullable: true })
type_id: string
@ManyToOne(() => ProductType)
@JoinColumn({ name: "type_id" })
type: ProductType
@ManyToMany(() => ProductTag)
@JoinTable({
name: "product_tags",
joinColumn: {
name: "product_id",
referencedColumnName: "id",
},
inverseJoinColumn: {
name: "product_tag_id",
referencedColumnName: "id",
},
})
tags: ProductTag[]
@CreateDateColumn({ type: "timestamptz" })
created_at: Date
@@ -124,5 +153,9 @@ export class Product {
if (this.id) return
const id = ulid()
this.id = `prod_${id}`
if (!this.handle) {
this.handle = _.kebabCase(this.title)
}
}
}