diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 45e550a..52eb165 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -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>( + 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>( + 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>( + 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>( + 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>( + 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>( + query.sql, + query.bindings, + ); + + assert.deepEqual(result.rows, [ + { + artists___name: "AC/DC", + }, + ]); + }); }); describe("repository with context", async () => { diff --git a/src/lib/query-builder/filter-builder/ilike-filter-builder.ts b/src/lib/query-builder/filter-builder/ilike-filter-builder.ts index d7da74d..2571617 100644 --- a/src/lib/query-builder/filter-builder/ilike-filter-builder.ts +++ b/src/lib/query-builder/filter-builder/ilike-filter-builder.ts @@ -18,8 +18,8 @@ const DOCUMENTATION = { function makeILikeFilterBuilder( name: T, - startsWith: boolean, - endsWith: boolean, + beginWithWildcard: boolean, + endWithWildcard: boolean, negation: boolean, connective: "and" | "or", ) { @@ -35,8 +35,8 @@ function makeILikeFilterBuilder( (acc, value) => { acc.sqls.push( filterBuilder.queryBuilder.dialect.ilike( - startsWith, - endsWith, + beginWithWildcard, + endWithWildcard, negation, member.sql, ), @@ -57,43 +57,43 @@ function makeILikeFilterBuilder( 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", );