typeOrm 在 where 子句中使用条件
@Injectable()
导出类 WeightPriceService { 构造函数(readonly dbContext: DbContext) {}
async findPriceByWeight(weight: number, tariffType?: PackageMaterialType): Promise<number> {
const { price } = await this.dbContext.tariffs.findOne({
where: {
type: tariffType ? tariffType : ,
isActive: true,
weight: { min: LessThan(weight), max: MoreThan(weight) },
},
relations: ['weight'],
});
return price;
}
}
“tariffType”参数是否为 true 我想检查“type:tariffType”,否则不想检查“type”
@Injectable()
export class WeightPriceService {
constructor(readonly dbContext: DbContext) {}
async findPriceByWeight(weight: number, tariffType?: PackageMaterialType): Promise<number> {
const { price } = await this.dbContext.tariffs.findOne({
where: {
type: tariffType ? tariffType : ,
isActive: true,
weight: { min: LessThan(weight), max: MoreThan(weight) },
},
relations: ['weight'],
});
return price;
}
}
Is "tariffType" parameter is true I would like to check "type:tariffType" else do not want to check "type"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用展开运算符来实现此目的。
当条件为 True 时,它将注入对象,否则不会添加任何内容。
在你的情况下它会是
You can use the spread operator for this.
When the condition is
True
it will inject the object otherwise wouldn't add anything.In your case it would be