Skip to content

Commit d18c445

Browse files
authored
Merge pull request #14 from Maanu07/enable-ts-support
Enabled ts support for the project
2 parents 3adb6a8 + c0a555b commit d18c445

File tree

6 files changed

+158
-92
lines changed

6 files changed

+158
-92
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.env
33
.vscode
4+
dist/

index.js

Lines changed: 0 additions & 79 deletions
This file was deleted.

package-lock.json

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
{
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",
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+
"scripts": {
9+
"start": "npm run build && node dist/index.js",
10+
"build": "tsc --skipLibCheck"
11+
},
12+
"author": {
13+
"name": "Dave Gray",
14+
"email": "[email protected]",
15+
"url": "https://www.davegray.codes/"
16+
},
17+
"license": "MIT",
1418
"devDependencies": {
1519
"husky": "^8.0.3",
1620
"prettier": "3.1.1",
17-
"pretty-quick": "^3.1.3"
21+
"pretty-quick": "^3.1.3",
22+
"@types/node": "^20.10.5",
23+
"typescript": "^5.3.3"
1824
},
1925
"husky": {
2026
"hooks": {

src/index.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { promises as fs } from "fs";
2+
3+
async function writeSQL(statement: string, saveFileAs = "") {
4+
try {
5+
const destinationFile = process.argv[2] || saveFileAs;
6+
7+
if (!destinationFile) {
8+
throw new Error("Missing saveFileAs parameter");
9+
}
10+
11+
await fs.writeFile(`sql/${process.argv[2]}.sql`, statement);
12+
} catch (err) {
13+
console.log(err);
14+
}
15+
}
16+
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`, {
26+
encoding: "utf8",
27+
});
28+
29+
const linesArray = data.split(/\r|\n/).filter((line) => line);
30+
const columnNames = linesArray?.shift()?.split(",") || [];
31+
32+
let beginSQLInsert = `INSERT INTO ${fileAndTableName} (`;
33+
columnNames.forEach((name) => (beginSQLInsert += `${name}, `));
34+
beginSQLInsert = beginSQLInsert.slice(0, -2) + ")\nVALUES\n";
35+
36+
let values = "";
37+
linesArray.forEach((line) => {
38+
// Parses each line of CSV into field values array
39+
const arr = line.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
40+
41+
if (arr.length > columnNames.length) {
42+
console.log(arr);
43+
throw new Error("Too Many Values in row");
44+
} else if (arr.length < columnNames.length) {
45+
console.log(arr);
46+
throw new Error("Too Few Values in row");
47+
}
48+
49+
let valueLine = "\t(";
50+
arr.forEach((value) => {
51+
// Matches NULL values, Numbers,
52+
// Strings accepted as numbers, and Booleans (0 or 1)
53+
if (value === "NULL" || !isNaN(+value)) {
54+
valueLine += `${value}, `;
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+
} catch (err) {
75+
console.log(err);
76+
}
77+
}
78+
79+
readCSV();
80+
81+
console.log("Finished!");

tsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "ES2020",
5+
"outDir": "./dist",
6+
"rootDir": "./src",
7+
"strict": true,
8+
"noEmitOnError": true
9+
},
10+
"include": ["src/**/*.ts"],
11+
"exclude": ["node_modules"]
12+
}

0 commit comments

Comments
 (0)