The*_*son 5 typescript knex.js objection.js nestjs
我目前正在使用 NestJS 设置 Objection.js,但在使用任何类型的图形获取或保存方法时遇到问题。相关型号:
import { BaseModel } from './base.model';
import { Model } from 'objection';
import { ResourceGroupTypeModel } from './resource-group-type.model';
import { CollectionModel } from './collection.model';
import { AttributeModel } from './attribute.model';
import { ResourceModel } from './resource.model';
export class ResourceGroupModel extends BaseModel {
static tableName = 'resource_group';
typeId: number;
title: string;
groupId: string;
collectionId?: number;
defaultResourceId?: number;
isDeleted: boolean;
static relationMappings = {
attributes: {
modelClass: AttributeModel,
relation: Model.ManyToManyRelation,
join: {
from: 'resource_group.id',
through: {
from: 'resourcegroup_attribute.resource_group_id',
to: 'resourcegroup_attribute.attribute_id'
},
to: 'attribute.id',
},
},
}
}
Run Code Online (Sandbox Code Playgroud)
import { BaseModel } from './base.model';
import { Model } from 'objection';
export class AttributeModel extends BaseModel {
static tableName = 'attribute'
name: string;
value: string;
isSystem: boolean;
static relationMappings = {
resourceGroups: {
modelClass: `${__dirname}/resource-group.model.js`,
relation: Model.ManyToManyRelation,
join: {
from: 'attribute.id',
through: {
from: 'resourcegroup_attribute.attribute_id',
to: 'resourcegroup_attribute.resource_group_id',
},
to: 'resource_group.id',
},
},
}
}
Run Code Online (Sandbox Code Playgroud)
import { Injectable, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { EntityService } from '../abstract';
import { ResourceGroup } from '../entities';
import { Repository } from 'typeorm';
import { ModelClass } from 'objection';
import { ResourceGroupModel } from '../db/models/resource-group.model';
@Injectable()
export class ResourceGroupService extends EntityService<ResourceGroup> {
constructor(
@InjectRepository(ResourceGroup)
protected readonly repository: Repository<ResourceGroup>,
@Inject(ResourceGroupModel)
protected readonly model: ModelClass<ResourceGroupModel>,
) {
super(repository);
}
test() {
return this.model
.query()
// .findById(25); // this line works fine
.withGraphFetched(['attributes']) // these two lines do not
.where({id: 25});
}
}
Run Code Online (Sandbox Code Playgroud)
有人对发生的事情有任何想法吗?我查看了源代码,但这似乎是我的代码中的一个问题,也许是我的一个模型中的问题。
The*_*son 11
所以我在这个问题上停留了一段时间,结果发现这是非常简单的事情。我们resource-group.service.ts需要做一些小小的改变。
return this.model
.query()
.withGraphFetched(['attributes'])
.where({id: 25});
Run Code Online (Sandbox Code Playgroud)
需要成为
return this.model
.query()
.withGraphFetched('[attributes]') // square brackets are inside the string itself
.where({id: 25});
Run Code Online (Sandbox Code Playgroud)
代码盲的典型案例,但在 Google 上找不到我的错误消息的任何点击。希望这对将来真正需要一杯咖啡的人有所帮助(:
| 归档时间: |
|
| 查看次数: |
1233 次 |
| 最近记录: |