From 0b7919cd3d8029a572e59e049af6b0fbdc2b7bb1 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 10 Mar 2022 10:18:03 +0000 Subject: [PATCH 1/2] Update date filtering to only use a between statement when 2 dates are provided --- packages/server/src/integrations/base/sql.ts | 23 ++++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index ffa405f016..f4ebc02098 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -27,11 +27,8 @@ function parse(input: any) { if (typeof input !== "string") { return input } - if (input === MAX_ISO_DATE) { - return new Date(8640000000000000) - } - if (input === MIN_ISO_DATE) { - return new Date(-8640000000000000) + if (input === MAX_ISO_DATE || input === MIN_ISO_DATE) { + return null } if (isIsoDateString(input)) { return new Date(input) @@ -130,11 +127,19 @@ class InternalBuilder { } if (filters.range) { iterate(filters.range, (key, value) => { - if (!value.high || !value.low) { - return + if (value.low && value.high) { + // 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) { From 1c104d6fa580306d15468c9ca9966b2ec3f9b3d2 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 15 Mar 2022 11:52:54 +0000 Subject: [PATCH 2/2] Add test cases for SQL range filtering changes --- .../server/src/integrations/tests/sql.spec.js | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/packages/server/src/integrations/tests/sql.spec.js b/packages/server/src/integrations/tests/sql.spec.js index 6ef82f0113..c2e65c56b7 100644 --- a/packages/server/src/integrations/tests/sql.spec.js +++ b/packages/server/src/integrations/tests/sql.spec.js @@ -187,4 +187,55 @@ describe("SQL query builder", () => { 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}"` + }) + }) })