【问题标题】:Injecting ObjectionJS model using NestJS throws exception使用 NestJS 注入 ObjectionJS 模型会引发异常
【发布时间】:2021-12-29 14:58:34
【问题描述】:

当我尝试将 Objection.js 模型注入 NestJs 服务时:

constructor(@Inject('UserModel') private readonly modelClass: ModelClass<UserModel>) {}

我在编译时收到Maximum call stack size exceeded 错误。我认为这是由于我的其他模型和一些循环依赖。我删除了单个模型之外的所有内容,但仍然出现异常。

如果我在 tsconfig.json 中设置 "strict": true,代码将按预期构建和运行。我该如何补救这种情况?

下面是使用的模型和包。

基础模型

export default class BaseModel extends Model {
  readonly id: number;
  createdBy: number;
  created!: string;
  modified: string;
  modifiedBy: number; 
}

用户模型

export default class UserModel extends BaseModel {
  static tableName = 'users';
  static virtualAttributes = ['fullName'];

  firstName!: string;
  lastName!: string;
  company?: string;
  email!: string;

  fullName(): string {
    return `${this.firstName} ${this.lastName}`;
  }


  static jsonSchema = {
    type: 'object',
    required: ['firstName', 'lastName', 'email'],
    properties: {
      id: { type: 'number' },
      firstName: { type: 'string' },
      lastName: { type: 'string' },
      company: { type: 'string' },
      email: { type: 'string' },
    },
  };
}

包:

  "dependencies": {
    "@apollo/gateway": "^0.44.0",
    "@nestjs/common": "^8.2.1",
    "@nestjs/core": "^8.2.1",
    "@nestjs/graphql": "^9.1.1",
    "@nestjs/platform-express": "^8.0.0",
    "ajv-formats": "^2.1.1",
    "apollo-server-express": "^3.5.0",
    "class-transformer": "^0.4.0",
    "class-validator": "^0.13.1",
    "dayjs": "^1.10.7",
    "dotenv": "^10.0.0",
    "graphql-subscriptions": "^2.0.0",
    "knex": "^0.95.14",
    "objection": "^3.0.0",
    "pg": "^8.7.1",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.4.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^8.0.0",
    "@nestjs/schematics": "^8.0.0",
    "@nestjs/testing": "^8.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "^27.0.1",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-import": "2.25.2",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "^27.2.5",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "^27.0.3",
    "ts-loader": "^9.2.3",
    "ts-morph": "^12.2.0",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "^3.10.1",
    "typescript": "^4.4.2"
  },

【问题讨论】:

  • 你们的模型是圆形的吗?即它们相互引用?
  • 我认为这可能是罪魁祸首,但我删除了发布的用户之外的所有模型,但我仍然收到此错误。
  • 通常像这样的最大堆大小错误来自内存泄漏,这在循环文件/循环类中很常见。当您nest build 时,您是否收到此错误?
  • 我确实收到了嵌套构建错误。我知道这是一个编译错误。有趣的是,如果我在 tsconfig.json 中设置"strict": true,代码会按预期构建和运行。
  • 那么最后一个问题:您是否仍然使用tsc 得到这个错误?

标签: node.js npm nestjs knex.js objection.js


【解决方案1】:

According to this issue Typescript 必须在strict 模式下运行才能正确转译代码。似乎 Objection v3 仍处于某种预发布状态,因此尚未完全记录。

【讨论】:

  • 感谢您的帮助。我看到建议运行严格模式。我会保持启用它,但如果图书馆可以在没有它的情况下工作,那就太好了。
猜你喜欢
  • 1970-01-01
  • 2021-03-07
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多