From fe658ac1736d61f4b70d4dfdb66cb967568d28e0 Mon Sep 17 00:00:00 2001 From: Ahmad Murey Date: Sat, 23 Dec 2023 08:34:04 +0300 Subject: [PATCH 1/2] Update readme file with new command line syntax to specify rows per insert parameter --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4e2be1d..cb4a504 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Provide table data as a CSV ([comma-separated values](https://en.wikipedia.org/w 1. Confirm you have a directory named `csv` 2. Confirm you have a directory named `sql` 3. Save your input CSV file in the `csv` directory -4. In a terminal window, first run `npm install` to install dependencies and then run `npm start YourFileName` +4. In a terminal window, first run `npm install` to install dependencies and then run `npm start YourFileName` or `npm start YourFileName RowsPerInsert` where `RowsPerInsert` is an optional integer number specifies how many rows to include in one `INSERT` statement 5. Watch the terminal window for any error messages 6. Your SQL insert statement will be saved in `sql/YourFileName.sql` From 246b2ec76c9e796f78875a7e4093d9498c1e9618 Mon Sep 17 00:00:00 2001 From: Ahmad Murey Date: Sat, 23 Dec 2023 09:26:02 +0300 Subject: [PATCH 2/2] Code refactoring: Decoupling / Pass process.args as parameters --- src/index.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index d308068..e78b6ad 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,15 +3,14 @@ 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) { + if (!saveFileAs) { throw new Error("Missing saveFileAs parameter"); } - createIfNot(path.resolve(`./sql/${destinationFile}.sql`)) - if(isAppend){ - await fs.appendFile(`sql/${process.argv[2]}.sql`, statement); + createIfNot(path.resolve(`./sql/${saveFileAs}.sql`)) + if(isAppend){ + await fs.appendFile(`sql/${saveFileAs}.sql`, statement); }else{ - await fs.writeFile(`sql/${process.argv[2]}.sql`, statement); + await fs.writeFile(`sql/${saveFileAs}.sql`, statement); } } catch (err) { console.log(err); @@ -20,10 +19,10 @@ async function writeSQL(statement: string, saveFileAs = "", isAppend: boolean = async function readCSV(csvFileName = "", batchSize: number = 0) { try { - const fileAndTableName = process.argv[2] || csvFileName; + const fileAndTableName = csvFileName || 'output'; - batchSize = parseInt(process.argv[3]) || batchSize || 500; - let isAppend: boolean = false; + batchSize = batchSize || 500; + let isAppend: boolean = false; if (!fileAndTableName) { throw new Error("Missing csvFileName parameter"); @@ -91,5 +90,8 @@ async function readCSV(csvFileName = "", batchSize: number = 0) { console.log(err); } } -readCSV(); + +const outputFileName = process.argv[2]; +const batchSize = parseInt(process.argv[3]); +readCSV(outputFileName, batchSize); console.log("Finished!");