TypeORMError:循环依赖:Typeorm @Tree Entity 中的“ProductCategory”

问题描述 投票:0回答:0

我在 TypeORM 中有一个实体,它在 postgres 中定义了一个类似树的表:

import { generateEntityId } from "../utils/generate-entity-id"
import { BaseEntity } from "../interfaces/models/base-entity"
import { kebabCase } from "lodash"
import { Product } from "."
import {
  BeforeInsert,
  Column,
  Entity,
  Index,
  JoinColumn,
  JoinTable,
  ManyToMany,
  Tree,
  TreeChildren,
  TreeParent,
} from "typeorm"

@Entity()
@Tree("materialized-path")
@Index(["parent_category_id", "rank"], { unique: true })
export class ProductCategory extends BaseEntity {
  static productCategoryProductJoinTable = "product_category_product"
  static treeRelations = ["parent_category", "category_children"]


  @Index({ unique: true })
  @Column({ nullable: false })
  handle: string

  @TreeParent()
  @JoinColumn({ name: "parent_category_id" })
  parent_category: ProductCategory | null

  // Typeorm also keeps track of the category's parent at all times.
  @Column()
  parent_category_id: string | null

  @TreeChildren({ cascade: true })
  category_children: ProductCategory[]

  @ManyToMany(() => Product, { cascade: ["remove", "soft-remove"] })
  @JoinTable({
    name: ProductCategory.productCategoryProductJoinTable,
    joinColumn: {
      name: "product_category_id",
      referencedColumnName: "id",
    },
    inverseJoinColumn: {
      name: "product_id",
      referencedColumnName: "id",
    },
  })
  products: Product[]

  @BeforeInsert()
  private beforeInsert(): void {
    this.id = generateEntityId(this.id, "pcat")

    if (!this.handle) {
      this.handle = kebabCase(this.name)
    }
  }
}

现在我将这个

ProductCategory
导入到我自己的文件中以像这样扩展它:

import {
  ProductCategory as MedusaProductCategory,
} from "@medusajs/medusa";
import {

  Column,
} from "typeorm";

@Entity()
@Tree("materialized-path")
export class ProductCategory extends MedusaProductCategory {
  @Column({ nullable: true })
  breadcrumb_name: string;

我正在使用别名,然后我将别名扩展 1 列。

在下一步中,我想使用我的自定义 ProductCategory 类并扩展现有的 TypeORM 存储库,如下所示:

import { ProductCategory } from "../models/product-category";
import { dataSource } from "@medusajs/medusa/dist/loaders/database";
import {
  // alias the core repository to not cause a naming conflict
  ProductCategoryRepository as MedusaProductCategoryRepository,
} from "@medusajs/medusa/dist/repositories/product-category";

export const ProductCategoryRepository = dataSource
  .getTreeRepository(ProductCategory)
  .extend({
    // it is important to spread the existing repository here.
    // Otherwise you will end up losing core properties.
    // you also update the target to the extended entity
    ...Object.assign(MedusaProductCategoryRepository, {
      target: ProductCategory,
    }),
    // you can add other customizations as well...
  });

export default ProductCategoryRepository;

如您所见,我导入了我的自定义 ProductCategory 并尝试扩展 Medusa 已经提供的现有 ProductCategoryRepository。

现在最大的问题是:每当我构建并运行它并尝试与数据库交互时,TypeORM 都会抛出这个错误:

error:   Cyclic dependency: "ProductCategory"
TypeORMError: Cyclic dependency: "ProductCategory"
    at visit (my_server/node_modules/typeorm/persistence/SubjectTopoligicalSorter.js:147:23)
    at visit (my_server/node_modules/typeorm/persistence/SubjectTopoligicalSorter.js:164:21)
    at SubjectTopoligicalSorter.toposort (my_server/node_modules/typeorm/persistence/SubjectTopoligicalSorter.js:143:17)
    at SubjectTopoligicalSorter.sort (my_server/node_modules/typeorm/persistence/SubjectTopoligicalSorter.js:53:45)
    at SubjectExecutor.execute (my_server/node_modules/typeorm/persistence/SubjectExecutor.js:91:108)
    at EntityPersistExecutor.execute (my_server/node_modules/typeorm/persistence/EntityPersistExecutor.js:148:36)

我已经尝试了几个小时,但找不到问题所在。 我的估计是我还需要以某种方式使用 @Tree 装饰器扩展实体。

非常感谢能回答这个问题的人和其他人🙏

node.js typeorm circular-dependency medusajs
© www.soinside.com 2019 - 2024. All rights reserved.