-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
ILIKE find operator #4418
Comments
Performing case insensitive operations right now is pretty counterintuitive, this is a needed feature IMO |
If you're using postgres, I've got a solution to have a custom operator: import { Connection, FindOperator, FindOperatorType } from 'typeorm';
class FindOperatorWithExtras<T> extends FindOperator<T> {
constructor(
type: FindOperatorType | 'ilike',
value: FindOperator<T> | T,
useParameter?: boolean,
multipleParameters?: boolean,
) {
// @ts-ignore
super(type, value, useParameter, multipleParameters);
}
public toSql(
connection: Connection,
aliasPath: string,
parameters: string[],
): string {
// @ts-ignore
if (this._type === 'ilike') {
return `${aliasPath} ILIKE ${parameters[0]}`;
}
return super.toSql(connection, aliasPath, parameters);
}
}
/**
* Find Options Operator.
* Example: { someField: Like("%some sting%") }
*/
export function ILike<T>(
value: T | FindOperator<T>,
): FindOperatorWithExtras<T> {
return new FindOperatorWithExtras('ilike', value);
} I would suggest opening the |
I used import { Raw } from 'typeorm';
const name = 'hello';
const books = await this.booksRepository.find({
where: {
name: Raw(alias => `${alias} ILIKE '%${name}%'`),
},
}); But there's of course SQL Injection problem that comes with this. |
This looks great. Does it piggyback on all of the sql-injection preventive measures of TypeORM? |
@noamgat I feel like it is since it's using the params array unlike the 'raw' operator which uses the value directly. typeorm/src/find-options/FindOperator.ts Lines 87 to 121 in abb2dd3
|
Oh, we had this feature on |
@pleerock could that mean we have built in ILIKE in the next release? |
yes, if PR will be finished and merged anytime soon. |
I'm also looking for this, but it should fallback for Also, I agree that FindOperator should be public so we have less hacky, more extensible code. |
This is live now. |
@imnotjames Just wondering what you meant by "live," as it does not appear to be noted in It is in |
@kendallroth I think the latest release isn't exactly as master, this happened before. The problem with the current implementation it only works with PostgreSQL. My personal take on this: ...
case "ilike":
if (this.connection.driver instanceof PostgresqlDriver) {
return `${aliasPath} ILIKE ${parameters[0]}`;
}
return `UPPER(${aliasPath}) LIKE UPPER(${parameters[0]})`; |
@leonardofalk That's what I was understanding as well, was just curious what "is live" meant (unless that referred to just being on
👍 I use PostgresSQL anyway, but does this mean it is why it hasn't been released to |
You're right - I thought we had done a release but we hadn't. Apologies on that. |
Also - should you want to get support for drivers / dialects that don't support |
Hey, possibly stupid question but I am a little lost between next, master and released and couldn't find anything on this in the changelog (searched for "case-insensitive", "case insensitive" and "ilike"). Since the operator suggested above doesn't work in TypeScript anymore ( I also couldn't find a PR that contained more detail in this thread nor does this seem to be in the docs anywhere yet. Is there an ETA for this? |
can someone specify the version this is fixed in? |
I think you can now do the following instead of using the code in this issue: return this.userRepository.findOne({ email: new FindOperator('ilike', userEmail) }); |
This answers @geisterfurz007 's question and fixes my issue as well. TL;DR Custom case-insensitive like operator proposed by @wafs no longer works after updating TypeORM to 0.2.29. Please see above comment to have this fixed. Thanks @noamgat! |
Does indeed; thanks for the mention @metelski! |
Seems like MySQL string comparisons are case-insensitive by default. For that driver the 'like' operand is fine |
this can be further simplified as:
For searching it's useful to do something like:
|
This is a great update. But how could I use it in query builder? I have a searchUser function searches user with given
If there's quote How can I re-write above query with |
Does the |
@nebkat |
repository.find({
name: ILike("John%")
}); Just import |
Is it safe to use user input as input to the ILike Operator? Would something like this repository.find({
name: ILike(`%${unsafeUserInput}%`)
}); be vulnerable to sql injection attacks? Update: |
See this solution for alternative to custom ILike: typeorm/typeorm#4418 (comment)
Hi, This solution to create a custom operator seems to no longer work with the v0.3 I want to create another kind of operator. Do you have any solution? |
Issue type:
[ ] question
[ ] bug report
[x] feature request
[ ] documentation issue
Database system/driver:
[ ]
cordova
[ ]
mongodb
[ ]
mssql
[ ]
mysql
/mariadb
[ ]
oracle
[x]
postgres
[ ]
cockroachdb
[ ]
sqlite
[ ]
sqljs
[ ]
react-native
[ ]
expo
TypeORM version:
[x]
latest
[ ]
@next
[ ]
0.x.x
(or put your version here)Would it be a big hassle to include ILIKE find operator for case insensitive string query? I see that LIKE is implemented, ILIKE would be a nice touch
The text was updated successfully, but these errors were encountered: