#Extend Entity AdminPostReq properties

9 messages · Page 1 of 1 (latest)

crisp grove
#

A bit of explanation. I'm adding is_featured field to ProductCategory

models/product-category.ts

import { Column, Entity, Index, Tree } from "typeorm"
import {
  ProductCategory as MedusaProductCategory,
} from "@medusajs/medusa"

@Entity()
@Tree("materialized-path")
@Index(["parent_category_id", "rank"], { unique: true })
export class ProductCategory extends MedusaProductCategory {
  @Column()
  is_featured: boolean
}

loaders/extend-product-category-fields.ts

export default async function () {
  const storeImports = (await import(
    "@medusajs/medusa/dist/api/routes/store/product-categories/index"
  )) as any
  storeImports.defaultStoreProductCategoryFields = [
    ...storeImports.defaultStoreProductCategoryFields,
    "is_featured",
  ]

  const adminImports = (await import(
    "@medusajs/medusa/dist/api/routes/admin/product-categories/index"
  )) as any
  adminImports.defaultProductCategoryFields = [
    ...adminImports.defaultProductCategoryFields,
    "is_featured",
  ]
}

repositories/product-category.ts

import { 
  dataSource,
} from "@medusajs/medusa/dist/loaders/database"
import {
  ProductCategoryRepository as MedusaProductCategoryRepository,
} from "@medusajs/medusa/dist/repositories/product-category"
import { ProductCategory } from "../models/product-category"

export const ProductCategoryRepository = dataSource
  .getTreeRepository(ProductCategory)
  .extend({
    ...Object.assign(
      MedusaProductCategoryRepository, 
      { target: ProductCategory }
    ),
  })

export default ProductCategoryRepository

index.d.ts

export declare module "@medusajs/medusa/dist/models/product-category" {
  declare interface ProductCategory {
    is_featured: boolean;
  }
}
azure locust
neat raft
#

Additionally, can you help me understand your use-case? Do you think having tags in product categories for this purpose help you achieve what you want?

Context: I built the category feature here and would like to get some feedback to iterate on it

crisp grove
crisp grove
crisp grove
#

@neat raft a little comment. Seems weird that I need to use useProductCategories({parent_category_id: "null"}) instead of useProductCategories({parent_category_id: null}) to get root categories