Skip to content

Commit

Permalink
Add support for ILIKE operator (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
chr1sjf0x committed Aug 7, 2020
1 parent 6f421ef commit 083bb83
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/integration-tests/src/EntityManager.queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,14 @@ describe("EntityManager.queries", () => {
expect(authors.length).toEqual(2);
});

it("can find by ilike", async () => {
await insertAuthor({ first_name: "a1", age: 1 });
await insertAuthor({ first_name: "a2", age: 2 });
const em = newEntityManager();
const authors = await em.find(Author, { firstName: { ilike: "A%" } });
expect(authors.length).toEqual(2);
});

it("can find by like and join with not equal enum", async () => {
await insertPublisher({ name: "p1", size_id: 1 });
await insertPublisher({ name: "p2", size_id: 2 });
Expand Down
7 changes: 5 additions & 2 deletions packages/orm/src/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export type ValueFilter<V, N> =
| { ne: V | N }
| { lt: V }
| { lte: V }
| { like: V };
| { like: V }
| { ilike: V };

// For filtering by a foreign key T, i.e. either joining/recursing into with FilterQuery<T>, or matching it is null/not null/etc.
export type EntityFilter<T, I, F, N> = T | I | I[] | F | N | { ne: T | I | N };
Expand All @@ -49,6 +50,7 @@ export type ValueGraphQLFilter<V> =
lt?: V | null;
lte?: V | null;
like?: V | null;
ilike?: V | null;
}
| { op: Operator; value: Primitive }
| V
Expand All @@ -60,7 +62,7 @@ export type EnumGraphQLFilter<V> = V[] | null | undefined;
/** A GraphQL version of EntityFilter. */
export type EntityGraphQLFilter<T, I, F> = T | I | I[] | F | { ne: T | I } | null | undefined;

const operators = ["eq", "gt", "gte", "ne", "lt", "lte", "like", "in"] as const;
const operators = ["eq", "gt", "gte", "ne", "lt", "lte", "like", "ilike", "in"] as const;
export type Operator = typeof operators[number];
const opToFn: Record<Operator, string> = {
eq: "=",
Expand All @@ -70,6 +72,7 @@ const opToFn: Record<Operator, string> = {
lt: "<",
lte: "<=",
like: "LIKE",
ilike: "ILIKE",
in: "...",
};

Expand Down

0 comments on commit 083bb83

Please sign in to comment.