Skip to content

Update readme file with new command line syntax to specify rows per insert #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
22 changes: 12 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
Expand Down Expand Up @@ -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!");