Skip to content

Commit

Permalink
feat: backport ilike from next
Browse files Browse the repository at this point in the history
the next branch has support for ilike and this backports
the FindOperator into the main branch
  • Loading branch information
imnotjames committed Oct 7, 2020
1 parent 2d9a3ce commit 7dd5426
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/find-options/FindOperatorType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export type FindOperatorType = "not"
| "in"
| "any"
| "isNull"
| "ilike"
| "like"
| "raw";
9 changes: 9 additions & 0 deletions src/find-options/operator/ILike.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {FindOperator} from "../FindOperator";

/**
* Find Options Operator.
* Example: { someField: ILike("%SOME string%") }
*/
export function ILike<T>(value: T|FindOperator<T>) {
return new FindOperator("ilike", value);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export * from "./find-options/operator/In";
export * from "./find-options/operator/IsNull";
export * from "./find-options/operator/LessThan";
export * from "./find-options/operator/LessThanOrEqual";
export * from "./find-options/operator/ILike";
export * from "./find-options/operator/Like";
export * from "./find-options/operator/MoreThan";
export * from "./find-options/operator/MoreThanOrEqual";
Expand Down
2 changes: 2 additions & 0 deletions src/query-builder/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,8 @@ export abstract class QueryBuilder<Entity> {
return `${aliasPath} >= ${parameters[0]}`;
case "equal":
return `${aliasPath} = ${parameters[0]}`;
case "ilike":
return `${aliasPath} ILIKE ${parameters[0]}`;
case "like":
return `${aliasPath} LIKE ${parameters[0]}`;
case "between":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Between,
Connection,
Equal,
ILike,
In,
IsNull,
LessThan,
Expand All @@ -19,6 +20,8 @@ import {PostgresDriver} from "../../../../src/driver/postgres/PostgresDriver";
import {Raw} from "../../../../src/find-options/operator/Raw";
import {PersonAR} from "./entity/PersonAR";
import {expect} from "chai";
import {OracleDriver} from "../../../../src/driver/oracle/OracleDriver";
import {AbstractSqliteDriver} from "../../../../src/driver/sqlite-abstract/AbstractSqliteDriver";

describe("repository > find options > operators", () => {

Expand Down Expand Up @@ -271,6 +274,50 @@ describe("repository > find options > operators", () => {

})));

it("ilike", () => Promise.all(connections.map(async connection => {
if (!(connection.driver instanceof PostgresDriver))
return;

// insert some fake data
const post1 = new Post();
post1.title = "about #1";
post1.likes = 12;
await connection.manager.save(post1);
const post2 = new Post();
post2.title = "ABOUT #2";
post2.likes = 3;
await connection.manager.save(post2);

// check operator
const loadedPosts = await connection.getRepository(Post).find({
title: ILike("%out #%")
});
loadedPosts.should.be.eql([{ id: 1, likes: 12, title: "about #1" }, { id: 2, likes: 3, title: "ABOUT #2" }]);

})));

it("not(ilike)", () => Promise.all(connections.map(async connection => {
if (!(connection.driver instanceof PostgresDriver))
return;

// insert some fake data
const post1 = new Post();
post1.title = "about #1";
post1.likes = 12;
await connection.manager.save(post1);
const post2 = new Post();
post2.title = "ABOUT #2";
post2.likes = 3;
await connection.manager.save(post2);

// check operator
const loadedPosts = await connection.getRepository(Post).find({
title: Not(ILike("%out #1"))
});
loadedPosts.should.be.eql([{ id: 2, likes: 3, title: "ABOUT #2" }]);

})));

it("like", () => Promise.all(connections.map(async connection => {

// insert some fake data
Expand Down

0 comments on commit 7dd5426

Please sign in to comment.