-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathindex.ts
95 lines (88 loc) · 3.14 KB
/
index.ts
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
85
86
87
88
89
90
91
92
93
94
95
import { promises as fs ,existsSync} from "fs";
import {createIfNot} from "./utils/fileUtils.js"
import * as path from "node:path"
async function writeSQL(statement: string, saveFileAs = "", isAppend: boolean = false) {
try {
const destinationFile = process.argv[2] || saveFileAs;
if (!destinationFile) {
throw new Error("Missing saveFileAs parameter");
}
createIfNot(path.resolve(`./sql/${destinationFile}.sql`))
if(isAppend){
await fs.appendFile(`sql/${process.argv[2]}.sql`, statement);
}else{
await fs.writeFile(`sql/${process.argv[2]}.sql`, statement);
}
} catch (err) {
console.log(err);
}
}
async function readCSV(csvFileName = "", batchSize: number = 0) {
try {
const fileAndTableName = process.argv[2] || csvFileName;
batchSize = parseInt(process.argv[3]) || batchSize || 500;
let isAppend: boolean = false;
if (!fileAndTableName) {
throw new Error("Missing csvFileName parameter");
}
if(!existsSync(path.resolve(`./csv/${fileAndTableName}.csv`))){
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, index) => {
// Parses each line of CSV into field values array
const arr = line.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
if (arr.length > columnNames.length) {
console.log(arr);
throw new Error("Too Many Values in row");
} else if (arr.length < columnNames.length) {
console.log(arr);
throw new Error("Too Few Values in row");
}
// Check batch size (rows per batch)
if(index > 0 && index % batchSize == 0){
values = values.slice(0, -2) + ";\n\n";
const sqlStatement = beginSQLInsert + values;
// Write File
writeSQL(sqlStatement, fileAndTableName, isAppend);
values = "";
isAppend = true;
}
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, fileAndTableName, isAppend);
} catch (err) {
console.log(err);
}
}
readCSV();
console.log("Finished!");