Typeorm .loadRelationCountAndMap returns 个零
Typeorm .loadRelationCountAndMap returns zeros
请帮忙。
我正在尝试执行以下 typeorm 查询:
return await getRepository(Company)
.createQueryBuilder("Company")
.leftJoinAndSelect("Company.plants", "Plant")
.leftJoinAndSelect("Plant.documents", "Document")
.leftJoinAndSelect("Plant.notes", "Note")
.loadRelationCountAndMap("Plant.documentsCount", "Plant.documents")
.loadRelationCountAndMap("Plant.notesCount", "Plant.notes")
.getMany();
我们的想法是 select 每个工厂以及所有公司的所有工厂的文件和注释的数量。
(实际上 select 不需要记录笔记和文件本身,但我这样做是为了证明关系确实有效)。
我还指定了占位符变量以在 Plant 实体中保持计数:
@OneToMany(() => Document, (document) => document.plant)
documents: Document[];
documentsCount: number;
@OneToMany(() => Note, (note) => note.plant)
notes: Note[];
notesCount: number;
奇怪的是返回的 Plant.documentsCount 和 Plant.notesCount 是 0(而文档和笔记的集合不为空并且正在 selected)。
另一个奇怪的事情是我在 SQL 查询中没有看到任何尝试 select 这些计数,因此我希望 typeorm 本身会计数(因为它有集合 select编辑正确)。
有人可以就如何 select 这些计数给出一些建议吗?
不幸的是,Typeorm 是最无能的框架。所有重要功能都已弃用或未实现。
为了解决这个特殊问题,我必须:
- Select 集合本身:
.leftJoinAndSelect("Plant.documents", "Document")
.leftJoinAndSelect("Plant.notes", "Note")
- 添加计算和冗余关系数组删除:
@AfterLoad()
getDocumentsCount() {
this.documentsCount = this.documents.length;
delete this.documents;
}
@AfterLoad()
getNotesCount() {
this.notesCount = this.notes.length;
delete this.notes;
}
- 决定永远不使用 TypeORM。
请帮忙。 我正在尝试执行以下 typeorm 查询:
return await getRepository(Company)
.createQueryBuilder("Company")
.leftJoinAndSelect("Company.plants", "Plant")
.leftJoinAndSelect("Plant.documents", "Document")
.leftJoinAndSelect("Plant.notes", "Note")
.loadRelationCountAndMap("Plant.documentsCount", "Plant.documents")
.loadRelationCountAndMap("Plant.notesCount", "Plant.notes")
.getMany();
我们的想法是 select 每个工厂以及所有公司的所有工厂的文件和注释的数量。 (实际上 select 不需要记录笔记和文件本身,但我这样做是为了证明关系确实有效)。
我还指定了占位符变量以在 Plant 实体中保持计数:
@OneToMany(() => Document, (document) => document.plant)
documents: Document[];
documentsCount: number;
@OneToMany(() => Note, (note) => note.plant)
notes: Note[];
notesCount: number;
奇怪的是返回的 Plant.documentsCount 和 Plant.notesCount 是 0(而文档和笔记的集合不为空并且正在 selected)。
另一个奇怪的事情是我在 SQL 查询中没有看到任何尝试 select 这些计数,因此我希望 typeorm 本身会计数(因为它有集合 select编辑正确)。
有人可以就如何 select 这些计数给出一些建议吗?
不幸的是,Typeorm 是最无能的框架。所有重要功能都已弃用或未实现。
为了解决这个特殊问题,我必须:
- Select 集合本身:
.leftJoinAndSelect("Plant.documents", "Document")
.leftJoinAndSelect("Plant.notes", "Note")
- 添加计算和冗余关系数组删除:
@AfterLoad()
getDocumentsCount() {
this.documentsCount = this.documents.length;
delete this.documents;
}
@AfterLoad()
getNotesCount() {
this.notesCount = this.notes.length;
delete this.notes;
}
- 决定永远不使用 TypeORM。