forked from gitdagray/csv-to-sql-insert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (74 loc) · 2.48 KB
/
index.js
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
import fs from "node:fs/promises"
import { existsSync } from "node:fs"
import { createIfNot } from "./utils/fileUtils.mjs"
import path from "node:path"
async function writeSQL(statement, saveFileAs = "") {
try {
const destinationFile = process.argv[2] || saveFileAs
if (!destinationFile) {
throw new Error("Missing saveFileAs parameter")
}
createIfNot(path.resolve(`./sql/${destinationFile}`))
await fs.writeFile(`sql/${process.argv[2]}.sql`, statement)
} catch (err) {
console.log(err)
}
}
async function readCSV(csvFileName = "") {
try {
const fileAndTableName = process.argv[2] || csvFileName
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 => {
// 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")
}
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)
} catch (err) {
console.log(err)
}
}
readCSV()
console.log("Finished!")