-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (121 loc) · 3.35 KB
/
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
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
'use strict';
/**
* SQL Query Template
* @param file relative path to the sql file
* @param conn MySQL Connection Object
* @param queryParams Object to be passed on to handlebar template
* @param cb Callback method to be invoked after the query returns result.
*/
module.exports = function sqt(conn, files, queryParams, cb) {
var fs = require('fs');
var HB = require('handlebars');
var Q = require('q');
var _ = require('lodash');
var chalk = require('chalk');
//var logError = chalk.bold.red;
function handleError(error) {
//error = '\u274C [sqt] : ' + error;
error = '[sqt] : ' + error;
throw new TypeError(error);
}
/**
*
* @param file
* @returns {*}
* @private
*/
function _addDotSQL(file) {
if (_.isString(file)) {
if (file.slice(-4) !== '.sql') {
file = file + ".sql";
}
}
return file;
}
/**
*
* @param file
* @param params
* @returns String the query string
* @private
*/
function _compile(file, params) {
var queryTemplate = fs.readFileSync(file).toString(),
compiledTemplate = HB.compile(queryTemplate);
return compiledTemplate(params);
}
/**
*
* @param file
* @param params
* @returns {*|promise}
* @private
*/
function _getDeferredPromise(file, params) {
var deferred = Q.defer(),
queryString = _compile(file, params);
conn.query(queryString, deferred.makeNodeResolver());
return deferred.promise;
}
/**
*
* @param files
* @param queryParams
* @param isSingleQuery
* @private
*/
function _executeQueue(files, queryParams, isSingleQuery) {
var queryQueue = [];
files.forEach(function (file, index) {
queryQueue.push(_getDeferredPromise(file, queryParams[index]));
});
var response = [];
Q.allSettled(queryQueue).then(function (results) {
_.each(results, function (res) {
if (res.state === 'fulfilled') {
response.push({state: res.state, data: res.value[0]});
} else {
response.push({state: res.state, message: res.reason});
}
});
if (isSingleQuery) {
cb(response[0]);
} else {
cb(response);
}
});
}
if (_.isArray(files)) {
if (files.length > 0) {
if (_.isArray(queryParams)) {
if (queryParams.length === files.length) {
var fileTypes = _.chain(files).map(function (file) {
return (_.isString(file) && !_.isEmpty(file))
}).uniq().value();
if (fileTypes.length === 1 && fileTypes[0]) {
files = _.map(files, function (file) {
return _addDotSQL(file);
});
_executeQueue(files, queryParams);
} else {
handleError('All files are not valid');
}
} else {
handleError('Query params and file list length do not match');
}
} else {
handleError('Query parameters should also be an array');
}
} else {
handleError('No files specified');
}
} else if (_.isString(files)) {
if (_.isObject(queryParams)) {
_executeQueue([_addDotSQL(files)], [queryParams], true);
} else {
handleError('Expect single query parameter as only one query is to be executed');
}
} else {
handleError('Expect an [array] or "string" of file(s) but got ' + JSON.stringify(files));
}
};