【问题标题】:Dependency cycle detected.eslint with typeorm依赖循环检测到.eslint 与 typeorm
【发布时间】:2020-11-08 19:25:35
【问题描述】:

我有两个具有一对一双向的实体类型:

部门:

@Entity('Departament')
export default class Departament {
  @PrimaryGeneratedColumn()
  id: string;

  @Column()
  departament_name: string;

  @OneToOne(type => User, user => user.departament)
  @JoinColumn()
  user: User;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  UpdatedAt: Date;
}

用户:

@Entity('User')
export default class User {
  @PrimaryGeneratedColumn()
  id: string;

  @Column()
  name: string;

  @Column()
  last_name: string;

  @Column()
  email: string;

  @Column()
  login: string;

  @Column()
  password: string;

  @OneToOne(type => Departament, departament => departament.user)
  departament: Departament;
}

这些是我的 .eslintrc 设置:

{
  "env": {
    "es6": true,
    "node": true,
    "jest": true
  },
  "extends": [
    "airbnb-base",
    "plugin:@typescript-eslint/recommended",
    "prettier/@typescript-eslint",
    "plugin:prettier/recommended"
  ],
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module"
  },
  "plugins": ["@typescript-eslint", "prettier"],
  "rules": {
    "prettier/prettier": "error",
    "no-new": "off",
    "no-underscore-dangle": "off",
    "class-methods-use-this": "off",
    "no-await-in-loop": "off",
    "import/prefer-default-export": "off",
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "ts": "never"
      }
    ],
    "import/no-extraneous-dependencies": [
      "error",
      {
        "devDependencies": ["**/*.spec.ts", "src/utils/tests/*.ts"]
      }
    ],
    "no-useless-constructor": "off",
    "@typescript-eslint/no-unused-vars": [
      "error",
      {
        "argsIgnorePattern": "_"
      }
    ],
    "@typescript-eslint/no-useless-constructor": "error",
    "camelcase": "off",
    "@typescript-eslint/camelcase": "off"
  },
  "overrides": [
    {
      "files": ["*.js"],
      "rules": {
        "@typescript-eslint/no-var-requires": "off"
      }
    }
  ],
  "settings": {
    "import/extensions": [".ts", ".js"],
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".js"]
    },
    "import/resolver": {
      "typescript": {
        "alwaysTryTypes": true
      }
    }
  }
}

我收到了这个错误:

检测到依赖循环.eslintimport/no-cycle

on(用户和部门)

和:

'type' 已定义但从未使用过。允许的未使用参数必须匹配 /_/u.eslint@typescript-eslint/no-unused-vars

我无法解决这个问题,我不知道最好的选择是什么,我正在关注 typeorm 的入门

【问题讨论】:

    标签: typescript typeorm


    【解决方案1】:

    循环导入的某些情况可能会让编译器感到困惑,但大多数情况下它们可以毫无问题地解决。 eslint import/no-cycle 规则的存在是为了尽早消除任何潜在的问题,这是一件好事。有一个很好的 Stack Overflow answer 可以更清楚地说明这个问题,并建议完全避免循环依赖。

    TypeORM 存储库中有 a discussion,它提供了多种解决方案,每个解决方案都有自己的取舍。最完整的解决方案是entity schemas 和string references。两种解决方案都使用类似的想法——定义与字符串的关系并在稍后解析字符串。

    这是解决循环依赖的常用方法,可以在其他 ORM 中找到,例如 Python 的 SQLAlchemy 或 Ruby on Rails 的 ActiveRecord。这个想法很简单:实体类/模式是通过实体的名称字符串从注册表中检索的,这消除了显式导入模块来定义关系的需要。

    让我举两个例子。

    实体架构

    这是一种定义实体的实用方法。类定义替换为EntitySchema 的实例,并且没有使用装饰器。

    export const DepartmentEntity = new EntitySchema({
      name: "Department",
      columns: { /* omitting for brevity */ },
      relations: {
        user: {
          type: "one-to-one",
          target: "User"
        }
      }
    })
    
    export const UserEntity = new EntitySchema({
      name: "User",
      columns: { /* omitting for brevity */ },
      relations: {
        department: {
          type: "one-to-one",
          target: "Department"
        }
      }
    })
    

    字符串引用

    这对您已有的内容进行了不太彻底的更改,它只需要将类型函数和逆侧函数更改为字符串。要正确键入关系属性,您需要使用import type:

    import type User from './user';
    
    @Entity('Department')
    export default class Department {
      // omitting most of the columns
      // ...
    
      @OneToOne('User', 'department')
      @JoinColumn()
      user: User;
    }
    
    import type Department from './department';
    
    @Entity('User')
    export default class User {
      // omitting most of the columns
      // ...
    
      @OneToOne('Department', 'user')
      department: Department;
    }
    

    选择什么?

    这取决于你。机制完全相同,但实体模式很可能会保留并成为 TypeORM versions to come 中的一等公民,它们为您提供比类所提供的更多类型安全性。

    【讨论】:

      猜你喜欢
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      相关资源
      最近更新 更多