Skip to content

Commit

Permalink
fix: fix ILIKE filters (contains, notContains, startsWith, notStartsW…
Browse files Browse the repository at this point in the history
…ith, endsWith, notEndsWith)
  • Loading branch information
retro committed May 7, 2024
1 parent edb0ff8 commit 5dec565
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 12 deletions.
156 changes: 156 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,162 @@ await describe("semantic layer", async () => {

assert.deepEqual(parsedQuery, query);
});

await it("can filter by contains", async () => {
const query = queryBuilder.buildQuery({
members: ["artists.name"],
filters: [
{
operator: "contains",
member: "artists.name",
value: ["ac"],
},
],
order: [{ member: "artists.name", direction: "asc" }],
limit: 1,
});

const result = await client.query<InferSqlQueryResultType<typeof query>>(
query.sql,
query.bindings,
);

assert.deepEqual(result.rows, [
{
artists___name: "AC/DC",
},
]);
});

await it("can filter by notContains", async () => {
const query = queryBuilder.buildQuery({
members: ["artists.name"],
filters: [
{
operator: "notContains",
member: "artists.name",
value: ["cor"],
},
],
order: [{ member: "artists.name", direction: "asc" }],
limit: 1,
});

const result = await client.query<InferSqlQueryResultType<typeof query>>(
query.sql,
query.bindings,
);

assert.deepEqual(result.rows, [
{
artists___name: "AC/DC",
},
]);
});

await it("can filter by startsWith", async () => {
const query = queryBuilder.buildQuery({
members: ["artists.name"],
filters: [
{
operator: "startsWith",
member: "artists.name",
value: ["ac"],
},
],
order: [{ member: "artists.name", direction: "asc" }],
limit: 1,
});

const result = await client.query<InferSqlQueryResultType<typeof query>>(
query.sql,
query.bindings,
);

assert.deepEqual(result.rows, [
{
artists___name: "AC/DC",
},
]);
});

await it("can filter by notStartsWith", async () => {
const query = queryBuilder.buildQuery({
members: ["artists.name"],
filters: [
{
operator: "notStartsWith",
member: "artists.name",
value: ["a cor"],
},
],
order: [{ member: "artists.name", direction: "asc" }],
limit: 1,
});

const result = await client.query<InferSqlQueryResultType<typeof query>>(
query.sql,
query.bindings,
);

assert.deepEqual(result.rows, [
{
artists___name: "AC/DC",
},
]);
});

await it("can filter by endsWith", async () => {
const query = queryBuilder.buildQuery({
members: ["artists.name"],
filters: [
{
operator: "endsWith",
member: "artists.name",
value: ["dc"],
},
],
order: [{ member: "artists.name", direction: "asc" }],
limit: 1,
});

const result = await client.query<InferSqlQueryResultType<typeof query>>(
query.sql,
query.bindings,
);

assert.deepEqual(result.rows, [
{
artists___name: "AC/DC",
},
]);
});

await it("can filter by notEndsWith", async () => {
const query = queryBuilder.buildQuery({
members: ["artists.name"],
filters: [
{
operator: "notEndsWith",
member: "artists.name",
value: ["som"],
},
],
order: [{ member: "artists.name", direction: "asc" }],
limit: 1,
});

const result = await client.query<InferSqlQueryResultType<typeof query>>(
query.sql,
query.bindings,
);

assert.deepEqual(result.rows, [
{
artists___name: "AC/DC",
},
]);
});
});

describe("repository with context", async () => {
Expand Down
24 changes: 12 additions & 12 deletions src/lib/query-builder/filter-builder/ilike-filter-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const DOCUMENTATION = {

function makeILikeFilterBuilder<T extends keyof typeof DOCUMENTATION>(
name: T,
startsWith: boolean,
endsWith: boolean,
beginWithWildcard: boolean,
endWithWildcard: boolean,
negation: boolean,
connective: "and" | "or",
) {
Expand All @@ -35,8 +35,8 @@ function makeILikeFilterBuilder<T extends keyof typeof DOCUMENTATION>(
(acc, value) => {
acc.sqls.push(
filterBuilder.queryBuilder.dialect.ilike(
startsWith,
endsWith,
beginWithWildcard,
endWithWildcard,
negation,
member.sql,
),
Expand All @@ -57,43 +57,43 @@ function makeILikeFilterBuilder<T extends keyof typeof DOCUMENTATION>(

export const contains = makeILikeFilterBuilder(
"contains" as const,
false,
false,
true,
true,
false,
"or",
);
export const notContains = makeILikeFilterBuilder(
"notContains" as const,
false,
false,
true,
true,
true,
"and",
);
export const startsWith = makeILikeFilterBuilder(
"startsWith" as const,
true,
false,
true,
false,
"or",
);
export const notStartsWith = makeILikeFilterBuilder(
"notStartsWith" as const,
true,
false,
true,
true,
"and",
);
export const endsWith = makeILikeFilterBuilder(
"endsWith" as const,
false,
true,
false,
false,
"or",
);
export const notEndsWith = makeILikeFilterBuilder(
"notEndsWith" as const,
false,
true,
false,
true,
"and",
);

0 comments on commit 5dec565

Please sign in to comment.