forked from node-fun/execsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (37 loc) · 810 Bytes
/
index.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
var fs = require('fs'),
_ = require('underscore'),
mysql = require('mysql'),
config = {
multipleStatements: true
},
conn;
function exec(sql, callback) {
conn.query(sql, function (err, results) {
if (!_.isArray(results)) {
results = [results];
}
if (typeof callback === 'function') {
callback(err, results);
}
});
return this;
}
function execFile(filename, callback) {
fs.readFile(filename, 'utf8', function (err, data) {
if (err) throw err;
exec(data, callback);
});
return this;
}
exports.exec = exec;
exports.execFile = execFile;
exports.end = function () {
conn.end();
return this;
};
exports.config = function (options) {
_.extend(config, _.pick(options, ['host', 'port', 'user', 'password']));
conn = mysql.createConnection(config);
conn.connect();
return this;
};