简体   繁体   中英

How to add a custom field to existing entity in MedusaJS

I would like to add a custom field(named description ) to ProductCollection Entity in MedusaJS server application. What I have followed so far is that:

  • created a new file under src/models folder, which is myCollection.ts with following content
import { Column, Entity } from "typeorm";
import { ProductCollection } from "@medusajs/medusa";

@Entity()
export class MyProductCollection extends ProductCollection {
  @Column({ type: "varchar" })
  description: string | null;
}

  • created an migration using this command: npx typeorm migration:create -n DescriptionAdded --dir src/migrations and it gives me new file under folder src/migrations named 1674744755671-DescriptionAdded.ts and I edited the file like this:
import { MigrationInterface, QueryRunner, TableColumn } from "typeorm";

export class DescriptionAdded1674744755671 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.addColumn(
      "product_collection",
      new TableColumn({
        name: "description",
        type: "varchar",
        isNullable: true,
      })
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropColumn("product_collection", "description");
  }
}

Then I run the migration using this command medusa migrations run . And as I expected, it created the field in database table. Then I built the medusa application by this command yarn run build . And I restarted the application with yarn run start . I am using latest medusajs server application with the latest medusa package which is 1.7.5 .

What I expected is that getting this field when I call the following api:

http://localhost:9000/store/collections

I have created the medusajs server app with these commands:

yarn global add @medusajs/medusa-cli
medusa new my-medusa-store --seed

I am running medusa server app on my local and it does not give the new field named description .

I could not find how to add custom field to medusa server application. What is the way of extending existing entities in medusajs server application? I have seen medusa-extender repository but it does not show any example how to do it.

I hope you might help me.

Thanks

I have installed medusa server app using medusa/cli and added files described above. I could not find any documentation about this on medusajs website.

Co-author of Medusa here.

We generally advise developers to not extend the core database schemas with additional columns, as it could compromise the maintainability of your setup. Therefore, we do not expose a native way to do it. Though, we have plans to add a Custom Field API in the near future, that would effectively allow you to extend your schemas, but not directly. It will likely be accomplished following an EAV pattern.

Also, it seems you might have confused new entities with additional table columns. When you add models in /src/models , you add new tables to your database - not columns. Read more here .

I do not have experience with Typeorm's addColumn functionality, so I can't say anything about how that affects your setup. What I can say is that we query a set of default fields and relations on entities in most GET /store/* endpoints. If you don't explicitly provide your additional fields, they will not be part of the response.

Regarding the extender, you can indeed use that to extend your database schemas but, again, it comes with the caveat of potentially compromising the maintainability of your instance and future upgrade paths.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM