From 60d58034dd3afe48da503888551fd76fb76c76c2 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 13 Jun 2023 16:11:22 +0100 Subject: [PATCH] Fixes #6346 - issue with date parsing, adding in some testing around it. --- packages/server/src/integrations/mysql.ts | 9 +++++++-- .../src/integrations/tests/mysql.spec.ts | 20 ++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/server/src/integrations/mysql.ts b/packages/server/src/integrations/mysql.ts index 0dbd628ea3..c3bb5e066f 100644 --- a/packages/server/src/integrations/mysql.ts +++ b/packages/server/src/integrations/mysql.ts @@ -91,7 +91,7 @@ const SCHEMA: Integration = { }, } -function bindingTypeCoerce(bindings: any[]) { +export function bindingTypeCoerce(bindings: any[]) { for (let i = 0; i < bindings.length; i++) { const binding = bindings[i] if (typeof binding !== "string") { @@ -109,7 +109,12 @@ function bindingTypeCoerce(bindings: any[]) { dayjs(binding).isValid() && !binding.includes(",") ) { - bindings[i] = dayjs(binding).toDate() + let value: any + value = new Date(binding) + if (isNaN(value)) { + value = binding + } + bindings[i] = value } } return bindings diff --git a/packages/server/src/integrations/tests/mysql.spec.ts b/packages/server/src/integrations/tests/mysql.spec.ts index c4a26beb60..e4a496d80d 100644 --- a/packages/server/src/integrations/tests/mysql.spec.ts +++ b/packages/server/src/integrations/tests/mysql.spec.ts @@ -1,4 +1,4 @@ -import { default as MySQLIntegration } from "../mysql" +import { default as MySQLIntegration, bindingTypeCoerce } from "../mysql" jest.mock("mysql2") class TestConfiguration { @@ -131,3 +131,21 @@ describe("MySQL Integration", () => { }) }) }) + +describe("bindingTypeCoercion", () => { + it("shouldn't coerce something that looks like a date", () => { + const response = bindingTypeCoerce(["202205-1500"]) + expect(response[0]).toBe("202205-1500") + }) + + it("should coerce an actual date", () => { + const date = new Date("2023-06-13T14:24:22.620Z") + const response = bindingTypeCoerce(["2023-06-13T14:24:22.620Z"]) + expect(response[0]).toEqual(date) + }) + + it("should coerce numbers", () => { + const response = bindingTypeCoerce(["0"]) + expect(response[0]).toEqual(0) + }) +})