目前,我有以下查询:
return await this.siteRepository.find({
where: [{ id: Like(`%${q}%`) }, { name: Like(`%${q}%`) }]
});
但是我希望能够从一个数组中传递一个用于查询的列名列表,而不是手动编写每个列名。
const columns = ["id","name", "lastName", "age"]
const query = {};
return await this.siteRepository.find({
where: columns.map(column=>{(query[`${column}`] = `Like("%${q}%")}`)})
});
这甚至可能吗?我开始觉得目前不是。
答案 0 :(得分:0)
我无法使用Repository
TypeORM方法来完成我想要的工作,但是我确实可以使用QueryBuilder
这是我的解决方法
const res = ['id', 'name'].map(item => `${item} LIKE :q`).join(' OR ');
return await this.siteRepository
.createQueryBuilder()
.where(res, { q: `%${q}%` })
.getMany();
哪个查询为
SELECT `User`.`id` AS `User_id`, `User`.`name` AS `User_name`, `User`.`lastName` AS `User_lastName`, `User`.`active` AS `User_active` FROM `user` `User` WHERE name LIKE ? OR id LIKE ?