forked from medic/medic-bulk-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-mysql-query
executable file
·89 lines (71 loc) · 2.08 KB
/
export-mysql-query
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
#!/usr/bin/env node
var fs = require('fs'),
url = require('url'),
path = require('path'),
csv = require('fast-csv');
var SELF = process.argv[1].split(path.sep).pop();
// requires node >= 4 because of the multi-line string.
if (process.version.match(/^v(\d+\.\d+)/)[1] < 4) {
console.error("Please upgrade your NodeJS to >= 4.");
process.exit(1);
}
var usage = function() {
console.log(`
Usage: ${SELF} file
Description:
Query a MySQL database and stream results to stdout in csv format.
Normalizes date fields as GMT strings.
Enviornment Variables:
MYSQL_URL
Dependencies:
This script is rarely used so mysql dependency is handled manually:
npm install mysql
Examples:
MYSQL_URL=mysql://root@localhost:3306/mydb ./${SELF} query.sql \\
> data/families.csv
`);
process.exit(1);
};
if (!process.env.MYSQL_URL) {
console.error('MYSQL_URL not defined.');
usage();
}
// Allow usage to print without mysql dependency
var mysql = require('mysql'),
mysqlOpts = url.parse(process.env.MYSQL_URL);
var connection = mysql.createConnection({
host : mysqlOpts.hostname,
user : mysqlOpts.auth.split(':')[0],
password : mysqlOpts.auth.split(':')[1],
database : mysqlOpts.path.replace('/','')
});
var csvStream = csv.createWriteStream({headers: true});
csvStream.pipe(process.stdout);
fs.readFile(process.argv[2], function(err, data) {;
if (err) throw err;
//runQuery(data.toString());
var query = connection.query(data.toString()),
fields;
query
.on('error', function(err) {
throw err;
})
.on('fields', function(res) {
fields = res;
})
.on('result', function(row) {
var data = {};
fields.forEach(function(field) {
// mysql module casts DATE, DATETIME and TIMESTAMP fields as Dates
if (row[field.name] && row[field.name].constructor === Date) {
data[field.name] = row[field.name].toGMTString();
} else {
data[field.name] = row[field.name];
}
});
csvStream.write(data);
})
.on('end', function() {
connection.end();
});
});