Skip to content

Commit

Permalink
Date filter freeze fix (Budibase#9635)
Browse files Browse the repository at this point in the history
* Filter with enrichedSchemaFields

* Handle empty object values in date range
  • Loading branch information
melohagan authored Feb 15, 2023
1 parent 783907b commit b14a071
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
}
const getSchema = filter => {
return schemaFields.find(field => field.name === filter.field)
return enrichedSchemaFields.find(field => field.name === filter.field)
}
const santizeTypes = filter => {
Expand Down
13 changes: 13 additions & 0 deletions packages/server/src/integrations/base/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@ class InternalBuilder {
}
if (filters.range) {
iterate(filters.range, (key, value) => {
const isEmptyObject = (val: any) => {
return (
val &&
Object.keys(val).length === 0 &&
Object.getPrototypeOf(val) === Object.prototype
)
}
if (isEmptyObject(value.low)) {
value.low = ""
}
if (isEmptyObject(value.high)) {
value.high = ""
}
if (value.low && value.high) {
// Use a between operator if we have 2 valid range values
const fnc = allOr ? "orWhereBetween" : "whereBetween"
Expand Down
38 changes: 38 additions & 0 deletions packages/server/src/integrations/tests/sql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,4 +553,42 @@ describe("SQL query builder", () => {
sql: `select * from (select top (@p0) * from [${tableName}] where LOWER([${tableName}].[name]) LIKE @p1) as [${tableName}]`,
})
})

it("should ignore high range value if it is an empty object", () => {
const query = sql._query(
generateReadJson({
filters: {
range: {
dob: {
low: "2000-01-01 00:00:00",
high: {},
},
},
},
})
)
expect(query).toEqual({
bindings: ["2000-01-01 00:00:00", 500],
sql: `select * from (select * from \"${TABLE_NAME}\" where \"${TABLE_NAME}\".\"dob\" > $1 limit $2) as \"${TABLE_NAME}\"`,
})
})

it("should ignore low range value if it is an empty object", () => {
const query = sql._query(
generateReadJson({
filters: {
range: {
dob: {
low: {},
high: "2010-01-01 00:00:00",
},
},
},
})
)
expect(query).toEqual({
bindings: ["2010-01-01 00:00:00", 500],
sql: `select * from (select * from \"${TABLE_NAME}\" where \"${TABLE_NAME}\".\"dob\" < $1 limit $2) as \"${TABLE_NAME}\"`,
})
})
})

0 comments on commit b14a071

Please sign in to comment.