Typeorm 类似查询

问题描述 投票:0回答:1

typeorm的类似查询是不工作的.这里是代码,我试图通过关键字在电子邮件中搜索。

async findAll(findAllQuery: FindAllQuery): Promise<any> {
    const [users, total] = await this.usersRepository.findAndCount(
      {
        where: {
          email: Like(`%${findAllQuery.keyword}%`),
        },
        skip: findAllQuery.page - 1,
        take: findAllQuery.itemsPerPage,
      },
    );
    return { users, total };
  }

有什么办法可以让这个工作?

node.js search typeorm
1个回答
0
投票

我终于做到了避开Like,看来我误解了Like的意思,我想要的是通过关键字在用户中搜索,所以我做了这样的事情,并且成功了。

async findAll(findAllQuery: FindAllQuery): Promise<any> {
    const options: FindManyOptions<User> = {};

    if(findAllQuery.keyword){
      options.where = {

        $or:[
          {email: RegExp(`^${findAllQuery.keyword}`,'i')},
          {firstName: RegExp(`^${findAllQuery.keyword}`,'i')},
          {lastName: RegExp(`^${findAllQuery.keyword}`,'i')},
          {username: RegExp(`^${findAllQuery.keyword}`,'i')},
        ],
      }
    }

    if(findAllQuery.page){
      options.skip = findAllQuery.page - 1;
    }

    if(findAllQuery.itemsPerPage){
      options.take = findAllQuery.itemsPerPage;
    }

    const [users, total] = await this.usersRepository.findAndCount(options);
    return { users, total };
  }
© www.soinside.com 2019 - 2024. All rights reserved.