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.

This commit is contained in:
mike12345567 2021-07-12 10:51:30 +01:00
parent bebfe4c6e1
commit 184df6c685
3 changed files with 31 additions and 5 deletions

View File

@ -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');

View File

@ -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("*")
}
}

View File

@ -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
}