query-core 0.6.6 → 0.6.7

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/index.js CHANGED
@@ -12,6 +12,7 @@ __export(require("./query"));
12
12
  __export(require("./search"));
13
13
  __export(require("./SearchBuilder"));
14
14
  __export(require("./services"));
15
+ __export(require("./log"));
15
16
  var Loader = (function () {
16
17
  function Loader(query, sql, map, bools) {
17
18
  this.query = query;
package/lib/log.js ADDED
@@ -0,0 +1,339 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ function log(db, isLog, logger, q, result, r, duration) {
17
+ if (!isLog) {
18
+ return db;
19
+ }
20
+ if (q !== undefined && q != null && q.length > 0) {
21
+ if (!logger.isDebugEnabled()) {
22
+ return db;
23
+ }
24
+ return new LogExecutor(db, logger.error, logger.debug, q, result, r, duration);
25
+ }
26
+ if (!logger.isInfoEnabled()) {
27
+ return db;
28
+ }
29
+ return new LogExecutor(db, logger.error, logger.info, q, result, r, duration);
30
+ }
31
+ exports.log = log;
32
+ function useLog(db, isLog, err, lg, q, result, r, duration) {
33
+ if (!isLog) {
34
+ return db;
35
+ }
36
+ if (err) {
37
+ return new LogDB(db, err, lg, q, result, r, duration);
38
+ }
39
+ return db;
40
+ }
41
+ exports.useLog = useLog;
42
+ var LogExecutor = (function () {
43
+ function LogExecutor(executor, error, lg, q, result, r, duration) {
44
+ this.executor = executor;
45
+ this.error = error;
46
+ this.driver = executor.driver;
47
+ this.duration = duration && duration.length > 0 ? duration : "duration";
48
+ this.sql = q === undefined ? "" : q;
49
+ this.result = result !== undefined && result != null ? result : "";
50
+ this.return = r !== undefined && r != null ? r : "count";
51
+ this.log = lg;
52
+ this.param = this.param.bind(this);
53
+ this.execute = this.execute.bind(this);
54
+ this.executeBatch = this.executeBatch.bind(this);
55
+ this.query = this.query.bind(this);
56
+ this.queryOne = this.queryOne.bind(this);
57
+ this.executeScalar = this.executeScalar.bind(this);
58
+ this.count = this.count.bind(this);
59
+ }
60
+ LogExecutor.prototype.param = function (i) {
61
+ return this.executor.param(i);
62
+ };
63
+ LogExecutor.prototype.execute = function (sql, args, ctx) {
64
+ var _this = this;
65
+ var t1 = new Date();
66
+ return this.executor
67
+ .execute(sql, args, ctx)
68
+ .then(function (v) {
69
+ setTimeout(function () {
70
+ if (_this.log) {
71
+ var d = diff(t1);
72
+ var obj = {};
73
+ if (_this.sql.length > 0) {
74
+ obj[_this.sql] = getString(sql, args);
75
+ }
76
+ if (_this.return.length > 0) {
77
+ obj[_this.return] = v;
78
+ }
79
+ obj[_this.duration] = d;
80
+ _this.log("query", obj);
81
+ }
82
+ }, 0);
83
+ return v;
84
+ })
85
+ .catch(function (er) {
86
+ setTimeout(function () {
87
+ var d = diff(t1);
88
+ var obj = {};
89
+ if (_this.sql.length > 0) {
90
+ obj[_this.sql] = getString(sql, args);
91
+ }
92
+ obj[_this.duration] = d;
93
+ _this.error("error query: " + buildString(er));
94
+ }, 0);
95
+ throw er;
96
+ });
97
+ };
98
+ LogExecutor.prototype.executeBatch = function (statements, firstSuccess, ctx) {
99
+ var _this = this;
100
+ var t1 = new Date();
101
+ return this.executor
102
+ .executeBatch(statements, firstSuccess, ctx)
103
+ .then(function (v) {
104
+ setTimeout(function () {
105
+ if (_this.log) {
106
+ var d = diff(t1);
107
+ var obj = {};
108
+ if (_this.sql.length > 0) {
109
+ obj[_this.sql] = JSON.stringify(statements);
110
+ }
111
+ if (_this.return.length > 0) {
112
+ obj[_this.return] = v;
113
+ }
114
+ obj[_this.duration] = d;
115
+ _this.log("exec batch", obj);
116
+ }
117
+ }, 0);
118
+ return v;
119
+ })
120
+ .catch(function (er) {
121
+ setTimeout(function () {
122
+ var d = diff(t1);
123
+ var obj = {};
124
+ if (_this.sql.length > 0) {
125
+ obj[_this.sql] = JSON.stringify(statements);
126
+ }
127
+ obj[_this.duration] = d;
128
+ _this.error("error exec batch: " + buildString(er));
129
+ }, 0);
130
+ throw er;
131
+ });
132
+ };
133
+ LogExecutor.prototype.query = function (sql, args, m, bools, ctx) {
134
+ var _this = this;
135
+ var t1 = new Date();
136
+ return this.executor
137
+ .query(sql, args, m, bools, ctx)
138
+ .then(function (v) {
139
+ setTimeout(function () {
140
+ if (_this.log) {
141
+ var d = diff(t1);
142
+ var obj = {};
143
+ if (_this.sql.length > 0) {
144
+ obj[_this.sql] = getString(sql, args);
145
+ }
146
+ if (_this.result.length > 0) {
147
+ if (v && v.length > 0) {
148
+ obj[_this.result] = JSON.stringify(v);
149
+ }
150
+ }
151
+ if (_this.return.length > 0) {
152
+ obj[_this.return] = v ? v.length : 0;
153
+ }
154
+ obj[_this.duration] = d;
155
+ _this.log("query", obj);
156
+ }
157
+ }, 0);
158
+ return v;
159
+ })
160
+ .catch(function (er) {
161
+ setTimeout(function () {
162
+ var d = diff(t1);
163
+ var obj = {};
164
+ if (_this.sql.length > 0) {
165
+ obj[_this.sql] = getString(sql, args);
166
+ }
167
+ obj[_this.duration] = d;
168
+ _this.error("error query: " + buildString(er));
169
+ }, 0);
170
+ throw er;
171
+ });
172
+ };
173
+ LogExecutor.prototype.queryOne = function (sql, args, m, bools, ctx) {
174
+ var _this = this;
175
+ var t1 = new Date();
176
+ return this.executor
177
+ .queryOne(sql, args, m, bools, ctx)
178
+ .then(function (v) {
179
+ setTimeout(function () {
180
+ if (_this.log) {
181
+ var d = diff(t1);
182
+ var obj = {};
183
+ if (_this.sql.length > 0) {
184
+ obj[_this.sql] = getString(sql, args);
185
+ }
186
+ if (_this.result.length > 0) {
187
+ obj[_this.result] = v ? JSON.stringify(v) : "null";
188
+ }
189
+ if (_this.return.length > 0) {
190
+ obj[_this.return] = v ? 1 : 0;
191
+ }
192
+ obj[_this.duration] = d;
193
+ _this.log("query one", obj);
194
+ }
195
+ }, 0);
196
+ return v;
197
+ })
198
+ .catch(function (er) {
199
+ setTimeout(function () {
200
+ var d = diff(t1);
201
+ var obj = {};
202
+ if (_this.sql.length > 0) {
203
+ obj[_this.sql] = getString(sql, args);
204
+ }
205
+ obj[_this.duration] = d;
206
+ _this.error("error query one: " + buildString(er));
207
+ }, 0);
208
+ throw er;
209
+ });
210
+ };
211
+ LogExecutor.prototype.executeScalar = function (sql, args, ctx) {
212
+ var _this = this;
213
+ var t1 = new Date();
214
+ return this.executor
215
+ .executeScalar(sql, args, ctx)
216
+ .then(function (v) {
217
+ setTimeout(function () {
218
+ if (_this.log) {
219
+ var d = diff(t1);
220
+ var obj = {};
221
+ if (_this.sql.length > 0) {
222
+ obj[_this.sql] = getString(sql, args);
223
+ }
224
+ if (_this.result.length > 0) {
225
+ obj[_this.result] = v ? buildString(v) : "null";
226
+ }
227
+ if (_this.return.length > 0) {
228
+ obj[_this.return] = v ? 1 : 0;
229
+ }
230
+ obj[_this.duration] = d;
231
+ _this.log("exec scalar", obj);
232
+ }
233
+ }, 0);
234
+ return v;
235
+ })
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 exec scalar: " + buildString(er));
245
+ }, 0);
246
+ throw er;
247
+ });
248
+ };
249
+ LogExecutor.prototype.count = function (sql, args, ctx) {
250
+ var _this = this;
251
+ var t1 = new Date();
252
+ return this.executor
253
+ .count(sql, args)
254
+ .then(function (v) {
255
+ setTimeout(function () {
256
+ if (_this.log) {
257
+ var d = diff(t1);
258
+ var obj = {};
259
+ if (_this.sql.length > 0) {
260
+ obj[_this.sql] = getString(sql, args);
261
+ }
262
+ if (_this.return.length > 0) {
263
+ obj[_this.return] = v;
264
+ }
265
+ obj[_this.duration] = d;
266
+ _this.log("count", obj);
267
+ }
268
+ }, 0);
269
+ return v;
270
+ })
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 count: " + buildString(er));
280
+ }, 0);
281
+ throw er;
282
+ });
283
+ };
284
+ return LogExecutor;
285
+ }());
286
+ exports.LogExecutor = LogExecutor;
287
+ function buildString(v) {
288
+ if (typeof v === "string") {
289
+ return v;
290
+ }
291
+ else {
292
+ return JSON.stringify(v);
293
+ }
294
+ }
295
+ function getString(sql, args) {
296
+ if (args && args.length > 0) {
297
+ return sql + " " + JSON.stringify(args);
298
+ }
299
+ else {
300
+ return sql;
301
+ }
302
+ }
303
+ function diff(d1) {
304
+ var d2 = new Date();
305
+ return d2.getTime() - d1.getTime();
306
+ }
307
+ exports.diff = diff;
308
+ var LogTransaction = (function (_super) {
309
+ __extends(LogTransaction, _super);
310
+ function LogTransaction(tx, err, lg, q, result, r, duration) {
311
+ var _this = _super.call(this, tx, err, lg, q, result, r, duration) || this;
312
+ _this.tx = tx;
313
+ return _this;
314
+ }
315
+ LogTransaction.prototype.commit = function () {
316
+ return this.tx.commit();
317
+ };
318
+ LogTransaction.prototype.rollback = function () {
319
+ return this.tx.rollback();
320
+ };
321
+ return LogTransaction;
322
+ }(LogExecutor));
323
+ exports.LogTransaction = LogTransaction;
324
+ var LogDB = (function (_super) {
325
+ __extends(LogDB, _super);
326
+ function LogDB(db, err, lg, q, result, r, duration) {
327
+ var _this = _super.call(this, db, err, lg, q, result, r, duration) || this;
328
+ _this.db = db;
329
+ return _this;
330
+ }
331
+ LogDB.prototype.beginTransaction = function () {
332
+ var _this = this;
333
+ return this.db.beginTransaction().then(function (tx) {
334
+ return new LogTransaction(tx, _this.error, _this.log, _this.sql, _this.result, _this.return, _this.duration);
335
+ });
336
+ };
337
+ return LogDB;
338
+ }(LogExecutor));
339
+ exports.LogDB = LogDB;
package/lib/services.js CHANGED
@@ -120,298 +120,6 @@ var QueryRepository = (function () {
120
120
  return QueryRepository;
121
121
  }());
