https://docs.medusajs.com/development/entities/extend-entity
In documentation it only shows how it's possible to extend allowed fields for listing. Was wondering how and if it's possible to add these custom fields for Update/Create requests. Currently I'm getting Error: property * should not exist error when trying to update custom field
#Extend Entity AdminPostReq properties
9 messages · Page 1 of 1 (latest)
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;
}
}
Hi maybe this will help you : https://medium.com/@adevinwild/extend-validators-with-medusa-js-1-8-x-and-up-3e730040fd53 🙌🏼
@crisp grove - you have to extend this class to include your field for create - https://github.com/medusajs/medusa/blob/develop/packages/medusa/src/api/routes/admin/product-categories/create-product-category.ts#L123
and this one for update - https://github.com/medusajs/medusa/blob/develop/packages/medusa/src/api/routes/admin/product-categories/update-product-category.ts#L126
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
This helped. Would be nice if you specified path for extender validator
The article @azure locust provided worked and will be usefull for any other custom fields I want to add.
Basically I want to filter categories to get only featured and display them in header menu bar. Might actually abandon this idea and just fetch root categories and display all of them in header with ... option if there are a lot.
@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