query-core 0.1.30 → 0.1.31
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/services.js +392 -238
- package/package.json +1 -1
- package/src/services.ts +4 -4
package/lib/services.js
CHANGED
|
@@ -1,17 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
2
|
+
var __extends =
|
|
3
|
+
(this && this.__extends) ||
|
|
4
|
+
(function () {
|
|
5
|
+
var extendStatics = function (d, b) {
|
|
6
|
+
extendStatics =
|
|
7
|
+
Object.setPrototypeOf ||
|
|
8
|
+
({ __proto__: [] } instanceof Array &&
|
|
9
|
+
function (d, b) {
|
|
10
|
+
d.__proto__ = b;
|
|
11
|
+
}) ||
|
|
12
|
+
function (d, b) {
|
|
13
|
+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
|
14
|
+
};
|
|
15
|
+
return extendStatics(d, b);
|
|
16
|
+
};
|
|
17
|
+
return function (d, b) {
|
|
18
|
+
extendStatics(d, b);
|
|
19
|
+
function __() {
|
|
20
|
+
this.constructor = d;
|
|
21
|
+
}
|
|
22
|
+
d.prototype =
|
|
23
|
+
b === null
|
|
24
|
+
? Object.create(b)
|
|
25
|
+
: ((__.prototype = b.prototype), new __());
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
15
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
29
|
var build_1 = require("./build");
|
|
17
30
|
function useGet(q, table, attrs, param, fromDB) {
|
|
@@ -29,8 +42,7 @@ var SqlLoader = (function () {
|
|
|
29
42
|
if (Array.isArray(attrs)) {
|
|
30
43
|
this.primaryKeys = build_1.attributes(attrs);
|
|
31
44
|
this.attributes = {};
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
45
|
+
} else {
|
|
34
46
|
var m = build_1.metadata(attrs);
|
|
35
47
|
this.attributes = attrs;
|
|
36
48
|
this.primaryKeys = m.keys;
|
|
@@ -54,34 +66,48 @@ var SqlLoader = (function () {
|
|
|
54
66
|
SqlLoader.prototype.load = function (id, ctx) {
|
|
55
67
|
var stmt = build_1.select(id, this.table, this.primaryKeys, this.param);
|
|
56
68
|
if (!stmt) {
|
|
57
|
-
throw new Error(
|
|
69
|
+
throw new Error("cannot build query by id");
|
|
58
70
|
}
|
|
59
71
|
var fn = this.fromDB;
|
|
60
72
|
if (fn) {
|
|
61
|
-
return this.query(stmt.query, stmt.params, this.map, ctx).then(function (
|
|
73
|
+
return this.query(stmt.query, stmt.params, this.map, ctx).then(function (
|
|
74
|
+
res
|
|
75
|
+
) {
|
|
62
76
|
if (!res || res.length === 0) {
|
|
63
77
|
return null;
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
78
|
+
} else {
|
|
66
79
|
var obj = res[0];
|
|
67
80
|
return fn(obj);
|
|
68
81
|
}
|
|
69
82
|
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
83
|
+
} else {
|
|
84
|
+
return this.query(stmt.query, stmt.params, this.map).then(function (res) {
|
|
85
|
+
return !res || res.length === 0 ? null : res[0];
|
|
86
|
+
});
|
|
73
87
|
}
|
|
74
88
|
};
|
|
75
89
|
SqlLoader.prototype.exist = function (id, ctx) {
|
|
76
|
-
var field =
|
|
77
|
-
|
|
90
|
+
var field = this.primaryKeys[0].column
|
|
91
|
+
? this.primaryKeys[0].column
|
|
92
|
+
: this.primaryKeys[0].name;
|
|
93
|
+
var stmt = build_1.exist(
|
|
94
|
+
id,
|
|
95
|
+
this.table,
|
|
96
|
+
this.primaryKeys,
|
|
97
|
+
this.param,
|
|
98
|
+
field
|
|
99
|
+
);
|
|
78
100
|
if (!stmt) {
|
|
79
|
-
throw new Error(
|
|
101
|
+
throw new Error("cannot build query by id");
|
|
80
102
|
}
|
|
81
|
-
return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(
|
|
103
|
+
return this.query(stmt.query, stmt.params, this.map, undefined, ctx).then(
|
|
104
|
+
function (res) {
|
|
105
|
+
return !res || res.length === 0 ? false : true;
|
|
106
|
+
}
|
|
107
|
+
);
|
|
82
108
|
};
|
|
83
109
|
return SqlLoader;
|
|
84
|
-
}()
|
|
110
|
+
})();
|
|
85
111
|
exports.SqlLoader = SqlLoader;
|
|
86
112
|
var QueryRepository = (function () {
|
|
87
113
|
function QueryRepository(db, table, attrs, sort, id) {
|
|
@@ -89,7 +115,7 @@ var QueryRepository = (function () {
|
|
|
89
115
|
this.table = table;
|
|
90
116
|
this.attrs = attrs;
|
|
91
117
|
this.sort = sort;
|
|
92
|
-
this.id =
|
|
118
|
+
this.id = id && id.length > 0 ? id : "id";
|
|
93
119
|
this.query = this.query.bind(this);
|
|
94
120
|
var m = build_1.metadata(attrs);
|
|
95
121
|
this.map = m.map;
|
|
@@ -104,17 +130,34 @@ var QueryRepository = (function () {
|
|
|
104
130
|
for (var i = 1; i <= length; i++) {
|
|
105
131
|
ps.push(this.db.param(i));
|
|
106
132
|
}
|
|
107
|
-
var sql =
|
|
133
|
+
var sql =
|
|
134
|
+
"select * from " +
|
|
135
|
+
this.table +
|
|
136
|
+
" where " +
|
|
137
|
+
this.id +
|
|
138
|
+
" in (" +
|
|
139
|
+
ps.join(",") +
|
|
140
|
+
")";
|
|
108
141
|
if (this.sort && this.sort.length > 0) {
|
|
109
|
-
sql = sql +
|
|
142
|
+
sql = sql + " order by " + this.sort;
|
|
110
143
|
}
|
|
111
144
|
return this.db.query(sql, ids, this.map, this.bools);
|
|
112
145
|
};
|
|
113
146
|
return QueryRepository;
|
|
114
|
-
}()
|
|
147
|
+
})();
|
|
115
148
|
exports.QueryRepository = QueryRepository;
|
|
116
149
|
var SqlLoadRepository = (function () {
|
|
117
|
-
function SqlLoadRepository(
|
|
150
|
+
function SqlLoadRepository(
|
|
151
|
+
query,
|
|
152
|
+
table,
|
|
153
|
+
attrs,
|
|
154
|
+
param,
|
|
155
|
+
id1Field,
|
|
156
|
+
id2Field,
|
|
157
|
+
fromDB,
|
|
158
|
+
id1Col,
|
|
159
|
+
id2Col
|
|
160
|
+
) {
|
|
118
161
|
this.query = query;
|
|
119
162
|
this.table = table;
|
|
120
163
|
this.param = param;
|
|
@@ -133,25 +176,23 @@ var SqlLoadRepository = (function () {
|
|
|
133
176
|
this.exist = this.exist.bind(this);
|
|
134
177
|
if (id1Col && id1Col.length > 0) {
|
|
135
178
|
this.id1Col = id1Col;
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
179
|
+
} else {
|
|
138
180
|
var c = attrs[this.id1Field];
|
|
139
181
|
if (c) {
|
|
140
|
-
this.id1Col =
|
|
141
|
-
|
|
142
|
-
else {
|
|
182
|
+
this.id1Col =
|
|
183
|
+
c.column && c.column.length > 0 ? c.column : this.id1Field;
|
|
184
|
+
} else {
|
|
143
185
|
this.id1Col = this.id1Field;
|
|
144
186
|
}
|
|
145
187
|
}
|
|
146
188
|
if (id2Col && id2Col.length > 0) {
|
|
147
189
|
this.id2Col = id2Col;
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
190
|
+
} else {
|
|
150
191
|
var c = attrs[this.id2Field];
|
|
151
192
|
if (c) {
|
|
152
|
-
this.id2Col =
|
|
153
|
-
|
|
154
|
-
else {
|
|
193
|
+
this.id2Col =
|
|
194
|
+
c.column && c.column.length > 0 ? c.column : this.id2Field;
|
|
195
|
+
} else {
|
|
155
196
|
this.id2Col = this.id2Field;
|
|
156
197
|
}
|
|
157
198
|
}
|
|
@@ -165,33 +206,85 @@ var SqlLoadRepository = (function () {
|
|
|
165
206
|
};
|
|
166
207
|
SqlLoadRepository.prototype.load = function (id1, id2, ctx) {
|
|
167
208
|
var _this = this;
|
|
168
|
-
return this.query(
|
|
209
|
+
return this.query(
|
|
210
|
+
"select * from " +
|
|
211
|
+
this.table +
|
|
212
|
+
" where " +
|
|
213
|
+
this.id1Col +
|
|
214
|
+
" = " +
|
|
215
|
+
this.param(1) +
|
|
216
|
+
" and " +
|
|
217
|
+
this.id2Col +
|
|
218
|
+
" = " +
|
|
219
|
+
this.param(2),
|
|
220
|
+
[id1, id2],
|
|
221
|
+
this.map,
|
|
222
|
+
undefined,
|
|
223
|
+
ctx
|
|
224
|
+
).then(function (objs) {
|
|
169
225
|
if (!objs || objs.length === 0) {
|
|
170
226
|
return null;
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
227
|
+
} else {
|
|
173
228
|
var fn = _this.fromDB;
|
|
174
229
|
if (fn) {
|
|
175
230
|
return fn(objs[0]);
|
|
176
|
-
}
|
|
177
|
-
else {
|
|
231
|
+
} else {
|
|
178
232
|
return objs[0];
|
|
179
233
|
}
|
|
180
234
|
}
|
|
181
235
|
});
|
|
182
236
|
};
|
|
183
237
|
SqlLoadRepository.prototype.exist = function (id1, id2, ctx) {
|
|
184
|
-
return this.query(
|
|
185
|
-
|
|
238
|
+
return this.query(
|
|
239
|
+
"select " +
|
|
240
|
+
this.id1Col +
|
|
241
|
+
" from " +
|
|
242
|
+
this.table +
|
|
243
|
+
" where " +
|
|
244
|
+
this.id1Col +
|
|
245
|
+
" = " +
|
|
246
|
+
this.param(1) +
|
|
247
|
+
" and " +
|
|
248
|
+
this.id2Col +
|
|
249
|
+
" = " +
|
|
250
|
+
this.param(2),
|
|
251
|
+
[id1, id2],
|
|
252
|
+
undefined,
|
|
253
|
+
undefined,
|
|
254
|
+
ctx
|
|
255
|
+
).then(function (objs) {
|
|
256
|
+
return objs && objs.length > 0 ? true : false;
|
|
186
257
|
});
|
|
187
258
|
};
|
|
188
259
|
return SqlLoadRepository;
|
|
189
|
-
}()
|
|
260
|
+
})();
|
|
190
261
|
exports.SqlLoadRepository = SqlLoadRepository;
|
|
191
262
|
var GenericRepository = (function (_super) {
|
|
192
263
|
__extends(GenericRepository, _super);
|
|
193
|
-
function GenericRepository(
|
|
194
|
-
|
|
264
|
+
function GenericRepository(
|
|
265
|
+
manager,
|
|
266
|
+
table,
|
|
267
|
+
attrs,
|
|
268
|
+
id1Field,
|
|
269
|
+
id2Field,
|
|
270
|
+
toDB,
|
|
271
|
+
fromDB,
|
|
272
|
+
id1Col,
|
|
273
|
+
id2Col
|
|
274
|
+
) {
|
|
275
|
+
var _this =
|
|
276
|
+
_super.call(
|
|
277
|
+
this,
|
|
278
|
+
manager.query,
|
|
279
|
+
table,
|
|
280
|
+
attrs,
|
|
281
|
+
manager.param,
|
|
282
|
+
id1Field,
|
|
283
|
+
id2Field,
|
|
284
|
+
fromDB,
|
|
285
|
+
id1Col,
|
|
286
|
+
id2Col
|
|
287
|
+
) || this;
|
|
195
288
|
_this.toDB = toDB;
|
|
196
289
|
var x = build_1.version(attrs);
|
|
197
290
|
_this.exec = manager.exec;
|
|
@@ -199,29 +292,33 @@ var GenericRepository = (function (_super) {
|
|
|
199
292
|
if (x) {
|
|
200
293
|
_this.version = x.name;
|
|
201
294
|
}
|
|
202
|
-
_this.
|
|
295
|
+
_this.create = _this.create.bind(_this);
|
|
203
296
|
_this.update = _this.update.bind(_this);
|
|
204
297
|
_this.patch = _this.patch.bind(_this);
|
|
205
298
|
_this.delete = _this.delete.bind(_this);
|
|
206
299
|
return _this;
|
|
207
300
|
}
|
|
208
|
-
GenericRepository.prototype.
|
|
301
|
+
GenericRepository.prototype.create = function (obj, ctx) {
|
|
209
302
|
var obj2 = obj;
|
|
210
303
|
if (this.toDB) {
|
|
211
304
|
obj2 = this.toDB(obj);
|
|
212
305
|
}
|
|
213
|
-
var stmt = build_1.buildToInsert(
|
|
306
|
+
var stmt = build_1.buildToInsert(
|
|
307
|
+
obj2,
|
|
308
|
+
this.table,
|
|
309
|
+
this.attributes,
|
|
310
|
+
this.param,
|
|
311
|
+
this.version
|
|
312
|
+
);
|
|
214
313
|
if (stmt) {
|
|
215
314
|
return this.exec(stmt.query, stmt.params, ctx).catch(function (err) {
|
|
216
|
-
if (err && err.error ===
|
|
315
|
+
if (err && err.error === "duplicate") {
|
|
217
316
|
return 0;
|
|
218
|
-
}
|
|
219
|
-
else {
|
|
317
|
+
} else {
|
|
220
318
|
throw err;
|
|
221
319
|
}
|
|
222
320
|
});
|
|
223
|
-
}
|
|
224
|
-
else {
|
|
321
|
+
} else {
|
|
225
322
|
return Promise.resolve(0);
|
|
226
323
|
}
|
|
227
324
|
};
|
|
@@ -230,11 +327,16 @@ var GenericRepository = (function (_super) {
|
|
|
230
327
|
if (this.toDB) {
|
|
231
328
|
obj2 = this.toDB(obj);
|
|
232
329
|
}
|
|
233
|
-
var stmt = build_1.buildToUpdate(
|
|
330
|
+
var stmt = build_1.buildToUpdate(
|
|
331
|
+
obj2,
|
|
332
|
+
this.table,
|
|
333
|
+
this.attributes,
|
|
334
|
+
this.param,
|
|
335
|
+
this.version
|
|
336
|
+
);
|
|
234
337
|
if (stmt) {
|
|
235
338
|
return this.exec(stmt.query, stmt.params, ctx);
|
|
236
|
-
}
|
|
237
|
-
else {
|
|
339
|
+
} else {
|
|
238
340
|
return Promise.resolve(0);
|
|
239
341
|
}
|
|
240
342
|
};
|
|
@@ -242,10 +344,23 @@ var GenericRepository = (function (_super) {
|
|
|
242
344
|
return this.update(obj, ctx);
|
|
243
345
|
};
|
|
244
346
|
GenericRepository.prototype.delete = function (id1, id2, ctx) {
|
|
245
|
-
return this.exec(
|
|
347
|
+
return this.exec(
|
|
348
|
+
"delete from " +
|
|
349
|
+
this.table +
|
|
350
|
+
" where " +
|
|
351
|
+
this.id1Col +
|
|
352
|
+
" = " +
|
|
353
|
+
this.param(1) +
|
|
354
|
+
" and " +
|
|
355
|
+
this.id2Col +
|
|
356
|
+
" = " +
|
|
357
|
+
this.param(2),
|
|
358
|
+
[id1, id2],
|
|
359
|
+
ctx
|
|
360
|
+
);
|
|
246
361
|
};
|
|
247
362
|
return GenericRepository;
|
|
248
|
-
}(SqlLoadRepository)
|
|
363
|
+
})(SqlLoadRepository);
|
|
249
364
|
exports.GenericRepository = GenericRepository;
|
|
250
365
|
var SqlSearchLoader = (function (_super) {
|
|
251
366
|
__extends(SqlSearchLoader, _super);
|
|
@@ -259,7 +374,7 @@ var SqlSearchLoader = (function (_super) {
|
|
|
259
374
|
return this.find(s, limit, offset, fields);
|
|
260
375
|
};
|
|
261
376
|
return SqlSearchLoader;
|
|
262
|
-
}(SqlLoader)
|
|
377
|
+
})(SqlLoader);
|
|
263
378
|
exports.SqlSearchLoader = SqlSearchLoader;
|
|
264
379
|
function log(db, isLog, logger, q, result, r, duration) {
|
|
265
380
|
if (!isLog) {
|
|
@@ -269,7 +384,15 @@ function log(db, isLog, logger, q, result, r, duration) {
|
|
|
269
384
|
if (!logger.isDebugEnabled()) {
|
|
270
385
|
return db;
|
|
271
386
|
}
|
|
272
|
-
return new LogManager(
|
|
387
|
+
return new LogManager(
|
|
388
|
+
db,
|
|
389
|
+
logger.error,
|
|
390
|
+
logger.debug,
|
|
391
|
+
q,
|
|
392
|
+
result,
|
|
393
|
+
r,
|
|
394
|
+
duration
|
|
395
|
+
);
|
|
273
396
|
}
|
|
274
397
|
if (!logger.isInfoEnabled()) {
|
|
275
398
|
return db;
|
|
@@ -291,10 +414,10 @@ var LogManager = (function () {
|
|
|
291
414
|
function LogManager(db, err, lg, q, result, r, duration) {
|
|
292
415
|
this.db = db;
|
|
293
416
|
this.driver = db.driver;
|
|
294
|
-
this.duration =
|
|
295
|
-
this.sql =
|
|
296
|
-
this.return =
|
|
297
|
-
this.result =
|
|
417
|
+
this.duration = duration && duration.length > 0 ? duration : "duration";
|
|
418
|
+
this.sql = q === undefined ? "" : q;
|
|
419
|
+
this.return = r !== undefined && r != null ? r : "count";
|
|
420
|
+
this.result = result !== undefined && result != null ? result : "";
|
|
298
421
|
this.log = lg;
|
|
299
422
|
this.error = err;
|
|
300
423
|
this.param = this.param.bind(this);
|
|
@@ -311,222 +434,238 @@ var LogManager = (function () {
|
|
|
311
434
|
LogManager.prototype.exec = function (sql, args, ctx) {
|
|
312
435
|
var _this = this;
|
|
313
436
|
var t1 = new Date();
|
|
314
|
-
return this.db
|
|
315
|
-
|
|
316
|
-
|
|
437
|
+
return this.db
|
|
438
|
+
.exec(sql, args, ctx)
|
|
439
|
+
.then(function (v) {
|
|
440
|
+
setTimeout(function () {
|
|
441
|
+
if (_this.log) {
|
|
442
|
+
var d = diff(t1);
|
|
443
|
+
var obj = {};
|
|
444
|
+
if (_this.sql.length > 0) {
|
|
445
|
+
obj[_this.sql] = getString(sql, args);
|
|
446
|
+
}
|
|
447
|
+
if (_this.return.length > 0) {
|
|
448
|
+
obj[_this.return] = v;
|
|
449
|
+
}
|
|
450
|
+
obj[_this.duration] = d;
|
|
451
|
+
_this.log("query", obj);
|
|
452
|
+
}
|
|
453
|
+
}, 0);
|
|
454
|
+
return v;
|
|
455
|
+
})
|
|
456
|
+
.catch(function (er) {
|
|
457
|
+
setTimeout(function () {
|
|
317
458
|
var d = diff(t1);
|
|
318
459
|
var obj = {};
|
|
319
460
|
if (_this.sql.length > 0) {
|
|
320
461
|
obj[_this.sql] = getString(sql, args);
|
|
321
462
|
}
|
|
322
|
-
if (_this.return.length > 0) {
|
|
323
|
-
obj[_this.return] = v;
|
|
324
|
-
}
|
|
325
463
|
obj[_this.duration] = d;
|
|
326
|
-
_this.
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
}).catch(function (er) {
|
|
331
|
-
setTimeout(function () {
|
|
332
|
-
var d = diff(t1);
|
|
333
|
-
var obj = {};
|
|
334
|
-
if (_this.sql.length > 0) {
|
|
335
|
-
obj[_this.sql] = getString(sql, args);
|
|
336
|
-
}
|
|
337
|
-
obj[_this.duration] = d;
|
|
338
|
-
_this.error('error query: ' + buildString(er));
|
|
339
|
-
}, 0);
|
|
340
|
-
throw er;
|
|
341
|
-
});
|
|
464
|
+
_this.error("error query: " + buildString(er));
|
|
465
|
+
}, 0);
|
|
466
|
+
throw er;
|
|
467
|
+
});
|
|
342
468
|
};
|
|
343
469
|
LogManager.prototype.execBatch = function (statements, firstSuccess, ctx) {
|
|
344
470
|
var _this = this;
|
|
345
471
|
var t1 = new Date();
|
|
346
|
-
return this.db
|
|
347
|
-
|
|
348
|
-
|
|
472
|
+
return this.db
|
|
473
|
+
.execBatch(statements, firstSuccess, ctx)
|
|
474
|
+
.then(function (v) {
|
|
475
|
+
setTimeout(function () {
|
|
476
|
+
if (_this.log) {
|
|
477
|
+
var d = diff(t1);
|
|
478
|
+
var obj = {};
|
|
479
|
+
if (_this.sql.length > 0) {
|
|
480
|
+
obj[_this.sql] = JSON.stringify(statements);
|
|
481
|
+
}
|
|
482
|
+
if (_this.return.length > 0) {
|
|
483
|
+
obj[_this.return] = v;
|
|
484
|
+
}
|
|
485
|
+
obj[_this.duration] = d;
|
|
486
|
+
_this.log("exec batch", obj);
|
|
487
|
+
}
|
|
488
|
+
}, 0);
|
|
489
|
+
return v;
|
|
490
|
+
})
|
|
491
|
+
.catch(function (er) {
|
|
492
|
+
setTimeout(function () {
|
|
349
493
|
var d = diff(t1);
|
|
350
494
|
var obj = {};
|
|
351
495
|
if (_this.sql.length > 0) {
|
|
352
496
|
obj[_this.sql] = JSON.stringify(statements);
|
|
353
497
|
}
|
|
354
|
-
if (_this.return.length > 0) {
|
|
355
|
-
obj[_this.return] = v;
|
|
356
|
-
}
|
|
357
498
|
obj[_this.duration] = d;
|
|
358
|
-
_this.
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
}).catch(function (er) {
|
|
363
|
-
setTimeout(function () {
|
|
364
|
-
var d = diff(t1);
|
|
365
|
-
var obj = {};
|
|
366
|
-
if (_this.sql.length > 0) {
|
|
367
|
-
obj[_this.sql] = JSON.stringify(statements);
|
|
368
|
-
}
|
|
369
|
-
obj[_this.duration] = d;
|
|
370
|
-
_this.error('error exec batch: ' + buildString(er));
|
|
371
|
-
}, 0);
|
|
372
|
-
throw er;
|
|
373
|
-
});
|
|
499
|
+
_this.error("error exec batch: " + buildString(er));
|
|
500
|
+
}, 0);
|
|
501
|
+
throw er;
|
|
502
|
+
});
|
|
374
503
|
};
|
|
375
504
|
LogManager.prototype.query = function (sql, args, m, bools, ctx) {
|
|
376
505
|
var _this = this;
|
|
377
506
|
var t1 = new Date();
|
|
378
|
-
return this.db
|
|
379
|
-
|
|
380
|
-
|
|
507
|
+
return this.db
|
|
508
|
+
.query(sql, args, m, bools, ctx)
|
|
509
|
+
.then(function (v) {
|
|
510
|
+
setTimeout(function () {
|
|
511
|
+
if (_this.log) {
|
|
512
|
+
var d = diff(t1);
|
|
513
|
+
var obj = {};
|
|
514
|
+
if (_this.sql.length > 0) {
|
|
515
|
+
obj[_this.sql] = getString(sql, args);
|
|
516
|
+
}
|
|
517
|
+
if (_this.result.length > 0) {
|
|
518
|
+
if (v && v.length > 0) {
|
|
519
|
+
obj[_this.result] = JSON.stringify(v);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
if (_this.return.length > 0) {
|
|
523
|
+
obj[_this.return] = v ? v.length : 0;
|
|
524
|
+
}
|
|
525
|
+
obj[_this.duration] = d;
|
|
526
|
+
_this.log("query", obj);
|
|
527
|
+
}
|
|
528
|
+
}, 0);
|
|
529
|
+
return v;
|
|
530
|
+
})
|
|
531
|
+
.catch(function (er) {
|
|
532
|
+
setTimeout(function () {
|
|
381
533
|
var d = diff(t1);
|
|
382
534
|
var obj = {};
|
|
383
535
|
if (_this.sql.length > 0) {
|
|
384
536
|
obj[_this.sql] = getString(sql, args);
|
|
385
537
|
}
|
|
386
|
-
if (_this.result.length > 0) {
|
|
387
|
-
if (v && v.length > 0) {
|
|
388
|
-
obj[_this.result] = JSON.stringify(v);
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
if (_this.return.length > 0) {
|
|
392
|
-
obj[_this.return] = v ? v.length : 0;
|
|
393
|
-
}
|
|
394
538
|
obj[_this.duration] = d;
|
|
395
|
-
_this.
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
}).catch(function (er) {
|
|
400
|
-
setTimeout(function () {
|
|
401
|
-
var d = diff(t1);
|
|
402
|
-
var obj = {};
|
|
403
|
-
if (_this.sql.length > 0) {
|
|
404
|
-
obj[_this.sql] = getString(sql, args);
|
|
405
|
-
}
|
|
406
|
-
obj[_this.duration] = d;
|
|
407
|
-
_this.error('error query: ' + buildString(er));
|
|
408
|
-
}, 0);
|
|
409
|
-
throw er;
|
|
410
|
-
});
|
|
539
|
+
_this.error("error query: " + buildString(er));
|
|
540
|
+
}, 0);
|
|
541
|
+
throw er;
|
|
542
|
+
});
|
|
411
543
|
};
|
|
412
544
|
LogManager.prototype.queryOne = function (sql, args, m, bools, ctx) {
|
|
413
545
|
var _this = this;
|
|
414
546
|
var t1 = new Date();
|
|
415
|
-
return this.db
|
|
416
|
-
|
|
417
|
-
|
|
547
|
+
return this.db
|
|
548
|
+
.queryOne(sql, args, m, bools, ctx)
|
|
549
|
+
.then(function (v) {
|
|
550
|
+
setTimeout(function () {
|
|
551
|
+
if (_this.log) {
|
|
552
|
+
var d = diff(t1);
|
|
553
|
+
var obj = {};
|
|
554
|
+
if (_this.sql.length > 0) {
|
|
555
|
+
obj[_this.sql] = getString(sql, args);
|
|
556
|
+
}
|
|
557
|
+
if (_this.result.length > 0) {
|
|
558
|
+
obj[_this.result] = v ? JSON.stringify(v) : "null";
|
|
559
|
+
}
|
|
560
|
+
if (_this.return.length > 0) {
|
|
561
|
+
obj[_this.return] = v ? 1 : 0;
|
|
562
|
+
}
|
|
563
|
+
obj[_this.duration] = d;
|
|
564
|
+
_this.log("query one", obj);
|
|
565
|
+
}
|
|
566
|
+
}, 0);
|
|
567
|
+
return v;
|
|
568
|
+
})
|
|
569
|
+
.catch(function (er) {
|
|
570
|
+
setTimeout(function () {
|
|
418
571
|
var d = diff(t1);
|
|
419
572
|
var obj = {};
|
|
420
573
|
if (_this.sql.length > 0) {
|
|
421
574
|
obj[_this.sql] = getString(sql, args);
|
|
422
575
|
}
|
|
423
|
-
if (_this.result.length > 0) {
|
|
424
|
-
obj[_this.result] = v ? JSON.stringify(v) : 'null';
|
|
425
|
-
}
|
|
426
|
-
if (_this.return.length > 0) {
|
|
427
|
-
obj[_this.return] = v ? 1 : 0;
|
|
428
|
-
}
|
|
429
576
|
obj[_this.duration] = d;
|
|
430
|
-
_this.
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
}).catch(function (er) {
|
|
435
|
-
setTimeout(function () {
|
|
436
|
-
var d = diff(t1);
|
|
437
|
-
var obj = {};
|
|
438
|
-
if (_this.sql.length > 0) {
|
|
439
|
-
obj[_this.sql] = getString(sql, args);
|
|
440
|
-
}
|
|
441
|
-
obj[_this.duration] = d;
|
|
442
|
-
_this.error('error query one: ' + buildString(er));
|
|
443
|
-
}, 0);
|
|
444
|
-
throw er;
|
|
445
|
-
});
|
|
577
|
+
_this.error("error query one: " + buildString(er));
|
|
578
|
+
}, 0);
|
|
579
|
+
throw er;
|
|
580
|
+
});
|
|
446
581
|
};
|
|
447
582
|
LogManager.prototype.execScalar = function (sql, args, ctx) {
|
|
448
583
|
var _this = this;
|
|
449
584
|
var t1 = new Date();
|
|
450
|
-
return this.db
|
|
451
|
-
|
|
452
|
-
|
|
585
|
+
return this.db
|
|
586
|
+
.execScalar(sql, args, ctx)
|
|
587
|
+
.then(function (v) {
|
|
588
|
+
setTimeout(function () {
|
|
589
|
+
if (_this.log) {
|
|
590
|
+
var d = diff(t1);
|
|
591
|
+
var obj = {};
|
|
592
|
+
if (_this.sql.length > 0) {
|
|
593
|
+
obj[_this.sql] = getString(sql, args);
|
|
594
|
+
}
|
|
595
|
+
if (_this.result.length > 0) {
|
|
596
|
+
obj[_this.result] = v ? buildString(v) : "null";
|
|
597
|
+
}
|
|
598
|
+
if (_this.return.length > 0) {
|
|
599
|
+
obj[_this.return] = v ? 1 : 0;
|
|
600
|
+
}
|
|
601
|
+
obj[_this.duration] = d;
|
|
602
|
+
_this.log("exec scalar", obj);
|
|
603
|
+
}
|
|
604
|
+
}, 0);
|
|
605
|
+
return v;
|
|
606
|
+
})
|
|
607
|
+
.catch(function (er) {
|
|
608
|
+
setTimeout(function () {
|
|
453
609
|
var d = diff(t1);
|
|
454
610
|
var obj = {};
|
|
455
611
|
if (_this.sql.length > 0) {
|
|
456
612
|
obj[_this.sql] = getString(sql, args);
|
|
457
613
|
}
|
|
458
|
-
if (_this.result.length > 0) {
|
|
459
|
-
obj[_this.result] = v ? buildString(v) : 'null';
|
|
460
|
-
}
|
|
461
|
-
if (_this.return.length > 0) {
|
|
462
|
-
obj[_this.return] = v ? 1 : 0;
|
|
463
|
-
}
|
|
464
614
|
obj[_this.duration] = d;
|
|
465
|
-
_this.
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
}).catch(function (er) {
|
|
470
|
-
setTimeout(function () {
|
|
471
|
-
var d = diff(t1);
|
|
472
|
-
var obj = {};
|
|
473
|
-
if (_this.sql.length > 0) {
|
|
474
|
-
obj[_this.sql] = getString(sql, args);
|
|
475
|
-
}
|
|
476
|
-
obj[_this.duration] = d;
|
|
477
|
-
_this.error('error exec scalar: ' + buildString(er));
|
|
478
|
-
}, 0);
|
|
479
|
-
throw er;
|
|
480
|
-
});
|
|
615
|
+
_this.error("error exec scalar: " + buildString(er));
|
|
616
|
+
}, 0);
|
|
617
|
+
throw er;
|
|
618
|
+
});
|
|
481
619
|
};
|
|
482
620
|
LogManager.prototype.count = function (sql, args, ctx) {
|
|
483
621
|
var _this = this;
|
|
484
622
|
var t1 = new Date();
|
|
485
|
-
return this.db
|
|
486
|
-
|
|
487
|
-
|
|
623
|
+
return this.db
|
|
624
|
+
.count(sql, args)
|
|
625
|
+
.then(function (v) {
|
|
626
|
+
setTimeout(function () {
|
|
627
|
+
if (_this.log) {
|
|
628
|
+
var d = diff(t1);
|
|
629
|
+
var obj = {};
|
|
630
|
+
if (_this.sql.length > 0) {
|
|
631
|
+
obj[_this.sql] = getString(sql, args);
|
|
632
|
+
}
|
|
633
|
+
if (_this.return.length > 0) {
|
|
634
|
+
obj[_this.return] = v;
|
|
635
|
+
}
|
|
636
|
+
obj[_this.duration] = d;
|
|
637
|
+
_this.log("count", obj);
|
|
638
|
+
}
|
|
639
|
+
}, 0);
|
|
640
|
+
return v;
|
|
641
|
+
})
|
|
642
|
+
.catch(function (er) {
|
|
643
|
+
setTimeout(function () {
|
|
488
644
|
var d = diff(t1);
|
|
489
645
|
var obj = {};
|
|
490
646
|
if (_this.sql.length > 0) {
|
|
491
647
|
obj[_this.sql] = getString(sql, args);
|
|
492
648
|
}
|
|
493
|
-
if (_this.return.length > 0) {
|
|
494
|
-
obj[_this.return] = v;
|
|
495
|
-
}
|
|
496
649
|
obj[_this.duration] = d;
|
|
497
|
-
_this.
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
}).catch(function (er) {
|
|
502
|
-
setTimeout(function () {
|
|
503
|
-
var d = diff(t1);
|
|
504
|
-
var obj = {};
|
|
505
|
-
if (_this.sql.length > 0) {
|
|
506
|
-
obj[_this.sql] = getString(sql, args);
|
|
507
|
-
}
|
|
508
|
-
obj[_this.duration] = d;
|
|
509
|
-
_this.error('error count: ' + buildString(er));
|
|
510
|
-
}, 0);
|
|
511
|
-
throw er;
|
|
512
|
-
});
|
|
650
|
+
_this.error("error count: " + buildString(er));
|
|
651
|
+
}, 0);
|
|
652
|
+
throw er;
|
|
653
|
+
});
|
|
513
654
|
};
|
|
514
655
|
return LogManager;
|
|
515
|
-
}()
|
|
656
|
+
})();
|
|
516
657
|
exports.LogManager = LogManager;
|
|
517
658
|
function buildString(v) {
|
|
518
|
-
if (typeof v ===
|
|
659
|
+
if (typeof v === "string") {
|
|
519
660
|
return v;
|
|
520
|
-
}
|
|
521
|
-
else {
|
|
661
|
+
} else {
|
|
522
662
|
return JSON.stringify(v);
|
|
523
663
|
}
|
|
524
664
|
}
|
|
525
665
|
function getString(sql, args) {
|
|
526
666
|
if (args && args.length > 0) {
|
|
527
|
-
return sql +
|
|
528
|
-
}
|
|
529
|
-
else {
|
|
667
|
+
return sql + " " + JSON.stringify(args);
|
|
668
|
+
} else {
|
|
530
669
|
return sql;
|
|
531
670
|
}
|
|
532
671
|
}
|
|
@@ -538,7 +677,9 @@ exports.diff = diff;
|
|
|
538
677
|
var SqlWriter = (function (_super) {
|
|
539
678
|
__extends(SqlWriter, _super);
|
|
540
679
|
function SqlWriter(manager, table, attrs, toDB, fromDB) {
|
|
541
|
-
var _this =
|
|
680
|
+
var _this =
|
|
681
|
+
_super.call(this, manager.query, table, attrs, manager.param, fromDB) ||
|
|
682
|
+
this;
|
|
542
683
|
_this.toDB = toDB;
|
|
543
684
|
var x = build_1.version(attrs);
|
|
544
685
|
_this.exec = manager.exec;
|
|
@@ -546,29 +687,33 @@ var SqlWriter = (function (_super) {
|
|
|
546
687
|
if (x) {
|
|
547
688
|
_this.version = x.name;
|
|
548
689
|
}
|
|
549
|
-
_this.
|
|
690
|
+
_this.create = _this.create.bind(_this);
|
|
550
691
|
_this.update = _this.update.bind(_this);
|
|
551
692
|
_this.patch = _this.patch.bind(_this);
|
|
552
693
|
_this.delete = _this.delete.bind(_this);
|
|
553
694
|
return _this;
|
|
554
695
|
}
|
|
555
|
-
SqlWriter.prototype.
|
|
696
|
+
SqlWriter.prototype.create = function (obj, ctx) {
|
|
556
697
|
var obj2 = obj;
|
|
557
698
|
if (this.toDB) {
|
|
558
699
|
obj2 = this.toDB(obj);
|
|
559
700
|
}
|
|
560
|
-
var stmt = build_1.buildToInsert(
|
|
701
|
+
var stmt = build_1.buildToInsert(
|
|
702
|
+
obj2,
|
|
703
|
+
this.table,
|
|
704
|
+
this.attributes,
|
|
705
|
+
this.param,
|
|
706
|
+
this.version
|
|
707
|
+
);
|
|
561
708
|
if (stmt) {
|
|
562
709
|
return this.exec(stmt.query, stmt.params, ctx).catch(function (err) {
|
|
563
|
-
if (err && err.error ===
|
|
710
|
+
if (err && err.error === "duplicate") {
|
|
564
711
|
return 0;
|
|
565
|
-
}
|
|
566
|
-
else {
|
|
712
|
+
} else {
|
|
567
713
|
throw err;
|
|
568
714
|
}
|
|
569
715
|
});
|
|
570
|
-
}
|
|
571
|
-
else {
|
|
716
|
+
} else {
|
|
572
717
|
return Promise.resolve(0);
|
|
573
718
|
}
|
|
574
719
|
};
|
|
@@ -577,11 +722,16 @@ var SqlWriter = (function (_super) {
|
|
|
577
722
|
if (this.toDB) {
|
|
578
723
|
obj2 = this.toDB(obj);
|
|
579
724
|
}
|
|
580
|
-
var stmt = build_1.buildToUpdate(
|
|
725
|
+
var stmt = build_1.buildToUpdate(
|
|
726
|
+
obj2,
|
|
727
|
+
this.table,
|
|
728
|
+
this.attributes,
|
|
729
|
+
this.param,
|
|
730
|
+
this.version
|
|
731
|
+
);
|
|
581
732
|
if (stmt) {
|
|
582
733
|
return this.exec(stmt.query, stmt.params, ctx);
|
|
583
|
-
}
|
|
584
|
-
else {
|
|
734
|
+
} else {
|
|
585
735
|
return Promise.resolve(0);
|
|
586
736
|
}
|
|
587
737
|
};
|
|
@@ -589,16 +739,20 @@ var SqlWriter = (function (_super) {
|
|
|
589
739
|
return this.update(obj, ctx);
|
|
590
740
|
};
|
|
591
741
|
SqlWriter.prototype.delete = function (id, ctx) {
|
|
592
|
-
var stmt = build_1.buildToDelete(
|
|
742
|
+
var stmt = build_1.buildToDelete(
|
|
743
|
+
id,
|
|
744
|
+
this.table,
|
|
745
|
+
this.primaryKeys,
|
|
746
|
+
this.param
|
|
747
|
+
);
|
|
593
748
|
if (stmt) {
|
|
594
749
|
return this.exec(stmt.query, stmt.params, ctx);
|
|
595
|
-
}
|
|
596
|
-
else {
|
|
750
|
+
} else {
|
|
597
751
|
return Promise.resolve(0);
|
|
598
752
|
}
|
|
599
753
|
};
|
|
600
754
|
return SqlWriter;
|
|
601
|
-
}(SqlLoader)
|
|
755
|
+
})(SqlLoader);
|
|
602
756
|
exports.SqlWriter = SqlWriter;
|
|
603
757
|
var SqlSearchWriter = (function (_super) {
|
|
604
758
|
__extends(SqlSearchWriter, _super);
|
|
@@ -612,5 +766,5 @@ var SqlSearchWriter = (function (_super) {
|
|
|
612
766
|
return this.find(s, limit, offset, fields);
|
|
613
767
|
};
|
|
614
768
|
return SqlSearchWriter;
|
|
615
|
-
}(SqlWriter)
|
|
769
|
+
})(SqlWriter);
|
|
616
770
|
exports.SqlSearchWriter = SqlSearchWriter;
|
package/package.json
CHANGED
package/src/services.ts
CHANGED
|
@@ -207,12 +207,12 @@ export class GenericRepository<T, K1, K2> extends SqlLoadRepository<T, K1, K2> {
|
|
|
207
207
|
if (x) {
|
|
208
208
|
this.version = x.name;
|
|
209
209
|
}
|
|
210
|
-
this.
|
|
210
|
+
this.create = this.create.bind(this);
|
|
211
211
|
this.update = this.update.bind(this);
|
|
212
212
|
this.patch = this.patch.bind(this);
|
|
213
213
|
this.delete = this.delete.bind(this);
|
|
214
214
|
}
|
|
215
|
-
|
|
215
|
+
create(obj: T, ctx?: any): Promise<number> {
|
|
216
216
|
let obj2 = obj;
|
|
217
217
|
if (this.toDB) {
|
|
218
218
|
obj2 = this.toDB(obj);
|
|
@@ -588,12 +588,12 @@ export class SqlWriter<T, ID> extends SqlLoader<T, ID> {
|
|
|
588
588
|
if (x) {
|
|
589
589
|
this.version = x.name;
|
|
590
590
|
}
|
|
591
|
-
this.
|
|
591
|
+
this.create = this.create.bind(this);
|
|
592
592
|
this.update = this.update.bind(this);
|
|
593
593
|
this.patch = this.patch.bind(this);
|
|
594
594
|
this.delete = this.delete.bind(this);
|
|
595
595
|
}
|
|
596
|
-
|
|
596
|
+
create(obj: T, ctx?: any): Promise<number> {
|
|
597
597
|
let obj2 = obj;
|
|
598
598
|
if (this.toDB) {
|
|
599
599
|
obj2 = this.toDB(obj);
|