typeorm查询条件: 当为空的时候不查询,当存在的时候查询

1、解决方法:…( )

userRepository.find({
   where: {
      status: 1,
      ...(params?.company && { company: params.company }),
   }
});

所以,如果params.company 是undefined(或空字符串)那么

...(undefined && { company: undefined })

返回undefined 并像…{} 一样为您销毁。

如果params.company 包含一些值,则

...('company' && { company: 'company' })

返回 …{company: ‘company’} 并为您的位置销毁它。

例子:

const companyTestWithValue = 'company';
const companyTestWithoutValue = '';

const whereWithValue = {
  status: 1,
  ...(companyTestWithValue && { company: companyTestWithValue }),
};

const whereWithoutValue = {
  status: 1,
  ...(companyTestWithoutValue && { company: companyTestWithoutValue }),
};

console.log('whereWithValue:', whereWithValue);
console.log('whereWithoutValue:', whereWithoutValue);

2、解决方法

	import { User } from "./../user/entities/user.entity"
    let res = await this.articleRepository.createQueryBuilder()
      .leftJoinAndSelect(
        User,
        "user",
        "article.author_id = user.user_id"
      )
      .select(`
        article.article_id as articleId,
        article.title as title,
        date_format(article.update_time, '%Y-%m-%d %T') as updateTime,
        article.status as status,
        user.user_name as authorName
      `)
      .where(new Brackets((qb) => {
        if (body.authorName) {
          return qb.where(`user.user_name = ${body.authorName}`)
        } else {
          return qb;
        }
      }))
      .andWhere(new Brackets((qb) => {
        if (body.updateTimeStart && body.updateTimeEnd) {
          return qb.andWhere('article.update_time BETWEEN :start AND :end', {
            start: new Date(body.updateTimeStart),
            end: new Date(body.updateTimeEnd)
          })
        } else {
          return qb;
        }
      }))
      // .where(`user.user_name = ${body.authorName}`)
      // .andWhere('article.update_time BETWEEN :start AND :end', {
      //   start: new Date(body.updateTimeStart),
      //   end: new Date(body.updateTimeEnd)
      // })
      .andWhere({
        ...(body.sectionId && { sectionId: body.sectionId }),
        ...(body.sectionSubId && { sectionSubId: body.sectionSubId })
      })
      .orderBy("updateTime", "ASC")
      .skip(1)
      .take(20)
      .getRawMany();

where 和 andWhere ,orWhere的使用

const categories: CategoryEntity[] = await getManager()
      .createQueryBuilder(CategoryEntity, 'tsk')
      .where(new Brackets((qb) => qb.where('tsk.category = DEFAULT').andWhere('tsk.status = NEW')))
      .orWhere(new Brackets((qb) => qb.where('tsk.category = REVIEW').andWhere('tsk.status = OPEN')))
      .orWhere(new Brackets((qb) => qb.where('tsk.category = DEFAULT').andWhere('tsk.status = "IN PROGRESS"')))
      .getMany();
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值