fix: update searchBy query to allow searchBy on other column types than text

This commit is contained in:
Jakob Wagner 2023-01-13 15:53:41 +01:00 committed by GitHub
parent 529b727928
commit ab1c758def
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -298,23 +298,16 @@ export async function paginate<T extends ObjectLiteral>(
new Brackets((qb: SelectQueryBuilder<T>) => { new Brackets((qb: SelectQueryBuilder<T>) => {
for (const column of searchBy) { for (const column of searchBy) {
const propertyPath = (column as string).split('.') const propertyPath = (column as string).split('.')
if (propertyPath.length > 1) { const hasRelation =
const alias = queryBuilder.expressionMap.mainAlias.metadata.hasRelationWithPropertyPath( propertyPath.length > 1 &&
propertyPath[0] queryBuilder.expressionMap.mainAlias.metadata.hasRelationWithPropertyPath(propertyPath[0])
)
? `${qb.alias}_${column}` const aliasColumn = hasRelation ? `${qb.alias}_${column}` : `${qb.alias}.${column}`
: `${qb.alias}.${column}`
const condition: WherePredicateOperator = { if (['postgres', 'cockroachdb'].includes(queryBuilder.connection.options.type)) {
operator: 'ilike', qb.orWhere(`${aliasColumn}::text ILIKE '%${query.search}%'`)
parameters: [alias, `:${column}`],
}
qb.orWhere(qb['createWhereConditionExpression'](condition), {
[column]: `%${query.search}%`,
})
} else { } else {
qb.orWhere({ qb.orWhere(`UPPER(${aliasColumn}) LIKE UPPER('%${query.search}%')`)
[column]: ILike(`%${query.search}%`),
})
} }
} }
}) })