使用 NestJS 注入 ObjectionJS 模型抛出异常
Injecting ObjectionJS model using NestJS throws exception
当我尝试将 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"
},
According to this issue Typescript 必须在 strict
模式下 运行 才能 运行 正确拼写代码。似乎 Objection v3 仍处于某种预发布状态,因此还没有完整记录。
当我尝试将 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"
},
According to this issue Typescript 必须在 strict
模式下 运行 才能 运行 正确拼写代码。似乎 Objection v3 仍处于某种预发布状态,因此还没有完整记录。