Skip to content

Commit c17394b

Browse files
authored
Merge pull request #6 from YugandharrPatil/commonJS-to-ES6
commonJS syntax changed to ES6
2 parents 7282c50 + 168ce7d commit c17394b

File tree

2 files changed

+84
-86
lines changed

2 files changed

+84
-86
lines changed

index.js

Lines changed: 71 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,79 @@
1-
const fs = require('node:fs/promises');
1+
import fs from "node:fs/promises";
22

3-
async function writeSQL(statement, saveFileAs = '') {
4-
try {
5-
const destinationFile = process.argv[2] || saveFileAs
3+
async function writeSQL(statement, saveFileAs = "") {
4+
try {
5+
const destinationFile = process.argv[2] || saveFileAs;
66

7-
if (!destinationFile) {
8-
throw new Error("Missing saveFileAs parameter")
9-
}
7+
if (!destinationFile) {
8+
throw new Error("Missing saveFileAs parameter");
9+
}
1010

11-
await fs.writeFile(`sql/${process.argv[2]}.sql`, statement);
12-
} catch (err) {
13-
console.log(err);
14-
}
11+
await fs.writeFile(`sql/${process.argv[2]}.sql`, statement);
12+
} catch (err) {
13+
console.log(err);
14+
}
1515
}
1616

17-
async function readCSV(csvFileName = '') {
18-
try {
19-
20-
const fileAndTableName = process.argv[2] || csvFileName
21-
22-
if (!fileAndTableName) {
23-
throw new Error("Missing csvFileName parameter")
24-
}
25-
26-
const data = await fs.readFile(`csv/${fileAndTableName}.csv`, { encoding: 'utf8' });
27-
28-
const linesArray = data.split(/\r|\n/).filter(line => line)
29-
const columnNames = linesArray.shift().split(",")
30-
31-
let beginSQLInsert = `INSERT INTO ${fileAndTableName} (`
32-
columnNames.forEach(name => beginSQLInsert += `${name}, `)
33-
beginSQLInsert = beginSQLInsert.slice(0, -2) + ")\nVALUES\n"
34-
35-
let values = ''
36-
linesArray.forEach(line => {
37-
// Parses each line of CSV into field values array
38-
const arr = line.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/)
39-
40-
if (arr.length > columnNames.length) {
41-
console.log(arr)
42-
throw new Error("Too Many Values in row")
43-
} else if (arr.length < columnNames.length) {
44-
console.log(arr)
45-
throw new Error("Too Few Values in row")
46-
}
47-
48-
let valueLine = '\t('
49-
arr.forEach(value => {
50-
// Matches NULL values, Numbers,
51-
// Strings accepted as numbers, and Booleans (0 or 1)
52-
if (value === "NULL" || !isNaN(+value)) {
53-
valueLine += `${value}, `
54-
}
55-
else {
56-
// If a string is wrapped in quotes, it doesn't need more
57-
if (value.at(0) === '"') valueLine += `${value}, `
58-
else {
59-
// This wraps strings in quotes
60-
// also wraps timestamps
61-
valueLine += `"${value}", `
62-
}
63-
}
64-
})
65-
valueLine = valueLine.slice(0, -2) + "),\n"
66-
values += valueLine
67-
})
68-
values = values.slice(0, -2) + ";"
69-
70-
const sqlStatement = beginSQLInsert + values
71-
72-
// Write File
73-
writeSQL(sqlStatement)
74-
75-
} catch (err) {
76-
console.log(err);
77-
}
17+
async function readCSV(csvFileName = "") {
18+
try {
19+
const fileAndTableName = process.argv[2] || csvFileName;
20+
21+
if (!fileAndTableName) {
22+
throw new Error("Missing csvFileName parameter");
23+
}
24+
25+
const data = await fs.readFile(`csv/${fileAndTableName}.csv`, { encoding: "utf8" });
26+
27+
const linesArray = data.split(/\r|\n/).filter((line) => line);
28+
const columnNames = linesArray.shift().split(",");
29+
30+
let beginSQLInsert = `INSERT INTO ${fileAndTableName} (`;
31+
columnNames.forEach((name) => (beginSQLInsert += `${name}, `));
32+
beginSQLInsert = beginSQLInsert.slice(0, -2) + ")\nVALUES\n";
33+
34+
let values = "";
35+
linesArray.forEach((line) => {
36+
// Parses each line of CSV into field values array
37+
const arr = line.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
38+
39+
if (arr.length > columnNames.length) {
40+
console.log(arr);
41+
throw new Error("Too Many Values in row");
42+
} else if (arr.length < columnNames.length) {
43+
console.log(arr);
44+
throw new Error("Too Few Values in row");
45+
}
46+
47+
let valueLine = "\t(";
48+
arr.forEach((value) => {
49+
// Matches NULL values, Numbers,
50+
// Strings accepted as numbers, and Booleans (0 or 1)
51+
if (value === "NULL" || !isNaN(+value)) {
52+
valueLine += `${value}, `;
53+
} else {
54+
// If a string is wrapped in quotes, it doesn't need more
55+
if (value.at(0) === '"') valueLine += `${value}, `;
56+
else {
57+
// This wraps strings in quotes
58+
// also wraps timestamps
59+
valueLine += `"${value}", `;
60+
}
61+
}
62+
});
63+
valueLine = valueLine.slice(0, -2) + "),\n";
64+
values += valueLine;
65+
});
66+
values = values.slice(0, -2) + ";";
67+
68+
const sqlStatement = beginSQLInsert + values;
69+
70+
// Write File
71+
writeSQL(sqlStatement);
72+
} catch (err) {
73+
console.log(err);
74+
}
7875
}
7976

80-
readCSV()
77+
readCSV();
8178

82-
console.log('Finished!')
79+
console.log("Finished!");

package.json

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
2-
"name": "csv-to-sql-insert",
3-
"version": "0.1.0",
4-
"repository": "https://github.com/gitdagray/csv-to-sql",
5-
"description": "input a csv file, output a sql insert statement",
6-
"main": "index.js",
7-
"author": {
8-
"name": "Dave Gray",
9-
"email": "[email protected]",
10-
"url": "https://www.davegray.codes/"
11-
},
12-
"license": "MIT"
13-
}
2+
"name": "csv-to-sql-insert",
3+
"version": "0.1.0",
4+
"type": "module",
5+
"repository": "https://github.com/gitdagray/csv-to-sql",
6+
"description": "input a csv file, output a sql insert statement",
7+
"main": "index.js",
8+
"author": {
9+
"name": "Dave Gray",
10+
"email": "[email protected]",
11+
"url": "https://www.davegray.codes/"
12+
},
13+
"license": "MIT"
14+
}

0 commit comments

Comments
 (0)