Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed logic on addDataType function to set non-string values #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ typings/
# dotenv environment variables file
.env

example.js
# vscode launch configuration
.vscode

example.js
64 changes: 55 additions & 9 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,49 @@ const readline = require("readline"),

let s3Metadata = null;

let unflatten = function(data) {
"use strict";
if (Object(data) !== data || Array.isArray(data))
return data;
var regex = /\.?([^.\[\]]+)|\[(\d+)\]/g,
resultholder = {};
for (var p in data) {
var cur = resultholder,
prop = "",
m;
while (m = regex.exec(p)) {
cur = cur[prop] || (cur[prop] = (m[2] ? [] : {}));
prop = m[2] || m[1];
}
cur[prop] = data[p];
}
return resultholder[""] || resultholder;
};

let flatten = function(data) {
var result = {};
function recurse (cur, prop) {
if (Object(cur) !== cur) {
result[prop] = cur;
} else if (Array.isArray(cur)) {
for(var i=0, l=cur.length; i<l; i++)
recurse(cur[i], prop + "[" + i + "]");
if (l == 0)
result[prop] = [];
} else {
var isEmpty = true;
for (var p in cur) {
isEmpty = false;
recurse(cur[p], prop ? prop+"."+p : p);
}
if (isEmpty && prop)
result[prop] = {};
}
}
recurse(data, "");
return result;
}

function startQueryExecution(query, config) {
const QueryString = query.sql || query;

Expand Down Expand Up @@ -152,30 +195,33 @@ async function cleanUpDML(input, ignoreEmpty) {

function addDataType(input, dataTypes) {
let updatedObjectWithDataType = {};

for (const key in input) {
const flat = flatten(input)
for (const key in flat) {
switch (dataTypes[key]) {
case "varchar":
updatedObjectWithDataType[key] = input[key];
updatedObjectWithDataType[key] = flat[key];
break;
case "boolean":
updatedObjectWithDataType[key] = JSON.parse(
input[key].toLowerCase()
);
if (flat[key]) {
updatedObjectWithDataType[key] = JSON.parse(
flat[key].toLowerCase()
);
}
break;
case "integer":
case "tinyint":
case "smallint":
case "int":
case "float":
case "double":
updatedObjectWithDataType[key] = Number(input[key]);
updatedObjectWithDataType[key] = Number(flat[key]);
break;
default:
updatedObjectWithDataType[key] = input[key];
updatedObjectWithDataType[key] = flat[key];
}
}
return updatedObjectWithDataType;
const result = unflatten(updatedObjectWithDataType)
return result;
}

function cleanUpNonDML(input) {
Expand Down