-
-
Notifications
You must be signed in to change notification settings - Fork 551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Any way to make the filter/or conditions case insensitive? #77
Comments
Doesn't this have something to do with you database collation? I believe SQL queries are case sensitive by default. |
Right, sql queries are case sensitive by default, that's why I want a case insensitive way to do the filtering, so the api can respond to a request for "column=a" and "column=A" and do a query like "lower(column) = lower(:paramA)" automatically |
All the "LIKE" conditions should be case insensitive: starts, ends, cont, excl. |
This should be an option and not the default behavior. Otherwise it could break a lot of applications. EDIT: I think this is what you meant, but I thought this ought to be clearly stated 😉 |
Actually this seems to be different from one database to another. For example, MySQL LIKE is case-insensitive and in PostgreSQL it's case sensitive. And you use different methods depending on DB to change this behavior. |
You're almost right. From what I know that mainly depends on your collation, but I might be wrong. Sometimes, the collation has been chosen by the devs to have case sensitive searches, sometimes it's the opposite. Thus, Anyway, let's stick to the original subject. |
@fwoelffel yep, I think we can provide some option and adjust query interceptor. |
Will the feature be available? sometimes we need query like this.
|
Workaround (for PostgresSQL) // @ts-ignore
TypeOrmCrudService.prototype.mapOperatorsToQuery = function(cond: QueryFilter, param: any): { str: string; params: ObjectLiteral } {
const field = this.getFieldWithAlias(cond.field)
let str: string
let params: ObjectLiteral
switch (cond.operator) {
case 'eq':
str = `${field} = :${param}`
break
case 'ne':
str = `${field} != :${param}`
break
case 'gt':
str = `${field} > :${param}`
break
case 'lt':
str = `${field} < :${param}`
break
case 'gte':
str = `${field} >= :${param}`
break
case 'lte':
str = `${field} <= :${param}`
break
case 'starts':
str = `${field} ILIKE :${param}`
params = { [param]: `${cond.value}%` }
break
case 'ends':
str = `${field} ILIKE :${param}`
params = { [param]: `%${cond.value}` }
break
case 'cont':
str = `${field} ILIKE :${param}`
params = { [param]: `%${cond.value}%` }
break
case 'excl':
str = `${field} NOT ILIKE :${param}`
params = { [param]: `%${cond.value}%` }
break
case 'in':
/* istanbul ignore if */
if (!Array.isArray(cond.value) || !cond.value.length) {
this.throwBadRequestException(`Invalid column '${cond.field}' value`)
}
str = `${field} IN (:...${param})`
break
case 'notin':
/* istanbul ignore if */
if (!Array.isArray(cond.value) || !cond.value.length) {
this.throwBadRequestException(`Invalid column '${cond.field}' value`)
}
str = `${field} NOT IN (:...${param})`
break
case 'isnull':
str = `${field} IS NULL`
params = {}
break
case 'notnull':
str = `${field} IS NOT NULL`
params = {}
break
case 'between':
/* istanbul ignore if */
if (!Array.isArray(cond.value) || !cond.value.length || cond.value.length !== 2) {
this.throwBadRequestException(`Invalid column '${cond.field}' value`)
}
str = `${field} BETWEEN :${param}0 AND :${param}1`
params = {
[`${param}0`]: cond.value[0],
[`${param}1`]: cond.value[1],
}
break
/* istanbul ignore next */
default:
str = `${field} = :${param}`
break
}
if (typeof params === 'undefined') {
params = { [param]: cond.value }
}
return { str, params }
} It's based on https://github.com/nestjsx/crud/blob/master/packages/crud-typeorm/src/typeorm-crud.service.ts#L615 |
@jakubszalaty really appretiate your workaround, but still I'm expecting a much better solution. |
…sitive db queries fix nestjsx#77, nestjsx#212
now you can use
|
Is the community still relying on this workaround, or has a proper fix been found for comparing strings case insensitively? |
Seems like a common requirement, if it's not present already, do you think it would be hard to add?
The text was updated successfully, but these errors were encountered: