typescript - TypeORM RelationCount 替代
问题描述
我在一个项目中使用 TypeORM 一段时间。我们有一个拥有以下属性的实体
@RelationCount("sentences")
public sentencesCount?: number;
@OneToMany(() => Sentence, e => e.job)
public sentences?: Sentence[];
但是我注意到它RelationCount
已经贬值了
@RelationCount, deprecated 不要使用这个装饰器,它可能会在未来的版本中被移除
我在 TypeORM 官方自述文件中寻找替代品,但除了QueryBuilder
使用本机 SQLSELECT COUNT(*)
语句创建自定义之外,我什么也没找到。
在这个项目中我们使用了很多简单的语句,比如myRepo.find()
. 我宁愿不替换所有这些对QueryBuilders
.
我应该@RelationCount
在我们的项目中使用什么来代替?
我应该忽略这个“贬值”警告吗??
解决方案
你需要使用loadRelationCountAndMap
方法
yourRepository
.createQueryBuilder("entity")
.leftJoin('entity.sentences', 'sentences')
.loadRelationCountAndMap('entity.sentencesCount', 'entity.sentences');
@RelationCount("sentences")
public sentencesCount?: number;
并从您的实体类中删除。
推荐阅读
- ruby-on-rails - 在用户使用 Ruby 中的地理编码器唱歌期间进行地址验证
- business-intelligence - 数据仓库事实星座模式
- javascript - 我需要删除重复项并将总数与 JavaScript 中的对象相加
- python - 如何连接具有 O2M 和 M2M 关系的表?
- java - 在启动 Spring Boot 应用程序时刷新用户会话
- python - Python 3 - 如何创建允许用户在输入密码时同时输入字母和数字的代码
- node.js - 使用节点设置 Mac 终端
- javascript - 如何在微妙的slidershow.js中停止无限循环?
- python - Python手写文本提取
- r - 将多个分类列编码为R中的数字