From dc32c90aaa66715b9ee6b6449c67cf9078a33883 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 12 Jul 2021 10:51:30 +0100 Subject: [PATCH] Fixing some issues discovered with the new SQL connectors were the input type would always be string (causing some SQL types to break) - parsing these before input to attempt to fix this problem - issue referenced in #1943. --- .../scripts/integrations/mysql/init.sql | 3 ++- packages/server/src/integrations/base/sql.ts | 25 ++++++++++++++++--- packages/server/src/integrations/utils.ts | 8 ++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packages/server/scripts/integrations/mysql/init.sql b/packages/server/scripts/integrations/mysql/init.sql index 8cd120a8a3..efcef00899 100644 --- a/packages/server/scripts/integrations/mysql/init.sql +++ b/packages/server/scripts/integrations/mysql/init.sql @@ -2,6 +2,7 @@ CREATE DATABASE IF NOT EXISTS main; USE main; CREATE TABLE Persons ( PersonID int NOT NULL AUTO_INCREMENT, + CreatedAt datetime, LastName varchar(255), FirstName varchar(255), Address varchar(255), @@ -17,6 +18,6 @@ CREATE TABLE Tasks ( FOREIGN KEY(PersonID) REFERENCES Persons(PersonID) ); -INSERT INTO Persons (FirstName, LastName, Address, City) VALUES ('Mike', 'Hughes', '123 Fake Street', 'Belfast'); +INSERT INTO Persons (FirstName, LastName, Address, City, CreatedAt) VALUES ('Mike', 'Hughes', '123 Fake Street', 'Belfast', '2021-01-19 03:14:07'); INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'assembling'); INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'processing'); diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index e1c065dd26..66dd269412 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -8,9 +8,24 @@ import { Operation, RelationshipsJson, } from "../../definitions/datasource" +import { isIsoDateString } from "../utils" type KnexQuery = Knex.QueryBuilder | Knex +function parseBody(body: any) { + for (let [key, value] of Object.entries(body)) { + if (typeof value !== "string") { + continue + } + if (isIsoDateString(value)) { + body[key] = new Date(value) + } else if (!isNaN(parseFloat(value))) { + body[key] = parseFloat(value) + } + } + return body +} + // right now we only do filters on the specific table being queried function addFilters( tableName: string, @@ -119,11 +134,12 @@ function buildCreate( ): KnexQuery { const { endpoint, body } = json let query: KnexQuery = knex(endpoint.entityId) + const parsedBody = parseBody(body) // mysql can't use returning if (opts.disableReturning) { - return query.insert(body) + return query.insert(parsedBody) } else { - return query.insert(body).returning("*") + return query.insert(parsedBody).returning("*") } } @@ -173,12 +189,13 @@ function buildUpdate( ): KnexQuery { const { endpoint, body, filters } = json let query: KnexQuery = knex(endpoint.entityId) + const parsedBody = parseBody(body) query = addFilters(endpoint.entityId, query, filters) // mysql can't use returning if (opts.disableReturning) { - return query.update(body) + return query.update(parsedBody) } else { - return query.update(body).returning("*") + return query.update(parsedBody).returning("*") } } diff --git a/packages/server/src/integrations/utils.ts b/packages/server/src/integrations/utils.ts index d0af0e99a9..03751bb467 100644 --- a/packages/server/src/integrations/utils.ts +++ b/packages/server/src/integrations/utils.ts @@ -68,3 +68,11 @@ export function isSQL(datasource: Datasource): boolean { const SQL = [SourceNames.POSTGRES, SourceNames.SQL_SERVER, SourceNames.MYSQL] return SQL.indexOf(datasource.source) !== -1 } + +export function isIsoDateString(str: string) { + if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str)) { + return false + } + let d = new Date(str) + return d.toISOString() === str +}