Implement base entities manipulation #3965
Replies: 8 comments 25 replies
-
Same here 🍿 |
Beta Was this translation helpful? Give feedback.
-
This discussion is something that many people encounters. I ve provided a way of achieving that by following the steps here: // src/utils/register-extended-validator.ts
import { ValidatorOptions } from 'class-validator'
import { ClassConstructor } from "@medusajs/medusa";
const extendedValidators: any = []
let isInitialized = false
export async function registerExtendedValidator<T extends ClassConstructor<any>>(classValidator: T): Promise<void> {
extendedValidators.push(classValidator)
if (isInitialized) {
return
}
isInitialized = true
const module = await import('@medusajs/medusa/dist/utils/validator')
const originalValidator = module.validator
module.validator = <T extends ClassConstructor<any> = any, V = any>(typedClass: T, plain: V, config?: ValidatorOptions): Promise<any> => {
for (const extendedValidator of extendedValidators) {
if (extendedValidator.name === typedClass.name) {
typedClass = extendedValidator
break
}
}
return originalValidator(typedClass, plain, config)
}
} Then in a custom loader, you can register your custom extended validators. A custom validator would look like the following: import { StoreGetProductsParams as MedusaStoreGetProductsParams } from "@medusajs/medusa";
// It is important that the class has the same name as the one you are extending so here StoreGetProductsParams
export class StoreGetProductsParams extends MedusaStoreGetProductsParams {
@IsString()
my_new_prop_to_accept
} Now the end point will accept the new property. In regards to the store domain, there is some restriction on the fields that can be return in order to prevent any security issue. In order to override this default behaviour and alter the allowed props etc you can do the following in your custom loader export default async function () {
const imports = (await import("@medusajs/medusa/dist/api/routes/store/products/index")) as any
const originalValue = imports.allowedStoreProductsFields
imports.allowedStoreProductsFields = [...imports.allowedStoreProductsFields, "title_en"]
const originalValue = imports.defaultStoreProductsFields
imports.defaultStoreProductsFields = [...imports.defaultStoreProductsFields, "title_en"]
} and the above will alter the allowed fields meaning your custom property can then be returned as part of the store response. Hope this make sense otherwise let me know |
Beta Was this translation helpful? Give feedback.
-
I have created a new table and want this table's primary key as a foreign key in the Product table as a new column in the Product table. Can we do this? Please do have a detailed explanation. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Hello guys, I've the same issue above but i was extending the customer's attribute by adding companyName (screenshot attached), when i try to create new customer via Store API in Postman it returns : however when i try to get the customer's data, it works perfectly and the companyName attribute showed. So what should i do for this type of issue? I've followed @adrien2p answer but i keep getting error like : I've followed all the instruction given in below doc: Hope u guys can help, Thanks in advance! |
Beta Was this translation helpful? Give feedback.
-
Hello everyone, I have the same issue as mentioned above. I followed @adrien2p's answer, but something peculiar happens. When I initially run the build and log in as admin, I encounter a CORS issue. However, when I save my I hope you can help. Thank you in advance! |
Beta Was this translation helpful? Give feedback.
-
unfortunately it doesn't work for me... validator even check the validity for extended attribute. // src/loader/index.ts
// src/loader/product-entity-extend.ts
// src/utils/register-extended-validator.ts
data is for sure in database, and displayed in raw product object on detail page |
Beta Was this translation helpful? Give feedback.
-
I have a similar issue. I've extended the order entity by prop I've created a custom validator
I've created an extended Entity
I've extended API routes as it is mentioned in the docs
And this is my result:
Can anyone help me please? |
Beta Was this translation helpful? Give feedback.
-
I have extended the defaultFields like
this is for a cart. We need to add a store_id, from the store api calls. This is the other code
and the loader
when I create the cart the store_id is null even though it has been supplied in the POST call. This is the CartService extended
note the logs log1:
then to show cart is created finally But still the cart created has store_id as null, even after hardcoding the value in the extended service. What could be the issue. Any help would be appreciated |
Beta Was this translation helpful? Give feedback.
-
Hello everyone, I have been reading the Extend entity docs.
It is working amazingly, I can create fields and retrieve them.
But, what if we want to implement it using our admin?
Example:
The output will be:
The explanation about this is in:
src/api/admin/product/create-product.ts
medusa/packages/medusa/src/api/routes/admin/products/create-product.ts
Line 109 in d38b324
we find
medusa/packages/medusa/src/api/routes/admin/products/create-product.ts
Lines 551 to 661 in d38b324
Where we can't see our customAttribute (obviously)
So, the validator fails as our customAttribute doesn't exists.
Obviously, a developer could just override the /admin/products use the following code in the backend. (When implementing medusa backend in their project)
/api/index.ts
But I think this is not the best way to handle the problem.
I'm new checking medusa codebase, so I don't know how we could override the
AdminPostProductsReq
Any help?
Thank you.
You can replicate the problem using this comment as a reference:
#3907 (comment)
Beta Was this translation helpful? Give feedback.
All reactions