I am working on Medusa JS and need to add a custom field in Core Model of "Cart" that means extending an Entity. So following guide -> Extending Entity I was able to do everything. On updating the value of newly created field of cart using Store API I am not getting any error that means validation is also working fine, but the problem is field is not getting updated in DB.
Step-2 (as per documentation): Extending the Cart Model
import { Column, Entity } from "typeorm";
import {
// alias the core entity to not cause a naming conflict
Cart as MedusaCart,
} from "@medusajs/medusa";
@Entity()
export class Cart extends MedusaCart {
@Column()
is_subscribed?: boolean;
}
Step-3: Creating Typescript Declaration File
export declare module "@medusajs/medusa/dist/models/order" {
declare interface Order {
is_subscribed: boolean;
}
}
Extending Validation (Using Update Cart Validation)
import { registerOverriddenValidators } from "@medusajs/medusa";
import { StorePostCartsCartReq as MedusaStorePostCartsCartReq } from "@medusajs/medusa/dist/api/routes/store/carts/update-cart";
import { IsBoolean, IsOptional } from "class-validator";
class StorePostCartsCartReq extends MedusaStorePostCartsCartReq {
@IsOptional()
@IsBoolean()
is_subscribed?: boolean;
}
registerOverriddenValidators(StorePostCartsCartReq);
I need to update the value of the custom field so that I can create a subscriber on the basis of this value. That I can only do when I am able to modify the value of this attribute using Update store API.
I believe you also have to also extend the repo and the services (I assume you ran a migration) that pertain to the cart. I solved this issue after a long time of messing around and looking into the node modules. Here is my solution that I posted in a thread on Github:
I have solved this issue after a long time of messing around with everything I could and looking into the node modules. I'll show you what my files look like. I wanted to extend the prices (MoneyAmount entity) array within the variants array which is nested in the products. So when I POST a new product, the new custom attributes should be present in the prices array in the return and updated in the db for in the MoneyAmount Entity.
Extension of MoneyAmount:
Migration:
Loader file to allow return of custom attributes:
Extending Repository of MoneyAmount (Notice that it actually is different form the DOCS as following the way the docs showed resulted in an error. Refer to this thread which I followed https://github.com/medusajs/medusa/issues/6139):
Overriding validators:
Extending Product Variants Services (Creation of a product calls on product variants services):
I want to also include that I was trying to get this to work while working on Github code spaces and my files looked exactly like the ones I have shown you for a long time and it would not input the custom attribute values to the db I have supplied in the POST body, rather it would just leave it as the default values. After I used up all the hours they allowed me I had to export the changes I made in the code space to a new branch and clone the GitHub repo to VS code and work on there. I ran npm i twice because the first failed for some reason, and then instead of using region_id in my post request body I switched it to currency-code (for some reason the region_id in the POST body caused an error when sending the request). Then it worked to my amazement.
FIGURED OUT CUASE OF ISSUE HERE: Turns out you do not need to extend the repo or the service to have it to work. I figured out what was causing the issue. Replacing region_id to currency_code was the fix. In the docs it states for a POST request when crating a product, the prices array needs to have EITHER region_id or currency_code (having both results in a XOR error). I'm not really sure why that is the fix as the Money Amount (prices) table has columns for both currency_code and region_id. My guess is that when you create a product on the admin dashboard with both variants and prices, the data inserted in the Money Amount table has a currency_code value but the region_id value is empty. So by only having currency_code and not region_id in your request body, you follow how the admin dashboards creates data for the Money Amount table. Again this is an assumption for why region_id creates the issue and currency_code does not.