Merge pull request #4871 from Budibase/fix/date-filtering
Date filtering logic
This commit is contained in:
commit
dd1863d4c9
2 changed files with 65 additions and 9 deletions
|
@ -27,11 +27,8 @@ function parse(input: any) {
|
||||||
if (typeof input !== "string") {
|
if (typeof input !== "string") {
|
||||||
return input
|
return input
|
||||||
}
|
}
|
||||||
if (input === MAX_ISO_DATE) {
|
if (input === MAX_ISO_DATE || input === MIN_ISO_DATE) {
|
||||||
return new Date(8640000000000000)
|
return null
|
||||||
}
|
|
||||||
if (input === MIN_ISO_DATE) {
|
|
||||||
return new Date(-8640000000000000)
|
|
||||||
}
|
}
|
||||||
if (isIsoDateString(input)) {
|
if (isIsoDateString(input)) {
|
||||||
return new Date(input)
|
return new Date(input)
|
||||||
|
@ -130,11 +127,19 @@ class InternalBuilder {
|
||||||
}
|
}
|
||||||
if (filters.range) {
|
if (filters.range) {
|
||||||
iterate(filters.range, (key, value) => {
|
iterate(filters.range, (key, value) => {
|
||||||
if (!value.high || !value.low) {
|
if (value.low && value.high) {
|
||||||
return
|
// Use a between operator if we have 2 valid range values
|
||||||
|
const fnc = allOr ? "orWhereBetween" : "whereBetween"
|
||||||
|
query = query[fnc](key, [value.low, value.high])
|
||||||
|
} else if (value.low) {
|
||||||
|
// Use just a single greater than operator if we only have a low
|
||||||
|
const fnc = allOr ? "orWhere" : "where"
|
||||||
|
query = query[fnc](key, ">", value.low)
|
||||||
|
} else if (value.high) {
|
||||||
|
// Use just a single less than operator if we only have a high
|
||||||
|
const fnc = allOr ? "orWhere" : "where"
|
||||||
|
query = query[fnc](key, "<", value.high)
|
||||||
}
|
}
|
||||||
const fnc = allOr ? "orWhereBetween" : "whereBetween"
|
|
||||||
query = query[fnc](key, [value.low, value.high])
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (filters.equal) {
|
if (filters.equal) {
|
||||||
|
|
|
@ -187,4 +187,55 @@ describe("SQL query builder", () => {
|
||||||
sql: `select * from (select * from \`${TABLE_NAME}\` limit ?) as \`${TABLE_NAME}\``
|
sql: `select * from (select * from \`${TABLE_NAME}\` limit ?) as \`${TABLE_NAME}\``
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should use greater than when only low range specified", () => {
|
||||||
|
const date = new Date()
|
||||||
|
const query = sql._query(generateReadJson({
|
||||||
|
filters: {
|
||||||
|
range: {
|
||||||
|
property: {
|
||||||
|
low: date,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
expect(query).toEqual({
|
||||||
|
bindings: [date, limit],
|
||||||
|
sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."property" > $1 limit $2) as "${TABLE_NAME}"`
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should use less than when only high range specified", () => {
|
||||||
|
const date = new Date()
|
||||||
|
const query = sql._query(generateReadJson({
|
||||||
|
filters: {
|
||||||
|
range: {
|
||||||
|
property: {
|
||||||
|
high: date,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
expect(query).toEqual({
|
||||||
|
bindings: [date, limit],
|
||||||
|
sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."property" < $1 limit $2) as "${TABLE_NAME}"`
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should use greater than when only low range specified", () => {
|
||||||
|
const date = new Date()
|
||||||
|
const query = sql._query(generateReadJson({
|
||||||
|
filters: {
|
||||||
|
range: {
|
||||||
|
property: {
|
||||||
|
low: date,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
expect(query).toEqual({
|
||||||
|
bindings: [date, limit],
|
||||||
|
sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."property" > $1 limit $2) as "${TABLE_NAME}"`
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue