query-core 0.1.14 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/SearchBuilder.js +1 -1
- package/lib/health.js +116 -0
- package/lib/index.js +4 -1
- package/lib/query.js +8 -1
- package/lib/services.js +246 -0
- package/package.json +1 -1
- package/src/SearchBuilder.ts +3 -3
- package/src/health.ts +78 -0
- package/src/index.ts +5 -1
- package/src/query.ts +6 -1
- package/src/services.ts +275 -6
package/lib/SearchBuilder.js
CHANGED
|
@@ -54,7 +54,7 @@ var SearchBuilder = (function () {
|
|
|
54
54
|
var sn = s[st];
|
|
55
55
|
delete s[st];
|
|
56
56
|
var x = (this.provider === exports.postgres ? 'ilike' : this.buildParam);
|
|
57
|
-
var q2 = this.buildQuery(s, x, this.
|
|
57
|
+
var q2 = this.buildQuery(s, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding);
|
|
58
58
|
if (!q2) {
|
|
59
59
|
throw new Error('Cannot build query');
|
|
60
60
|
}
|
package/lib/health.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function () { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
var Checker = (function () {
|
|
40
|
+
function Checker(select, query, service, timeout) {
|
|
41
|
+
this.select = select;
|
|
42
|
+
this.query = query;
|
|
43
|
+
this.timeout = (timeout ? timeout : 4200);
|
|
44
|
+
this.service = (service ? service : 'sql');
|
|
45
|
+
this.check = this.check.bind(this);
|
|
46
|
+
this.name = this.name.bind(this);
|
|
47
|
+
this.build = this.build.bind(this);
|
|
48
|
+
}
|
|
49
|
+
Checker.prototype.check = function () {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
51
|
+
var obj, promise;
|
|
52
|
+
return __generator(this, function (_a) {
|
|
53
|
+
obj = {};
|
|
54
|
+
promise = this.select(this.query).then(function (r) { return obj; });
|
|
55
|
+
if (this.timeout > 0) {
|
|
56
|
+
return [2, promiseTimeOut(this.timeout, promise)];
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
return [2, promise];
|
|
60
|
+
}
|
|
61
|
+
return [2];
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
Checker.prototype.name = function () {
|
|
66
|
+
return this.service;
|
|
67
|
+
};
|
|
68
|
+
Checker.prototype.build = function (data, err) {
|
|
69
|
+
if (err) {
|
|
70
|
+
if (!data) {
|
|
71
|
+
data = {};
|
|
72
|
+
}
|
|
73
|
+
data['error'] = err;
|
|
74
|
+
}
|
|
75
|
+
return data;
|
|
76
|
+
};
|
|
77
|
+
return Checker;
|
|
78
|
+
}());
|
|
79
|
+
exports.Checker = Checker;
|
|
80
|
+
function promiseTimeOut(timeoutInMilliseconds, promise) {
|
|
81
|
+
return Promise.race([
|
|
82
|
+
promise,
|
|
83
|
+
new Promise(function (resolve, reject) {
|
|
84
|
+
setTimeout(function () {
|
|
85
|
+
reject("Timed out in: " + timeoutInMilliseconds + " milliseconds!");
|
|
86
|
+
}, timeoutInMilliseconds);
|
|
87
|
+
})
|
|
88
|
+
]);
|
|
89
|
+
}
|
|
90
|
+
exports.driverMap = {
|
|
91
|
+
'oracle': 'SELECT SYSDATE FROM DUAL',
|
|
92
|
+
'postgres': 'select now()',
|
|
93
|
+
'mssql': 'select getdate()',
|
|
94
|
+
'mysql': 'select current_time',
|
|
95
|
+
'sqlite': 'select date()'
|
|
96
|
+
};
|
|
97
|
+
function createChecker(query, sql, service, timeout) {
|
|
98
|
+
if (typeof query === 'function') {
|
|
99
|
+
if (!sql) {
|
|
100
|
+
sql = 'select getdate()';
|
|
101
|
+
}
|
|
102
|
+
return new Checker(query, sql, service, timeout);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
var db = query;
|
|
106
|
+
var s = exports.driverMap[db.driver];
|
|
107
|
+
if (!s) {
|
|
108
|
+
s = 'select getdate()';
|
|
109
|
+
}
|
|
110
|
+
if (!service) {
|
|
111
|
+
service = db.driver;
|
|
112
|
+
}
|
|
113
|
+
return new Checker(db.query, s, service, timeout);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.createChecker = createChecker;
|
package/lib/index.js
CHANGED
|
@@ -3,6 +3,8 @@ function __export(m) {
|
|
|
3
3
|
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
|
4
4
|
}
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var health_1 = require("./health");
|
|
7
|
+
exports.SqlChecker = health_1.Checker;
|
|
6
8
|
var services_1 = require("./services");
|
|
7
9
|
exports.SqlLoadRepository = services_1.SqlLoader;
|
|
8
10
|
exports.SqlViewRepository = services_1.SqlLoader;
|
|
@@ -20,10 +22,11 @@ exports.GenericSearchRepository = services_1.SqlSearchWriter;
|
|
|
20
22
|
exports.GenericSearchService = services_1.SqlSearchWriter;
|
|
21
23
|
exports.SqlRepository = services_1.SqlSearchWriter;
|
|
22
24
|
exports.SqlService = services_1.SqlSearchWriter;
|
|
23
|
-
exports.Repository = services_1.SqlSearchWriter;
|
|
24
25
|
exports.Service = services_1.SqlSearchWriter;
|
|
25
26
|
exports.SqlGenericRepository = services_1.SqlWriter;
|
|
27
|
+
exports.Repository = services_1.SqlWriter;
|
|
26
28
|
exports.SqlGenericService = services_1.SqlWriter;
|
|
29
|
+
__export(require("./health"));
|
|
27
30
|
__export(require("./build"));
|
|
28
31
|
__export(require("./services"));
|
|
29
32
|
__export(require("./batch"));
|
package/lib/query.js
CHANGED
|
@@ -56,7 +56,7 @@ function buildDollarParam(i) {
|
|
|
56
56
|
return '$' + i;
|
|
57
57
|
}
|
|
58
58
|
exports.buildDollarParam = buildDollarParam;
|
|
59
|
-
function buildQuery(filter, bparam,
|
|
59
|
+
function buildQuery(filter, bparam, sort, buildSort3, attrs, table, fields, sq, strExcluding) {
|
|
60
60
|
if (!table || !attrs) {
|
|
61
61
|
return undefined;
|
|
62
62
|
}
|
|
@@ -166,6 +166,10 @@ function buildQuery(filter, bparam, table, attrs, sort, fields, sq, strExcluding
|
|
|
166
166
|
filters.push(field + " <= " + param(i++));
|
|
167
167
|
args.push(v['max']);
|
|
168
168
|
}
|
|
169
|
+
else if (v['top']) {
|
|
170
|
+
filters.push(field + " < " + param(i++));
|
|
171
|
+
args.push(v['top']);
|
|
172
|
+
}
|
|
169
173
|
else if (v['endDate']) {
|
|
170
174
|
filters.push(field + " <= " + param(i++));
|
|
171
175
|
args.push(v['endDate']);
|
|
@@ -201,6 +205,9 @@ function buildQuery(filter, bparam, table, attrs, sort, fields, sq, strExcluding
|
|
|
201
205
|
if (v['max']) {
|
|
202
206
|
filters.push(field + " <= " + v['max']);
|
|
203
207
|
}
|
|
208
|
+
else if (v['top']) {
|
|
209
|
+
filters.push(field + " < " + v['top']);
|
|
210
|
+
}
|
|
204
211
|
else if (v['upper']) {
|
|
205
212
|
filters.push(field + " < " + v['upper']);
|
|
206
213
|
}
|
package/lib/services.js
CHANGED
|
@@ -91,6 +91,252 @@ var SqlSearchLoader = (function (_super) {
|
|
|
91
91
|
return SqlSearchLoader;
|
|
92
92
|
}(SqlLoader));
|
|
93
93
|
exports.SqlSearchLoader = SqlSearchLoader;
|
|
94
|
+
function log(db, isLog, logger, q, result, r, duration) {
|
|
95
|
+
if (!isLog) {
|
|
96
|
+
return db;
|
|
97
|
+
}
|
|
98
|
+
if (q !== undefined && q != null && q.length > 0) {
|
|
99
|
+
if (!logger.isDebugEnabled()) {
|
|
100
|
+
return db;
|
|
101
|
+
}
|
|
102
|
+
return new LogManager(db, logger.error, logger.debug, q, result, r, duration);
|
|
103
|
+
}
|
|
104
|
+
if (!logger.isInfoEnabled()) {
|
|
105
|
+
return db;
|
|
106
|
+
}
|
|
107
|
+
return new LogManager(db, logger.error, logger.info, q, result, r, duration);
|
|
108
|
+
}
|
|
109
|
+
exports.log = log;
|
|
110
|
+
function useLog(db, isLog, err, lg, q, result, r, duration) {
|
|
111
|
+
if (!isLog) {
|
|
112
|
+
return db;
|
|
113
|
+
}
|
|
114
|
+
if (err) {
|
|
115
|
+
return new LogManager(db, err, lg, q, result, r, duration);
|
|
116
|
+
}
|
|
117
|
+
return db;
|
|
118
|
+
}
|
|
119
|
+
exports.useLog = useLog;
|
|
120
|
+
var LogManager = (function () {
|
|
121
|
+
function LogManager(db, err, lg, q, result, r, duration) {
|
|
122
|
+
this.db = db;
|
|
123
|
+
this.driver = db.driver;
|
|
124
|
+
this.duration = (duration && duration.length > 0 ? duration : 'duration');
|
|
125
|
+
this.sql = (q === undefined ? '' : q);
|
|
126
|
+
this.return = (r !== undefined && r != null ? r : 'count');
|
|
127
|
+
this.result = (result !== undefined && result != null ? result : '');
|
|
128
|
+
this.log = lg;
|
|
129
|
+
this.error = err;
|
|
130
|
+
this.param = this.param.bind(this);
|
|
131
|
+
this.exec = this.exec.bind(this);
|
|
132
|
+
this.execBatch = this.execBatch.bind(this);
|
|
133
|
+
this.query = this.query.bind(this);
|
|
134
|
+
this.queryOne = this.queryOne.bind(this);
|
|
135
|
+
this.execScalar = this.execScalar.bind(this);
|
|
136
|
+
this.count = this.count.bind(this);
|
|
137
|
+
}
|
|
138
|
+
LogManager.prototype.param = function (i) {
|
|
139
|
+
return this.db.param(i);
|
|
140
|
+
};
|
|
141
|
+
LogManager.prototype.exec = function (sql, args, ctx) {
|
|
142
|
+
var t1 = new Date();
|
|
143
|
+
return this.db.exec(sql, args, ctx);
|
|
144
|
+
};
|
|
145
|
+
LogManager.prototype.execBatch = function (statements, firstSuccess, ctx) {
|
|
146
|
+
var _this = this;
|
|
147
|
+
var t1 = new Date();
|
|
148
|
+
return this.db.execBatch(statements, firstSuccess, ctx).then(function (v) {
|
|
149
|
+
setTimeout(function () {
|
|
150
|
+
if (_this.log) {
|
|
151
|
+
var d = diff(t1);
|
|
152
|
+
var obj = {};
|
|
153
|
+
if (_this.sql.length > 0) {
|
|
154
|
+
obj[_this.sql] = JSON.stringify(statements);
|
|
155
|
+
}
|
|
156
|
+
if (_this.return.length > 0) {
|
|
157
|
+
obj[_this.return] = v;
|
|
158
|
+
}
|
|
159
|
+
obj[_this.duration] = d;
|
|
160
|
+
_this.log('exec batch', obj);
|
|
161
|
+
}
|
|
162
|
+
}, 0);
|
|
163
|
+
return v;
|
|
164
|
+
}).catch(function (er) {
|
|
165
|
+
setTimeout(function () {
|
|
166
|
+
var d = diff(t1);
|
|
167
|
+
var obj = {};
|
|
168
|
+
if (_this.sql.length > 0) {
|
|
169
|
+
obj[_this.sql] = JSON.stringify(statements);
|
|
170
|
+
}
|
|
171
|
+
obj[_this.duration] = d;
|
|
172
|
+
_this.error('error exec batch: ' + buildString(er));
|
|
173
|
+
}, 0);
|
|
174
|
+
throw er;
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
LogManager.prototype.query = function (sql, args, m, bools, ctx) {
|
|
178
|
+
var _this = this;
|
|
179
|
+
var t1 = new Date();
|
|
180
|
+
return this.db.query(sql, args, m, bools, ctx).then(function (v) {
|
|
181
|
+
setTimeout(function () {
|
|
182
|
+
if (_this.log) {
|
|
183
|
+
var d = diff(t1);
|
|
184
|
+
var obj = {};
|
|
185
|
+
if (_this.sql.length > 0) {
|
|
186
|
+
obj[_this.sql] = getString(sql, args);
|
|
187
|
+
}
|
|
188
|
+
if (_this.result.length > 0) {
|
|
189
|
+
if (v && v.length > 0) {
|
|
190
|
+
obj[_this.result] = JSON.stringify(v);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (_this.return.length > 0) {
|
|
194
|
+
obj[_this.return] = v ? v.length : 0;
|
|
195
|
+
}
|
|
196
|
+
obj[_this.duration] = d;
|
|
197
|
+
_this.log('query', obj);
|
|
198
|
+
}
|
|
199
|
+
}, 0);
|
|
200
|
+
return v;
|
|
201
|
+
}).catch(function (er) {
|
|
202
|
+
setTimeout(function () {
|
|
203
|
+
var d = diff(t1);
|
|
204
|
+
var obj = {};
|
|
205
|
+
if (_this.sql.length > 0) {
|
|
206
|
+
obj[_this.sql] = getString(sql, args);
|
|
207
|
+
}
|
|
208
|
+
obj[_this.duration] = d;
|
|
209
|
+
_this.error('error query: ' + buildString(er));
|
|
210
|
+
}, 0);
|
|
211
|
+
throw er;
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
LogManager.prototype.queryOne = function (sql, args, m, bools, ctx) {
|
|
215
|
+
var _this = this;
|
|
216
|
+
var t1 = new Date();
|
|
217
|
+
return this.db.queryOne(sql, args, m, bools, ctx).then(function (v) {
|
|
218
|
+
setTimeout(function () {
|
|
219
|
+
if (_this.log) {
|
|
220
|
+
var d = diff(t1);
|
|
221
|
+
var obj = {};
|
|
222
|
+
if (_this.sql.length > 0) {
|
|
223
|
+
obj[_this.sql] = getString(sql, args);
|
|
224
|
+
}
|
|
225
|
+
if (_this.result.length > 0) {
|
|
226
|
+
obj[_this.result] = v ? JSON.stringify(v) : 'null';
|
|
227
|
+
}
|
|
228
|
+
if (_this.return.length > 0) {
|
|
229
|
+
obj[_this.return] = v ? 1 : 0;
|
|
230
|
+
}
|
|
231
|
+
obj[_this.duration] = d;
|
|
232
|
+
_this.log('query one', obj);
|
|
233
|
+
}
|
|
234
|
+
}, 0);
|
|
235
|
+
return v;
|
|
236
|
+
}).catch(function (er) {
|
|
237
|
+
setTimeout(function () {
|
|
238
|
+
var d = diff(t1);
|
|
239
|
+
var obj = {};
|
|
240
|
+
if (_this.sql.length > 0) {
|
|
241
|
+
obj[_this.sql] = getString(sql, args);
|
|
242
|
+
}
|
|
243
|
+
obj[_this.duration] = d;
|
|
244
|
+
_this.error('error query one: ' + buildString(er));
|
|
245
|
+
}, 0);
|
|
246
|
+
throw er;
|
|
247
|
+
});
|
|
248
|
+
};
|
|
249
|
+
LogManager.prototype.execScalar = function (sql, args, ctx) {
|
|
250
|
+
var _this = this;
|
|
251
|
+
var t1 = new Date();
|
|
252
|
+
return this.db.execScalar(sql, args, ctx).then(function (v) {
|
|
253
|
+
setTimeout(function () {
|
|
254
|
+
if (_this.log) {
|
|
255
|
+
var d = diff(t1);
|
|
256
|
+
var obj = {};
|
|
257
|
+
if (_this.sql.length > 0) {
|
|
258
|
+
obj[_this.sql] = getString(sql, args);
|
|
259
|
+
}
|
|
260
|
+
if (_this.result.length > 0) {
|
|
261
|
+
obj[_this.result] = v ? buildString(v) : 'null';
|
|
262
|
+
}
|
|
263
|
+
if (_this.return.length > 0) {
|
|
264
|
+
obj[_this.return] = v ? 1 : 0;
|
|
265
|
+
}
|
|
266
|
+
obj[_this.duration] = d;
|
|
267
|
+
_this.log('exec scalar', obj);
|
|
268
|
+
}
|
|
269
|
+
}, 0);
|
|
270
|
+
return v;
|
|
271
|
+
}).catch(function (er) {
|
|
272
|
+
setTimeout(function () {
|
|
273
|
+
var d = diff(t1);
|
|
274
|
+
var obj = {};
|
|
275
|
+
if (_this.sql.length > 0) {
|
|
276
|
+
obj[_this.sql] = getString(sql, args);
|
|
277
|
+
}
|
|
278
|
+
obj[_this.duration] = d;
|
|
279
|
+
_this.error('error exec scalar: ' + buildString(er));
|
|
280
|
+
}, 0);
|
|
281
|
+
throw er;
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
LogManager.prototype.count = function (sql, args, ctx) {
|
|
285
|
+
var _this = this;
|
|
286
|
+
var t1 = new Date();
|
|
287
|
+
return this.db.count(sql, args).then(function (v) {
|
|
288
|
+
setTimeout(function () {
|
|
289
|
+
if (_this.log) {
|
|
290
|
+
var d = diff(t1);
|
|
291
|
+
var obj = {};
|
|
292
|
+
if (_this.sql.length > 0) {
|
|
293
|
+
obj[_this.sql] = getString(sql, args);
|
|
294
|
+
}
|
|
295
|
+
if (_this.return.length > 0) {
|
|
296
|
+
obj[_this.return] = v;
|
|
297
|
+
}
|
|
298
|
+
obj[_this.duration] = d;
|
|
299
|
+
_this.log('count', obj);
|
|
300
|
+
}
|
|
301
|
+
}, 0);
|
|
302
|
+
return v;
|
|
303
|
+
}).catch(function (er) {
|
|
304
|
+
setTimeout(function () {
|
|
305
|
+
var d = diff(t1);
|
|
306
|
+
var obj = {};
|
|
307
|
+
if (_this.sql.length > 0) {
|
|
308
|
+
obj[_this.sql] = getString(sql, args);
|
|
309
|
+
}
|
|
310
|
+
obj[_this.duration] = d;
|
|
311
|
+
_this.error('error count: ' + buildString(er));
|
|
312
|
+
}, 0);
|
|
313
|
+
throw er;
|
|
314
|
+
});
|
|
315
|
+
};
|
|
316
|
+
return LogManager;
|
|
317
|
+
}());
|
|
318
|
+
exports.LogManager = LogManager;
|
|
319
|
+
function buildString(v) {
|
|
320
|
+
if (typeof v === 'string') {
|
|
321
|
+
return v;
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
return JSON.stringify(v);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
function getString(sql, args) {
|
|
328
|
+
if (args && args.length > 0) {
|
|
329
|
+
return sql + ' ' + JSON.stringify(args);
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
return sql;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function diff(d1) {
|
|
336
|
+
var d2 = new Date();
|
|
337
|
+
return d2.getTime() - d1.getTime();
|
|
338
|
+
}
|
|
339
|
+
exports.diff = diff;
|
|
94
340
|
var SqlWriter = (function (_super) {
|
|
95
341
|
__extends(SqlWriter, _super);
|
|
96
342
|
function SqlWriter(manager, table, attrs, toDB, fromDB) {
|
package/package.json
CHANGED
package/src/SearchBuilder.ts
CHANGED
|
@@ -10,7 +10,7 @@ export const sqlite = 'sqlite';
|
|
|
10
10
|
export class SearchBuilder<T, S> {
|
|
11
11
|
map?: StringMap;
|
|
12
12
|
bools?: Attribute[];
|
|
13
|
-
buildQuery: (s: S, bparam: LikeType|((i: number ) => string),
|
|
13
|
+
buildQuery: (s: S, bparam: LikeType|((i: number ) => string), sort?: string, buildSort3?: (sort?: string, map?: Attributes|StringMap) => string, attrs?: Attributes, table?: string, fields?: string[], sq?: string, strExcluding?: string) => Statement|undefined;
|
|
14
14
|
q?: string;
|
|
15
15
|
excluding?: string;
|
|
16
16
|
buildSort?: (sort?: string, map?: Attributes|StringMap) => string;
|
|
@@ -19,7 +19,7 @@ export class SearchBuilder<T, S> {
|
|
|
19
19
|
constructor(public query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[]) => Promise<T[]>, public table: string,
|
|
20
20
|
public attributes?: Attributes,
|
|
21
21
|
public provider?: string,
|
|
22
|
-
buildQ?: (s: S, bparam: LikeType|((i: number ) => string),
|
|
22
|
+
buildQ?: (s: S, bparam: LikeType|((i: number ) => string), sort?: string, buildSort3?: (sort?: string, map?: Attributes|StringMap) => string, attrs?: Attributes, table?: string, fields?: string[], sq?: string, strExcluding?: string) => Statement|undefined,
|
|
23
23
|
public fromDB?: (v: T) => T,
|
|
24
24
|
public sort?: string,
|
|
25
25
|
q?: string,
|
|
@@ -62,7 +62,7 @@ export class SearchBuilder<T, S> {
|
|
|
62
62
|
const sn = (s as any)[st] as string;
|
|
63
63
|
delete (s as any)[st];
|
|
64
64
|
const x = (this.provider === postgres ? 'ilike' : this.buildParam);
|
|
65
|
-
const q2 = this.buildQuery(s, x, this.
|
|
65
|
+
const q2 = this.buildQuery(s, x, sn, this.buildSort, this.attributes, this.table, fields, this.q, this.excluding);
|
|
66
66
|
if (!q2) {
|
|
67
67
|
throw new Error('Cannot build query');
|
|
68
68
|
}
|
package/src/health.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import {StringMap} from './metadata';
|
|
2
|
+
|
|
3
|
+
export interface SimpleDB {
|
|
4
|
+
driver: string;
|
|
5
|
+
query<T>(sql: string): Promise<T[]>;
|
|
6
|
+
}
|
|
7
|
+
export interface AnyMap {
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}
|
|
10
|
+
export class Checker {
|
|
11
|
+
timeout: number;
|
|
12
|
+
service: string;
|
|
13
|
+
constructor(public select: (sql: string) => Promise<any[]>, public query: string, service?: string, timeout?: number) {
|
|
14
|
+
this.timeout = (timeout ? timeout : 4200);
|
|
15
|
+
this.service = (service ? service : 'sql');
|
|
16
|
+
this.check = this.check.bind(this);
|
|
17
|
+
this.name = this.name.bind(this);
|
|
18
|
+
this.build = this.build.bind(this);
|
|
19
|
+
}
|
|
20
|
+
async check(): Promise<AnyMap> {
|
|
21
|
+
const obj = {} as AnyMap;
|
|
22
|
+
const promise = this.select(this.query).then(r => obj);
|
|
23
|
+
if (this.timeout > 0) {
|
|
24
|
+
return promiseTimeOut(this.timeout, promise);
|
|
25
|
+
} else {
|
|
26
|
+
return promise;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
name(): string {
|
|
30
|
+
return this.service;
|
|
31
|
+
}
|
|
32
|
+
build(data: AnyMap, err: any): AnyMap {
|
|
33
|
+
if (err) {
|
|
34
|
+
if (!data) {
|
|
35
|
+
data = {} as AnyMap;
|
|
36
|
+
}
|
|
37
|
+
data['error'] = err;
|
|
38
|
+
}
|
|
39
|
+
return data;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function promiseTimeOut(timeoutInMilliseconds: number, promise: Promise<any>): Promise<any> {
|
|
44
|
+
return Promise.race([
|
|
45
|
+
promise,
|
|
46
|
+
new Promise((resolve, reject) => {
|
|
47
|
+
setTimeout(() => {
|
|
48
|
+
reject(`Timed out in: ${timeoutInMilliseconds} milliseconds!`);
|
|
49
|
+
}, timeoutInMilliseconds);
|
|
50
|
+
})
|
|
51
|
+
]);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const driverMap: StringMap = {
|
|
55
|
+
'oracle': 'SELECT SYSDATE FROM DUAL',
|
|
56
|
+
'postgres': 'select now()',
|
|
57
|
+
'mssql': 'select getdate()',
|
|
58
|
+
'mysql': 'select current_time',
|
|
59
|
+
'sqlite': 'select date()'
|
|
60
|
+
};
|
|
61
|
+
export function createChecker(query: SimpleDB|((sql: string) => Promise<any[]>), sql?: string, service?: string, timeout?: number): Checker {
|
|
62
|
+
if (typeof query === 'function') {
|
|
63
|
+
if (!sql) {
|
|
64
|
+
sql = 'select getdate()';
|
|
65
|
+
}
|
|
66
|
+
return new Checker(query, sql, service, timeout);
|
|
67
|
+
} else {
|
|
68
|
+
const db = query as SimpleDB;
|
|
69
|
+
let s = driverMap[db.driver];
|
|
70
|
+
if (!s) {
|
|
71
|
+
s = 'select getdate()';
|
|
72
|
+
}
|
|
73
|
+
if (!service) {
|
|
74
|
+
service = db.driver;
|
|
75
|
+
}
|
|
76
|
+
return new Checker(db.query, s, service, timeout);
|
|
77
|
+
}
|
|
78
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
+
import {Checker} from './health';
|
|
2
|
+
export {Checker as SqlChecker};
|
|
3
|
+
|
|
1
4
|
import {Attribute, StringMap} from './metadata';
|
|
2
5
|
import {SqlLoader, SqlSearchLoader, SqlSearchWriter, SqlWriter} from './services';
|
|
3
6
|
export {SqlLoader as SqlLoadRepository};
|
|
4
7
|
export {SqlLoader as SqlViewRepository};
|
|
5
8
|
export {SqlWriter as SqlGenericRepository};
|
|
9
|
+
export {SqlWriter as Repository};
|
|
6
10
|
|
|
7
11
|
export {SqlLoader as SqlLoadService};
|
|
8
12
|
export {SqlLoader as SqlViewService};
|
|
@@ -21,10 +25,10 @@ export {SqlSearchWriter as GenericSearchRepository};
|
|
|
21
25
|
export {SqlSearchWriter as GenericSearchService};
|
|
22
26
|
export {SqlSearchWriter as SqlRepository};
|
|
23
27
|
export {SqlSearchWriter as SqlService};
|
|
24
|
-
export {SqlSearchWriter as Repository};
|
|
25
28
|
export {SqlSearchWriter as Service};
|
|
26
29
|
|
|
27
30
|
export * from './metadata';
|
|
31
|
+
export * from './health';
|
|
28
32
|
export * from './build';
|
|
29
33
|
export * from './services';
|
|
30
34
|
export * from './batch';
|
package/src/query.ts
CHANGED
|
@@ -52,7 +52,7 @@ export function buildOracleParam(i: number): string {
|
|
|
52
52
|
export function buildDollarParam(i: number): string {
|
|
53
53
|
return '$' + i;
|
|
54
54
|
}
|
|
55
|
-
export function buildQuery<S>(filter: S, bparam: LikeType|((i: number ) => string),
|
|
55
|
+
export function buildQuery<S>(filter: S, bparam: LikeType|((i: number ) => string), sort?: string, buildSort3?: (sort?: string, map?: Attributes|StringMap) => string, attrs?: Attributes, table?: string, fields?: string[], sq?: string, strExcluding?: string): Statement|undefined {
|
|
56
56
|
if (!table || !attrs) {
|
|
57
57
|
return undefined;
|
|
58
58
|
}
|
|
@@ -149,6 +149,9 @@ export function buildQuery<S>(filter: S, bparam: LikeType|((i: number ) => strin
|
|
|
149
149
|
if (v['max']) {
|
|
150
150
|
filters.push(`${field} <= ${param(i++)}`);
|
|
151
151
|
args.push(v['max']);
|
|
152
|
+
} else if (v['top']) {
|
|
153
|
+
filters.push(`${field} < ${param(i++)}`);
|
|
154
|
+
args.push(v['top']);
|
|
152
155
|
} else if (v['endDate']) {
|
|
153
156
|
filters.push(`${field} <= ${param(i++)}`);
|
|
154
157
|
args.push(v['endDate']);
|
|
@@ -177,6 +180,8 @@ export function buildQuery<S>(filter: S, bparam: LikeType|((i: number ) => strin
|
|
|
177
180
|
if (isNumberRange(v)) {
|
|
178
181
|
if (v['max']) {
|
|
179
182
|
filters.push(`${field} <= ${v['max']}`);
|
|
183
|
+
} else if (v['top']) {
|
|
184
|
+
filters.push(`${field} < ${v['top']}`);
|
|
180
185
|
} else if (v['upper']) {
|
|
181
186
|
filters.push(`${field} < ${v['upper']}`);
|
|
182
187
|
}
|
package/src/services.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {attributes, buildToDelete, buildToInsert, buildToUpdate, exist, metadata, select, version} from './build';
|
|
1
|
+
import {attributes, buildToDelete, buildToInsert, buildToUpdate, exist, metadata, select, toString, version} from './build';
|
|
2
2
|
import {Attribute, Attributes, Statement, StringMap} from './metadata';
|
|
3
3
|
import {SearchResult} from './search';
|
|
4
4
|
|
|
@@ -13,7 +13,7 @@ export class SqlLoader<T, ID> {
|
|
|
13
13
|
attributes: Attributes;
|
|
14
14
|
bools?: Attribute[];
|
|
15
15
|
constructor(public table: string,
|
|
16
|
-
public query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<
|
|
16
|
+
public query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
|
|
17
17
|
attrs: Attributes|string[],
|
|
18
18
|
public param: (i: number) => string,
|
|
19
19
|
public fromDB?: (v: T) => T) {
|
|
@@ -48,7 +48,7 @@ export class SqlLoader<T, ID> {
|
|
|
48
48
|
}
|
|
49
49
|
const fn = this.fromDB;
|
|
50
50
|
if (fn) {
|
|
51
|
-
return this.query(stmt.query, stmt.params, this.map, ctx).then(res => {
|
|
51
|
+
return this.query<T>(stmt.query, stmt.params, this.map, ctx).then(res => {
|
|
52
52
|
if (!res || res.length === 0) {
|
|
53
53
|
return null;
|
|
54
54
|
} else {
|
|
@@ -57,7 +57,7 @@ export class SqlLoader<T, ID> {
|
|
|
57
57
|
}
|
|
58
58
|
});
|
|
59
59
|
} else {
|
|
60
|
-
return this.query(stmt.query, stmt.params, this.map).then(res => (!res || res.length === 0) ? null : res[0]);
|
|
60
|
+
return this.query<T>(stmt.query, stmt.params, this.map).then(res => (!res || res.length === 0) ? null : res[0]);
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
exist(id: ID, ctx?: any): Promise<boolean> {
|
|
@@ -69,11 +69,12 @@ export class SqlLoader<T, ID> {
|
|
|
69
69
|
return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(res => (!res || res.length === 0) ? false : true);
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
72
73
|
export class SqlSearchLoader<T, ID, S extends Filter> extends SqlLoader<T, ID> {
|
|
73
74
|
constructor(
|
|
74
75
|
protected find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
|
|
75
76
|
table: string,
|
|
76
|
-
query: (sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<
|
|
77
|
+
query: <K>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any) => Promise<K[]>,
|
|
77
78
|
attrs: Attributes|string[],
|
|
78
79
|
param: (i: number) => string,
|
|
79
80
|
fromDB?: (v: T) => T) {
|
|
@@ -91,13 +92,280 @@ export interface Manager {
|
|
|
91
92
|
execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>;
|
|
92
93
|
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>;
|
|
93
94
|
}
|
|
94
|
-
export
|
|
95
|
+
export type DB = Manager;
|
|
96
|
+
export interface ExtManager {
|
|
95
97
|
driver: string;
|
|
96
98
|
param(i: number): string;
|
|
97
99
|
exec(sql: string, args?: any[], ctx?: any): Promise<number>;
|
|
98
100
|
execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number>;
|
|
99
101
|
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>;
|
|
102
|
+
queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T|null>;
|
|
103
|
+
execScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T>;
|
|
104
|
+
count(sql: string, args?: any[], ctx?: any): Promise<number>;
|
|
100
105
|
}
|
|
106
|
+
export interface SimpleMap {
|
|
107
|
+
[key: string]: string|number|boolean|Date;
|
|
108
|
+
}
|
|
109
|
+
export interface Logger {
|
|
110
|
+
level: number;
|
|
111
|
+
debug(msg: string, m?: SimpleMap, ctx?: any): void;
|
|
112
|
+
info(msg: string, m?: SimpleMap, ctx?: any): void;
|
|
113
|
+
error(msg: string, m?: SimpleMap, ctx?: any): void;
|
|
114
|
+
isDebugEnabled(): boolean;
|
|
115
|
+
isInfoEnabled(): boolean;
|
|
116
|
+
}
|
|
117
|
+
export function log(db: ExtManager, isLog: boolean|undefined|null, logger: Logger, q?: string, result?: string, r?: string, duration?: string): ExtManager {
|
|
118
|
+
if (!isLog) {
|
|
119
|
+
return db;
|
|
120
|
+
}
|
|
121
|
+
if (q !== undefined && q != null && q.length > 0) {
|
|
122
|
+
if (!logger.isDebugEnabled()) {
|
|
123
|
+
return db;
|
|
124
|
+
}
|
|
125
|
+
return new LogManager(db, logger.error, logger.debug, q, result, r, duration);
|
|
126
|
+
}
|
|
127
|
+
if (!logger.isInfoEnabled()) {
|
|
128
|
+
return db;
|
|
129
|
+
}
|
|
130
|
+
return new LogManager(db, logger.error, logger.info, q, result, r, duration);
|
|
131
|
+
}
|
|
132
|
+
export function useLog(db: ExtManager, isLog: boolean|undefined|null, err: ((msg: string, m?: SimpleMap) => void)|undefined, lg?: (msg: string, m?: SimpleMap) => void, q?: string, result?: string, r?: string, duration?: string): ExtManager {
|
|
133
|
+
if (!isLog) {
|
|
134
|
+
return db;
|
|
135
|
+
}
|
|
136
|
+
if (err) {
|
|
137
|
+
return new LogManager(db, err, lg, q, result, r, duration);
|
|
138
|
+
}
|
|
139
|
+
return db;
|
|
140
|
+
}
|
|
141
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
142
|
+
export class LogManager implements ExtManager {
|
|
143
|
+
constructor(public db: ExtManager, err: (msg: string, m?: SimpleMap) => void, lg?: (msg: string, m?: SimpleMap) => void, q?: string, result?: string, r?: string, duration?: string) {
|
|
144
|
+
this.driver = db.driver;
|
|
145
|
+
this.duration = (duration && duration.length > 0 ? duration : 'duration');
|
|
146
|
+
this.sql = (q === undefined ? '' : q);
|
|
147
|
+
this.return = (r !== undefined && r != null ? r : 'count');
|
|
148
|
+
this.result = (result !== undefined && result != null ? result : '');
|
|
149
|
+
// this.err = (er ? er : 'error');
|
|
150
|
+
this.log = lg;
|
|
151
|
+
this.error = err;
|
|
152
|
+
this.param = this.param.bind(this);
|
|
153
|
+
this.exec = this.exec.bind(this);
|
|
154
|
+
this.execBatch = this.execBatch.bind(this);
|
|
155
|
+
this.query = this.query.bind(this);
|
|
156
|
+
this.queryOne = this.queryOne.bind(this);
|
|
157
|
+
this.execScalar = this.execScalar.bind(this);
|
|
158
|
+
this.count = this.count.bind(this);
|
|
159
|
+
}
|
|
160
|
+
log?: (msg: string, m?: SimpleMap, ctx?: any) => void;
|
|
161
|
+
error: (msg: string, m?: SimpleMap, ctx?: any) => void;
|
|
162
|
+
driver: string;
|
|
163
|
+
duration: string;
|
|
164
|
+
sql: string;
|
|
165
|
+
return: string;
|
|
166
|
+
result: string;
|
|
167
|
+
// err: string;
|
|
168
|
+
param(i: number): string {
|
|
169
|
+
return this.db.param(i);
|
|
170
|
+
}
|
|
171
|
+
exec(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
172
|
+
const t1 = new Date();
|
|
173
|
+
return this.db.exec(sql, args, ctx);
|
|
174
|
+
}
|
|
175
|
+
execBatch(statements: Statement[], firstSuccess?: boolean, ctx?: any): Promise<number> {
|
|
176
|
+
const t1 = new Date();
|
|
177
|
+
return this.db.execBatch(statements, firstSuccess, ctx).then(v => {
|
|
178
|
+
setTimeout(() => {
|
|
179
|
+
if (this.log) {
|
|
180
|
+
const d = diff(t1);
|
|
181
|
+
const obj: SimpleMap = {} ;
|
|
182
|
+
if (this.sql.length > 0) {
|
|
183
|
+
obj[this.sql] = JSON.stringify(statements);
|
|
184
|
+
}
|
|
185
|
+
if (this.return.length > 0) {
|
|
186
|
+
obj[this.return] = v;
|
|
187
|
+
}
|
|
188
|
+
obj[this.duration] = d;
|
|
189
|
+
this.log('exec batch', obj);
|
|
190
|
+
}
|
|
191
|
+
}, 0);
|
|
192
|
+
return v;
|
|
193
|
+
}).catch(er => {
|
|
194
|
+
setTimeout(() => {
|
|
195
|
+
const d = diff(t1);
|
|
196
|
+
const obj: SimpleMap = {};
|
|
197
|
+
if (this.sql.length > 0) {
|
|
198
|
+
obj[this.sql] = JSON.stringify(statements);
|
|
199
|
+
}
|
|
200
|
+
obj[this.duration] = d;
|
|
201
|
+
this.error('error exec batch: ' + buildString(er));
|
|
202
|
+
}, 0);
|
|
203
|
+
throw er;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]> {
|
|
207
|
+
const t1 = new Date();
|
|
208
|
+
return this.db.query<T>(sql, args, m, bools, ctx).then(v => {
|
|
209
|
+
setTimeout(() => {
|
|
210
|
+
if (this.log) {
|
|
211
|
+
const d = diff(t1);
|
|
212
|
+
const obj: SimpleMap = {} ;
|
|
213
|
+
if (this.sql.length > 0) {
|
|
214
|
+
obj[this.sql] = getString(sql, args);
|
|
215
|
+
}
|
|
216
|
+
if (this.result.length > 0) {
|
|
217
|
+
if (v && v.length > 0) {
|
|
218
|
+
obj[this.result] = JSON.stringify(v);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (this.return.length > 0) {
|
|
222
|
+
obj[this.return] = v ? v.length : 0;
|
|
223
|
+
}
|
|
224
|
+
obj[this.duration] = d;
|
|
225
|
+
this.log('query', obj);
|
|
226
|
+
}
|
|
227
|
+
}, 0);
|
|
228
|
+
return v;
|
|
229
|
+
}).catch(er => {
|
|
230
|
+
setTimeout(() => {
|
|
231
|
+
const d = diff(t1);
|
|
232
|
+
const obj: SimpleMap = {};
|
|
233
|
+
if (this.sql.length > 0) {
|
|
234
|
+
obj[this.sql] = getString(sql, args);
|
|
235
|
+
}
|
|
236
|
+
obj[this.duration] = d;
|
|
237
|
+
this.error('error query: ' + buildString(er));
|
|
238
|
+
}, 0);
|
|
239
|
+
throw er;
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
queryOne<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T|null> {
|
|
243
|
+
const t1 = new Date();
|
|
244
|
+
return this.db.queryOne<T>(sql, args, m, bools, ctx).then(v => {
|
|
245
|
+
setTimeout(() => {
|
|
246
|
+
if (this.log) {
|
|
247
|
+
const d = diff(t1);
|
|
248
|
+
const obj: SimpleMap = {} ;
|
|
249
|
+
if (this.sql.length > 0) {
|
|
250
|
+
obj[this.sql] = getString(sql, args);
|
|
251
|
+
}
|
|
252
|
+
if (this.result.length > 0) {
|
|
253
|
+
obj[this.result] = v ? JSON.stringify(v) : 'null';
|
|
254
|
+
}
|
|
255
|
+
if (this.return.length > 0) {
|
|
256
|
+
obj[this.return] = v ? 1 : 0;
|
|
257
|
+
}
|
|
258
|
+
obj[this.duration] = d;
|
|
259
|
+
this.log('query one', obj);
|
|
260
|
+
}
|
|
261
|
+
}, 0);
|
|
262
|
+
return v;
|
|
263
|
+
}).catch(er => {
|
|
264
|
+
setTimeout(() => {
|
|
265
|
+
const d = diff(t1);
|
|
266
|
+
const obj: SimpleMap = {};
|
|
267
|
+
if (this.sql.length > 0) {
|
|
268
|
+
obj[this.sql] = getString(sql, args);
|
|
269
|
+
}
|
|
270
|
+
obj[this.duration] = d;
|
|
271
|
+
this.error('error query one: ' + buildString(er));
|
|
272
|
+
}, 0);
|
|
273
|
+
throw er;
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
execScalar<T>(sql: string, args?: any[], ctx?: any): Promise<T> {
|
|
277
|
+
const t1 = new Date();
|
|
278
|
+
return this.db.execScalar<T>(sql, args, ctx).then(v => {
|
|
279
|
+
setTimeout(() => {
|
|
280
|
+
if (this.log) {
|
|
281
|
+
const d = diff(t1);
|
|
282
|
+
const obj: SimpleMap = {} ;
|
|
283
|
+
if (this.sql.length > 0) {
|
|
284
|
+
obj[this.sql] = getString(sql, args);
|
|
285
|
+
}
|
|
286
|
+
if (this.result.length > 0) {
|
|
287
|
+
obj[this.result] = v ? buildString(v) : 'null';
|
|
288
|
+
}
|
|
289
|
+
if (this.return.length > 0) {
|
|
290
|
+
obj[this.return] = v ? 1 : 0;
|
|
291
|
+
}
|
|
292
|
+
obj[this.duration] = d;
|
|
293
|
+
this.log('exec scalar', obj);
|
|
294
|
+
}
|
|
295
|
+
}, 0);
|
|
296
|
+
return v;
|
|
297
|
+
}).catch(er => {
|
|
298
|
+
setTimeout(() => {
|
|
299
|
+
const d = diff(t1);
|
|
300
|
+
const obj: SimpleMap = {};
|
|
301
|
+
if (this.sql.length > 0) {
|
|
302
|
+
obj[this.sql] = getString(sql, args);
|
|
303
|
+
}
|
|
304
|
+
obj[this.duration] = d;
|
|
305
|
+
this.error('error exec scalar: ' + buildString(er));
|
|
306
|
+
}, 0);
|
|
307
|
+
throw er;
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
count(sql: string, args?: any[], ctx?: any): Promise<number> {
|
|
311
|
+
const t1 = new Date();
|
|
312
|
+
return this.db.count(sql, args).then(v => {
|
|
313
|
+
setTimeout(() => {
|
|
314
|
+
if (this.log) {
|
|
315
|
+
const d = diff(t1);
|
|
316
|
+
const obj: SimpleMap = {} ;
|
|
317
|
+
if (this.sql.length > 0) {
|
|
318
|
+
obj[this.sql] = getString(sql, args);
|
|
319
|
+
}
|
|
320
|
+
if (this.return.length > 0) {
|
|
321
|
+
obj[this.return] = v;
|
|
322
|
+
}
|
|
323
|
+
obj[this.duration] = d;
|
|
324
|
+
this.log('count', obj);
|
|
325
|
+
}
|
|
326
|
+
}, 0);
|
|
327
|
+
return v;
|
|
328
|
+
}).catch(er => {
|
|
329
|
+
setTimeout(() => {
|
|
330
|
+
const d = diff(t1);
|
|
331
|
+
const obj: SimpleMap = {};
|
|
332
|
+
if (this.sql.length > 0) {
|
|
333
|
+
obj[this.sql] = getString(sql, args);
|
|
334
|
+
}
|
|
335
|
+
obj[this.duration] = d;
|
|
336
|
+
this.error('error count: ' + buildString(er));
|
|
337
|
+
}, 0);
|
|
338
|
+
throw er;
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
function buildString(v: any): string {
|
|
343
|
+
if (typeof v === 'string') {
|
|
344
|
+
return v;
|
|
345
|
+
} else {
|
|
346
|
+
return JSON.stringify(v);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
function getString(sql: string, args?: any[]): string {
|
|
350
|
+
if (args && args.length > 0) {
|
|
351
|
+
return sql + ' ' + JSON.stringify(args);
|
|
352
|
+
} else {
|
|
353
|
+
return sql;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
export function diff(d1: Date): number {
|
|
357
|
+
const d2 = new Date();
|
|
358
|
+
return d2.getTime() - d1.getTime();
|
|
359
|
+
}
|
|
360
|
+
/*
|
|
361
|
+
const NS_PER_SEC = 1e9;
|
|
362
|
+
const NS_TO_MS = 1e6;
|
|
363
|
+
const getDurationInMilliseconds = (start: [number, number] | undefined) => {
|
|
364
|
+
const diff = process.hrtime(start);
|
|
365
|
+
return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
|
|
366
|
+
};
|
|
367
|
+
*/
|
|
368
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
101
369
|
export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
|
|
102
370
|
version?: string;
|
|
103
371
|
exec: (sql: string, args?: any[], ctx?: any) => Promise<number>;
|
|
@@ -160,6 +428,7 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
|
|
|
160
428
|
}
|
|
161
429
|
}
|
|
162
430
|
}
|
|
431
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
163
432
|
export class SqlSearchWriter<T, ID, S extends Filter> extends SqlWriter<T, ID> {
|
|
164
433
|
constructor(
|
|
165
434
|
protected find: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>,
|