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!" ) ;
0 commit comments