122
122
  exports.QueryRepository = QueryRepository;
123
- function log(db, isLog, logger, q, result, r, duration) {
124
- if (!isLog) {
125
- return db;
126
- }
127
- if (q !== undefined && q != null && q.length > 0) {
128
- if (!logger.isDebugEnabled()) {
129
- return db;
130
- }
131
- return new LogManager(db, logger.error, logger.debug, q, result, r, duration);
132
- }
133
- if (!logger.isInfoEnabled()) {
134
- return db;
135
- }
136
- return new LogManager(db, logger.error, logger.info, q, result, r, duration);
137
- }
138
- exports.log = log;
139
- function useLog(db, isLog, err, lg, q, result, r, duration) {
140
- if (!isLog) {
141
- return db;
142
- }
143
- if (err) {
144
- return new LogManager(db, err, lg, q, result, r, duration);
145
- }
146
- return db;
147
- }
148
- exports.useLog = useLog;
149
- var LogManager = (function () {
150
- function LogManager(db, err, lg, q, result, r, duration) {
151
- this.db = db;
152
- this.driver = db.driver;
153
- this.duration = duration && duration.length > 0 ? duration : "duration";
154
- this.sql = q === undefined ? "" : q;
155
- this.return = r !== undefined && r != null ? r : "count";
156
- this.result = result !== undefined && result != null ? result : "";
157
- this.log = lg;
158
- this.error = err;
159
- this.param = this.param.bind(this);
160
- this.execute = this.execute.bind(this);
161
- this.executeBatch = this.executeBatch.bind(this);
162
- this.query = this.query.bind(this);
163
- this.queryOne = this.queryOne.bind(this);
164
- this.executeScalar = this.executeScalar.bind(this);
165
- this.count = this.count.bind(this);
166
- }
167
- LogManager.prototype.param = function (i) {
168
- return this.db.param(i);
169
- };
170
- LogManager.prototype.execute = function (sql, args, ctx) {
171
- var _this = this;
172
- var t1 = new Date();
173
- return this.db
174
- .execute(sql, args, ctx)
175
- .then(function (v) {
176
- setTimeout(function () {
177
- if (_this.log) {
178
- var d = diff(t1);
179
- var obj = {};
180
- if (_this.sql.length > 0) {
181
- obj[_this.sql] = getString(sql, args);
182
- }
183
- if (_this.return.length > 0) {
184
- obj[_this.return] = v;
185
- }
186
- obj[_this.duration] = d;
187
- _this.log("query", obj);
188
- }
189
- }, 0);
190
- return v;
191
- })
192
- .catch(function (er) {
193
- setTimeout(function () {
194
- var d = diff(t1);
195
- var obj = {};
196
- if (_this.sql.length > 0) {
197
- obj[_this.sql] = getString(sql, args);
198
- }
199
- obj[_this.duration] = d;
200
- _this.error("error query: " + buildString(er));
201
- }, 0);
202
- throw er;
203
- });
204
- };
205
- LogManager.prototype.executeBatch = function (statements, firstSuccess, ctx) {
206
- var _this = this;
207
- var t1 = new Date();
208
- return this.db
209
- .executeBatch(statements, firstSuccess, ctx)
210
- .then(function (v) {
211
- setTimeout(function () {
212
- if (_this.log) {
213
- var d = diff(t1);
214
- var obj = {};
215
- if (_this.sql.length > 0) {
216
- obj[_this.sql] = JSON.stringify(statements);
217
- }
218
- if (_this.return.length > 0) {
219
- obj[_this.return] = v;
220
- }
221
- obj[_this.duration] = d;
222
- _this.log("exec batch", obj);
223
- }
224
- }, 0);
225
- return v;
226
- })
227
- .catch(function (er) {
228
- setTimeout(function () {
229
- var d = diff(t1);
230
- var obj = {};
231
- if (_this.sql.length > 0) {
232
- obj[_this.sql] = JSON.stringify(statements);
233
- }
234
- obj[_this.duration] = d;
235
- _this.error("error exec batch: " + buildString(er));
236
- }, 0);
237
- throw er;
238
- });
239
- };
240
- LogManager.prototype.query = function (sql, args, m, bools, ctx) {
241
- var _this = this;
242
- var t1 = new Date();
243
- return this.db
244
- .query(sql, args, m, bools, ctx)
245
- .then(function (v) {
246
- setTimeout(function () {
247
- if (_this.log) {
248
- var d = diff(t1);
249
- var obj = {};
250
- if (_this.sql.length > 0) {
251
- obj[_this.sql] = getString(sql, args);
252
- }
253
- if (_this.result.length > 0) {
254
- if (v && v.length > 0) {
255
- obj[_this.result] = JSON.stringify(v);
256
- }
257
- }
258
- if (_this.return.length > 0) {
259
- obj[_this.return] = v ? v.length : 0;
260
- }
261
- obj[_this.duration] = d;
262
- _this.log("query", obj);
263
- }
264
- }, 0);
265
- return v;
266
- })
267
- .catch(function (er) {
268
- setTimeout(function () {
269
- var d = diff(t1);
270
- var obj = {};
271
- if (_this.sql.length > 0) {
272
- obj[_this.sql] = getString(sql, args);
273
- }
274
- obj[_this.duration] = d;
275
- _this.error("error query: " + buildString(er));
276
- }, 0);
277
- throw er;
278
- });
279
- };
280
- LogManager.prototype.queryOne = function (sql, args, m, bools, ctx) {
281
- var _this = this;
282
- var t1 = new Date();
283
- return this.db
284
- .queryOne(sql, args, m, bools, ctx)
285
- .then(function (v) {
286
- setTimeout(function () {
287
- if (_this.log) {
288
- var d = diff(t1);
289
- var obj = {};
290
- if (_this.sql.length > 0) {
291
- obj[_this.sql] = getString(sql, args);
292
- }
293
- if (_this.result.length > 0) {
294
- obj[_this.result] = v ? JSON.stringify(v) : "null";
295
- }
296
- if (_this.return.length > 0) {
297
- obj[_this.return] = v ? 1 : 0;
298
- }
299
- obj[_this.duration] = d;
300
- _this.log("query one", obj);
301
- }
302
- }, 0);
303
- return v;
304
- })
305
- .catch(function (er) {
306
- setTimeout(function () {
307
- var d = diff(t1);
308
- var obj = {};
309
- if (_this.sql.length > 0) {
310
- obj[_this.sql] = getString(sql, args);
311
- }
312
- obj[_this.duration] = d;
313
- _this.error("error query one: " + buildString(er));
314
- }, 0);
315
- throw er;
316
- });
317
- };
318
- LogManager.prototype.executeScalar = function (sql, args, ctx) {
319
- var _this = this;
320
- var t1 = new Date();
321
- return this.db
322
- .executeScalar(sql, args, ctx)
323
- .then(function (v) {
324
- setTimeout(function () {
325
- if (_this.log) {
326
- var d = diff(t1);
327
- var obj = {};
328
- if (_this.sql.length > 0) {
329
- obj[_this.sql] = getString(sql, args);
330
- }
331
- if (_this.result.length > 0) {
332
- obj[_this.result] = v ? buildString(v) : "null";
333
- }
334
- if (_this.return.length > 0) {
335
- obj[_this.return] = v ? 1 : 0;
336
- }
337
- obj[_this.duration] = d;
338
- _this.log("exec scalar", obj);
339
- }
340
- }, 0);
341
- return v;
342
- })
343
- .catch(function (er) {
344
- setTimeout(function () {
345
- var d = diff(t1);
346
- var obj = {};
347
- if (_this.sql.length > 0) {
348
- obj[_this.sql] = getString(sql, args);
349
- }
350
- obj[_this.duration] = d;
351
- _this.error("error exec scalar: " + buildString(er));
352
- }, 0);
353
- throw er;
354
- });
355
- };
356
- LogManager.prototype.count = function (sql, args, ctx) {
357
- var _this = this;
358
- var t1 = new Date();
359
- return this.db
360
- .count(sql, args)
361
- .then(function (v) {
362
- setTimeout(function () {
363
- if (_this.log) {
364
- var d = diff(t1);
365
- var obj = {};
366
- if (_this.sql.length > 0) {
367
- obj[_this.sql] = getString(sql, args);
368
- }
369
- if (_this.return.length > 0) {
370
- obj[_this.return] = v;
371
- }
372
- obj[_this.duration] = d;
373
- _this.log("count", obj);
374
- }
375
- }, 0);
376
- return v;
377
- })
378
- .catch(function (er) {
379
- setTimeout(function () {
380
- var d = diff(t1);
381
- var obj = {};
382
- if (_this.sql.length > 0) {
383
- obj[_this.sql] = getString(sql, args);
384
- }
385
- obj[_this.duration] = d;
386
- _this.error("error count: " + buildString(er));
387
- }, 0);
388
- throw er;
389
- });
390
- };
391
- return LogManager;
392
- }());
393
- exports.LogManager = LogManager;
394
- function buildString(v) {
395
- if (typeof v === "string") {
396
- return v;
397
- }
398
- else {
399
- return JSON.stringify(v);
400
- }
401
- }
402
- function getString(sql, args) {
403
- if (args && args.length > 0) {
404
- return sql + " " + JSON.stringify(args);
405
- }
406
- else {
407
- return sql;
408
- }
409
- }
410
- function diff(d1) {
411
- var d2 = new Date();
412
- return d2.getTime() - d1.getTime();
413
- }
414
- exports.diff = diff;
415
123
  var SqlWriter = (function () {
416
124
  function SqlWriter(db, table, attributes, toDB) {
417
125
  this.db = db;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "query-core",
3
- "version": "0.6.6",
3
+ "version": "0.6.7",
4
4
  "description": "query",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "repository": {
19
19
  "type": "git",
20
- "url": "git@github.com/core-ts/query"
20
+ "url": "git@github.com/core-ts/sql"
21
21
  },
22
22
  "keywords": [
23
23
  "query",
@@ -1,18 +1,13 @@
1
- import { buildMetadata, param } from "./build"
2
- import { Attribute, Attributes, Statement, StringMap } from "./metadata"
3
- import { buildSort as bs, buildDollarParam, buildMsSQLParam, buildOracleParam, buildQuery, LikeType } from "./query"
4
- import { buildFromQuery, oracle, SearchResult } from "./search"
1
+ import { buildMetadata } from "./build"
2
+ import { Attribute, Attributes, MinDB, Statement, StringMap } from "./metadata"
3
+ import { buildSort as bs, buildQuery, LikeType } from "./query"
4
+ import { buildFromQuery, SearchResult } from "./search"
5
5
 
6
6
  export const postgres = "postgres"
7
7
  export const mssql = "mssql"
8
8
  export const mysql = "mysql"
9
9
  export const sqlite = "sqlite"
10
10
 
11
- interface MinDB {
12
- driver?: string
13
- param(i: number): string
14
- query<T>(sql: string, args?: any[], m?: StringMap, bools?: Attribute[], ctx?: any): Promise<T[]>
15
- }
16
11
  export class SearchBuilder<T, S> {
17
12
  protected map?: StringMap
18
13
  protected bools?: Attribute[]
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@ export * from "./query"
10
10
  export * from "./search"
11
11
  export * from "./SearchBuilder"
12
12
  export * from "./services"
13
+ export * from "./log"
13
14
 
14
15
  export interface Config {
15
16
  connectString?: string | undefined