当对象值具有不同类型时,按多个Query Params过滤数据



我已经试了好几天了,我觉得我已经掌握了这些东西,但我不知道我还缺什么。

我的对象到达我的服务时是这样的:

GetAllDataFilterDto {
orgId: 12345,
status: [ 'approved', 'pending' ],
}

我正在使用typeform,这是我的查询:

protected getAll(filter: GetAllDataFilter): SelectQueryBuilder<EntityName> {
const qb = Repository<EntityName>
.createQueryBuilder('entityName')
.select([
'entityName.id',
'entityName.status',
'entityName.orgId',
])
.groupBy('entityName.id')
.where(this.getFilterConditions(filter));
return qb;
}

我的查询使用getMany(),但在不同的文件中,这就是为什么它不包含在这个函数中。

这就是我遇到问题的地方,我当前的过滤器函数看起来像这样:

protected getFilterConditions(filter: GetAllDataFilter) {
return _.pickBy(filter, (value, key) => value !== undefined && this.entityProperties.has(key));
}

entityProperties是如下所示的集合:

Set {
'id',
'status',
'orgId'
}

目前,这个过滤器适用于数字,如果我的数组只有一个值,但是当我添加两个值时,它返回错误

trace: QueryFailedError: Operand should contain 1 column(s)

我的最终目标是过滤返回的数据,即使有可选参数,因此将返回所有已批准或未决的状态列,但我仍然需要能够同时通过orgId进行过滤。

请告诉我是否还有其他需要我介绍的内容,谢谢。

我认为问题可能是你的queryBuilder上的.where,它应该取1个参数(换句话说,只检查1个字段的条件),然后你可以添加.andWhere来添加过滤器上的1个参数。

From the docs:

.where

条件:

添加WHERE表达式就像:

createQueryBuilder("user").where("user.name = :name", { name: "Timber" })

用于多个.where条件:

可以在已有的WHERE表达式中添加AND:

createQueryBuilder("user")
.where("user.firstName = :firstName", { firstName: "Timber" })
.andWhere("user.lastName = :lastName", { lastName: "Saw" })

最新更新