This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2mysql.js
160 lines (131 loc) · 4.96 KB
/
csv2mysql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
const fs = require('fs');
const path = require('path');
const csv = require('csv-parse');
const mysql = require('mysql');
const prompt = require('prompt');
const chalk = require('chalk');
const parsearg = require('minimist');
const promptProperties = [{
name: 'password',
description: 'Enter your MySQL password',
hidden: true
}];
// Logging
const {log} = console;
const errorMsg = chalk.bold.red('Error: ');
// Arguments
const argv = parsearg(process.argv.slice(2));
const host = argv.host === undefined ? '127.0.0.1' : argv.host;
const user = argv.user === undefined ? 'root' : argv.user;
const database = argv.db === undefined ? '' : argv.db;
const table = argv.table === undefined ? '' : argv.table;
const file = argv.file === undefined ? '' : argv.file;
const delimiter = argv.delimiter === undefined ? ',' : argv.delimiter;
if (!database) {
throwMissingArgument('db');
}
if (!table) {
throwMissingArgument('table');
}
if (!file) {
throwMissingArgument('file');
}
function throwMissingArgument (type) {
log(errorMsg + chalk.red('Please define ' + type));
throw new Error('Missing argument');
}
prompt.start();
prompt.get(promptProperties, (err, result) => {
if (err) {
log(errorMsg + chalk.red('Password input failed!'));
throw err;
}
const {password} = result;
// Setup MySQL connection
const connection = mysql.createConnection({
host,
user,
password,
database
});
// Create parser of csv file
const parser = csv({delimiter}, (err, data) => {
if (err) {
log(errorMsg + chalk.red('File parsing failed'));
throw err;
}
const insertQuery = 'INSERT INTO ' + table + ' SET ?';
// Getting the first row containing the table column names
const fields = data.shift();
for (let i = 0; i < data.length; i++) {
const newEntry = {};
const relationTableAndCols = [];
for (let j = 0; j < fields.length; j++) {
// Checking if the column is used for a relation
const isRelation = fields[j].match(/\[(.*?)\]/);
if (isRelation && isRelation.length >= 2) {
const relationData = isRelation[1].split('|');
if (relationData && relationData.length === 3) {
relationTableAndCols[j] = {
table: relationData[0],
columnX: relationData[1],
columnY: relationData[2]
};
continue;
}
}
// Adding data to be inserted to a new entry object
newEntry[fields[j].toLowerCase()] = data[i][j];
}
const dataIndex = i;
// Insert data into MySQL
const query = connection.query(insertQuery, newEntry, (err, result) => {
if (err) {
log(errorMsg + chalk.red(err.sqlMessage));
throw err;
}
const {insertId} = result;
// Add relationship data if any specified
for (let k = 0; k < relationTableAndCols.length; k++) {
if (relationTableAndCols[k] !== undefined) {
const relData = relationTableAndCols[k];
const relValue = data[dataIndex][k].split('-');
if (relValue.length > 0) {
for (let l = 0; l < relValue.length; l++) {
const relEntry = {};
relEntry[relData.columnX] = insertId;
relEntry[relData.columnY] = relValue[l];
const relQuery = 'INSERT INTO ' + relData.table.toLowerCase() + ' SET ?';
const query = connection.query(relQuery, relEntry, err => {
if (err) {
log(errorMsg + chalk.red(err.sqlMessage));
throw err;
}
});
log(chalk.green(query.sql));
}
}
}
}
});
log(chalk.green(query.sql));
}
});
// Check for valid file source
fs.access(path.join(__dirname, file), fs.constants.R_OK, err => {
if (err) {
log(errorMsg + chalk.red('No access to file!'));
return;
}
// Connect to database
connection.connect(err => {
if (err) {
log(errorMsg + chalk.red('Could not connect to database!'));
return;
}
// Start reading file content
fs.createReadStream(path.join(__dirname, file)).pipe(parser);
});
});
});