From b9e21af7ec354ba49d3835e64f756e31a49ead71 Mon Sep 17 00:00:00 2001 From: krishnareddy-tadi Date: Tue, 27 Jun 2023 19:32:20 +0530 Subject: [PATCH] Modify condition to ignore accents and diacritics It will apply unaccent to field and search string to avoid accents and diacritics for like and ilike operators in a query --- src/db/util/conditions.ts | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/db/util/conditions.ts b/src/db/util/conditions.ts index 175008cd..23dff554 100644 --- a/src/db/util/conditions.ts +++ b/src/db/util/conditions.ts @@ -77,11 +77,11 @@ namespace PropertyConditions { // Simple binary operator conditions - export type LikeCondition = { - [Op.LIKE]: T & string; + export type LikeCondition = { + [Op.LIKE]: string; }; - export type ILikeCondition = { - [Op.ILIKE]: T & string; + export type ILikeCondition = { + [Op.ILIKE]: string; }; export type LtCondition = { [Op.LT]: T; @@ -104,8 +104,8 @@ namespace PropertyConditions { | NotInCondition | IsNullCondition | BetweenCondition - | LikeCondition - | ILikeCondition + | LikeCondition + | ILikeCondition | LtCondition | LteCondition | GtCondition @@ -138,12 +138,12 @@ namespace PropertyConditions { export const isLikeCondition = ( condition: Condition - ): condition is LikeCondition => + ): condition is LikeCondition => Object.prototype.hasOwnProperty.call(condition, Op.LIKE); export const isILikeCondition = ( condition: Condition - ): condition is ILikeCondition => + ): condition is ILikeCondition => Object.prototype.hasOwnProperty.call(condition, Op.ILIKE); export const isLtCondition = ( @@ -282,13 +282,15 @@ export const prepareCondition = propertyCondition[Op.BETWEEN][1], ]); } else if (PropertyConditions.isLikeCondition(propertyCondition)) { - builder.where(property as string, 'like', propertyCondition[Op.LIKE]); + builder.whereRaw('unaccent(:column:) like unaccent(:value)', { + column: String(property), + value: propertyCondition[Op.LIKE], + }); } else if (PropertyConditions.isILikeCondition(propertyCondition)) { - builder.where( - property as string, - 'ilike', - propertyCondition[Op.ILIKE] - ); + builder.whereRaw('unaccent(:column:) ilike unaccent(:value)', { + column: String(property), + value: propertyCondition[Op.ILIKE], + }); } else if (PropertyConditions.isLtCondition(propertyCondition)) { builder.where(property, '<', propertyCondition[Op.LT] as any); } else if (PropertyConditions.isLteCondition(propertyCondition)) {