Skip to content

Commit

Permalink
feat(mysql): add filter for lt and gt (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas authored Jun 10, 2024
1 parent 9cf51cf commit ae3c866
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/fuzzy-cars-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@codelytv/criteria-mysql": minor
"@codelytv/criteria": minor
---

allow filtering by grater and lower than
12 changes: 12 additions & 0 deletions packages/criteria-mysql/src/CriteriaToMySqlConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ export class CriteriaToMySqlConverter {
} else if (filter.operator.isNotEquals()) {
queryPart += "!= ?";
params.push(value);
} else if (filter.operator.isGreaterThan()) {
queryPart += "> ?";
params.push(value);
} else if (filter.operator.isGreaterThanOrEqual()) {
queryPart += ">= ?";
params.push(value);
} else if (filter.operator.isLowerThan()) {
queryPart += "< ?";
params.push(value);
} else if (filter.operator.isLowerThanOrEqual()) {
queryPart += "<= ?";
params.push(value);
} else {
queryPart += `${filter.operator.value} ?`;
params.push(value);
Expand Down
52 changes: 52 additions & 0 deletions packages/criteria-mysql/test/CriteriaToMySqlConverter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,58 @@ describe("CriteriaToMySqlConverter should", () => {
});
});

it("Generate select with one greater than filter", () => {
const actualQuery = converter.convert(
["id", "age"],
"users",
CriteriaMother.withOneFilter("age", "GREATER_THAN", "25"),
);

assert.deepEqual(actualQuery, {
query: "SELECT id, age FROM users WHERE age > ?;",
params: [25],
});
});

it("Generate select with one greater than or equal filter", () => {
const actualQuery = converter.convert(
["id", "age"],
"users",
CriteriaMother.withOneFilter("age", "GREATER_THAN_OR_EQUAL", "25"),
);

assert.deepEqual(actualQuery, {
query: "SELECT id, age FROM users WHERE age >= ?;",
params: [25],
});
});

it("Generate select with one lower than filter", () => {
const actualQuery = converter.convert(
["id", "age"],
"users",
CriteriaMother.withOneFilter("age", "LOWER_THAN", "18"),
);

assert.deepEqual(actualQuery, {
query: "SELECT id, age FROM users WHERE age < ?;",
params: [18],
});
});

it("Generate select with one lower than or equal filter", () => {
const actualQuery = converter.convert(
["id", "age"],
"users",
CriteriaMother.withOneFilter("age", "LOWER_THAN_OR_EQUAL", "18"),
);

assert.deepEqual(actualQuery, {
query: "SELECT id, age FROM users WHERE age <= ?;",
params: [18],
});
});

it("Generate select with one filter sorted", () => {
const actualQuery = converter.convert(
["id", "name"],
Expand Down
16 changes: 16 additions & 0 deletions packages/criteria/src/FilterOperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,20 @@ export class FilterOperator {
isNotEquals(): boolean {
return this.value.valueOf() === Operator.NOT_EQUAL.valueOf();
}

isGreaterThan(): boolean {
return this.value.valueOf() === Operator.GREATER_THAN.valueOf();
}

isGreaterThanOrEqual(): boolean {
return this.value.valueOf() === Operator.GREATER_THAN_OR_EQUAL.valueOf();
}

isLowerThan(): boolean {
return this.value.valueOf() === Operator.LOWER_THAN.valueOf();
}

isLowerThanOrEqual(): boolean {
return this.value.valueOf() === Operator.LOWER_THAN_OR_EQUAL.valueOf();
}
}

0 comments on commit ae3c866

Please sign in to comment.