Home > front end >  Injecting ObjectionJS model using NestJS throws exception
Injecting ObjectionJS model using NestJS throws exception

Time:11-19

When I try to inject an Objection.js model into a NestJs service:

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

I get an Maximum call stack size exceeded error at compile time. I thought it was due to my other models and some circular dependency. I removed everything outside of a lone model and still get the exception.

If I set "strict": true in tsconfig.json, the code builds and runs as expected. How do I remedy this situation?

Below are the models and the packages used.

Base Model

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

User Model

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' },
    },
  };
}

Packages:

  "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"
  },

CodePudding user response:

According to this issue Typescript must be ran in strict mode to transpile the code correctly. Seems Objection v3 is still in some pre-release state so this isn't quite documented yet.

  • Related