Browse Source

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.

gh-pages
mike12345567 5 years ago
parent
commit
dc32c90aaa
  1. 3
      packages/server/scripts/integrations/mysql/init.sql
  2. 25
      packages/server/src/integrations/base/sql.ts
  3. 8
      packages/server/src/integrations/utils.ts

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

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

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

Loading…
Cancel
Save