-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (83 loc) · 3.21 KB
/
index.js
File metadata and controls
84 lines (83 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import fs from "node:fs/promises";
import { existsSync } from "node:fs";
import { createIfNot } from "./utils/fileUtils.mjs";
import { logEvents } from "./middleware/logEvents.js";
import path from "node:path";
async function writeSQL(statement, saveFileAs = "") {
try {
const destinationFile = process.argv[2] || saveFileAs;
if (!destinationFile) {
logEvents(`Missing saveFileAs parameter: ${destinationFile}`, "fileSystemError.txt");
throw new Error("Missing saveFileAs parameter");
}
createIfNot(path.resolve(`./sql/${destinationFile}`));
await fs.writeFile(`sql/${process.argv[2]}.sql`, statement);
} catch (err) {
logEvents(`${err}`, "Error.txt");
console.log(err);
}
}
async function readCSV(csvFileName = "") {
try {
const fileAndTableName = process.argv[2] || csvFileName;
if (!fileAndTableName) {
logEvents(`Missing csvFileName parameter: ${fileAndTableName}`, "fileSystemError.txt");
throw new Error("Missing csvFileName parameter");
}
if (!existsSync(path.resolve(`./csv/${fileAndTableName}.csv`))) {
logEvents(`File not found: ./csv${fileAndTableName}`, "fileSystemError.txt");
console.log("file not found");
return;
}
const data = await fs.readFile(`csv/${fileAndTableName}.csv`, {
encoding: "utf8",
});
const linesArray = data.split(/\r|\n/).filter(line => line);
const columnNames = linesArray.shift().split(",");
let beginSQLInsert = `INSERT INTO ${fileAndTableName} (`;
columnNames.forEach(name => (beginSQLInsert += `${name}, `));
beginSQLInsert = beginSQLInsert.slice(0, -2) + ")\nVALUES\n";
let values = "";
linesArray.forEach(line => {
// Parses each line of CSV into field values array
const arr = line.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
if (arr.length > columnNames.length) {
console.log(arr);
logEvents(`Too Many Values in row ${arr.length} should be equal to ${columnNames.length}`, "fileSystemError.txt");
throw new Error("Too Many Values in row")
} else if (arr.length < columnNames.length) {
logEvents(`Too Few Values in row ${arr.length} should be equal to ${columnNames.length}`, "fileSystemError.txt");
console.log(arr)
throw new Error("Too Few Values in row")
}
let valueLine = "\t("
arr.forEach(value => {
// Matches NULL values, Numbers,
// Strings accepted as numbers, and Booleans (0 or 1)
if (value === "NULL" || !isNaN(+value)) {
valueLine += `${value}, `
} else {
// If a string is wrapped in quotes, it doesn't need more
if (value.at(0) === '"') valueLine += `${value}, `
else {
// This wraps strings in quotes
// also wraps timestamps
valueLine += `"${value}", `;
}
}
});
valueLine = valueLine.slice(0, -2) + "),\n";
values += valueLine;
});
values = values.slice(0, -2) + ";";
const sqlStatement = beginSQLInsert + values;
// Write File
writeSQL(sqlStatement);
logEvents("Process Finished Successfully", "Success.txt");
} catch (err) {
logEvents(`${err}`, "Error.txt");
console.log(err);
}
}
readCSV();
console.log("Finished!");