Skip to content

Commit

Permalink
fix(select-buildwhere): support More/LessThanOrEqual for relations
Browse files Browse the repository at this point in the history
Tweak `buildWhere()` to recognise more find operators and output
the SQL equivalent

- Recognise `moreThanOrEqual` and `lessThanOrEqual`, in addition to
  `moreThan` and `lessThan`
- Dedicate an if block to `moreThanOrEqual` and `lessThanOrEqual`,
  replicating most of the `moreThan`/`lessThan` equivalents, for
  reading simplicity
  • Loading branch information
LoneRifle committed May 9, 2023
1 parent 16f778e commit fe7bc74
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4412,6 +4412,97 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
" " +
parseInt(where[key].value),
)
} else if (
where[key].type === "moreThanOrEqual" ||
where[key].type === "lessThanOrEqual"
) {
const sqlOperator =
where[key].type === "moreThan" ? ">=" : "<="
// basically relation count functionality
const qb: QueryBuilder<any> = this.subQuery()
if (relation.isManyToManyOwner) {
qb.select("COUNT(*)")
.from(
relation.joinTableName,
relation.joinTableName,
)
.where(
relation.joinColumns
.map((column) => {
return `${
relation.joinTableName
}.${
column.propertyName
} = ${alias}.${
column.referencedColumn!
.propertyName
}`
})
.join(" AND "),
)
} else if (relation.isManyToManyNotOwner) {
qb.select("COUNT(*)")
.from(
relation.inverseRelation!.joinTableName,
relation.inverseRelation!.joinTableName,
)
.where(
relation
.inverseRelation!.inverseJoinColumns.map(
(column) => {
return `${
relation.inverseRelation!
.joinTableName
}.${
column.propertyName
} = ${alias}.${
column.referencedColumn!
.propertyName
}`
},
)
.join(" AND "),
)
} else if (relation.isOneToMany) {
qb.select("COUNT(*)")
.from(
relation.inverseEntityMetadata.target,
relation.inverseEntityMetadata
.tableName,
)
.where(
relation
.inverseRelation!.joinColumns.map(
(column) => {
return `${
relation
.inverseEntityMetadata
.tableName
}.${
column.propertyName
} = ${alias}.${
column.referencedColumn!
.propertyName
}`
},
)
.join(" AND "),
)
} else {
throw new Error(
`This relation isn't supported by given find operator`,
)
}
// this
// .addSelect(qb.getSql(), relation.propertyAliasName + "_cnt")
// .andWhere(this.escape(relation.propertyAliasName + "_cnt") + " " + sqlOperator + " " + parseInt(where[key].value));
this.andWhere(
qb.getSql() +
" " +
sqlOperator +
" " +
parseInt(where[key].value),
)
} else {
if (
relation.isManyToOne ||
Expand Down

0 comments on commit fe7bc74

Please sign in to comment.