oak-db 3.0.0 → 3.0.1

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.
@@ -1,439 +1,439 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MysqlStore = void 0;
4
- var tslib_1 = require("tslib");
5
- var CascadeStore_1 = require("oak-domain/lib/store/CascadeStore");
6
- var connector_1 = require("./connector");
7
- var translator_1 = require("./translator");
8
- var lodash_1 = require("lodash");
9
- var assert_1 = tslib_1.__importDefault(require("assert"));
10
- var relation_1 = require("oak-domain/lib/store/relation");
11
- function convertGeoTextToObject(geoText) {
12
- if (geoText.startsWith('POINT')) {
13
- var coord = geoText.match((/(\d|\.)+(?=\)|\s)/g));
14
- return {
15
- type: 'Point',
16
- coordinate: coord.map(function (ele) { return parseFloat(ele); }),
17
- };
18
- }
19
- else {
20
- throw new Error('only support Point now');
21
- }
22
- }
23
- var MysqlStore = /** @class */ (function (_super) {
24
- tslib_1.__extends(MysqlStore, _super);
25
- function MysqlStore(storageSchema, configuration) {
26
- var _this = _super.call(this, storageSchema) || this;
27
- _this.connector = new connector_1.MySqlConnector(configuration);
28
- _this.translator = new translator_1.MySqlTranslator(storageSchema);
29
- return _this;
30
- }
31
- MysqlStore.prototype.aggregateSync = function (entity, aggregation, context, option) {
32
- throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
33
- };
34
- MysqlStore.prototype.selectAbjointRow = function (entity, selection, context, option) {
35
- throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
36
- };
37
- MysqlStore.prototype.updateAbjointRow = function (entity, operation, context, option) {
38
- throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿');
39
- };
40
- MysqlStore.prototype.exec = function (script, txnId) {
41
- return this.connector.exec(script, txnId);
42
- };
43
- MysqlStore.prototype.aggregateAsync = function (entity, aggregation, context, option) {
44
- return tslib_1.__awaiter(this, void 0, void 0, function () {
45
- var sql, result;
46
- return tslib_1.__generator(this, function (_a) {
47
- switch (_a.label) {
48
- case 0:
49
- sql = this.translator.translateAggregate(entity, aggregation, option);
50
- return [4 /*yield*/, this.connector.exec(sql, context.getCurrentTxnId())];
51
- case 1:
52
- result = _a.sent();
53
- return [2 /*return*/, this.formResult(entity, result)];
54
- }
55
- });
56
- });
57
- };
58
- MysqlStore.prototype.aggregate = function (entity, aggregation, context, option) {
59
- return this.aggregateAsync(entity, aggregation, context, option);
60
- };
61
- MysqlStore.prototype.supportManyToOneJoin = function () {
62
- return true;
63
- };
64
- MysqlStore.prototype.supportMultipleCreate = function () {
65
- return true;
66
- };
67
- MysqlStore.prototype.formResult = function (entity, result) {
68
- var schema = this.getSchema();
69
- /* function resolveObject(r: Record<string, any>, path: string, value: any) {
70
- const i = path.indexOf(".");
71
- const bs = path.indexOf('[');
72
- const be = path.indexOf(']');
73
- if (i === -1 && bs === -1) {
74
- r[i] = value;
75
- }
76
- else if (i === -1) {
77
-
78
- }
79
- else if (bs === -1) {
80
- const attrHead = path.slice(0, i);
81
- const attrTail = path.slice(i + 1);
82
- if (!r[attrHead]) {
83
- r[attrHead] = {};
84
- }
85
- resolveObject(r[attrHead], attrTail, value);
86
- }
87
- } */
88
- function resolveAttribute(entity2, r, attr, value) {
89
- var _a;
90
- var _b = schema[entity2], attributes = _b.attributes, view = _b.view;
91
- if (!view) {
92
- var i = attr.indexOf(".");
93
- if (i !== -1) {
94
- var attrHead = attr.slice(0, i);
95
- var attrTail = attr.slice(i + 1);
96
- var rel = (0, relation_1.judgeRelation)(schema, entity2, attrHead);
97
- if (rel === 1) {
98
- (0, lodash_1.set)(r, attr, value);
99
- }
100
- else {
101
- if (!r[attrHead]) {
102
- r[attrHead] = {};
103
- }
104
- if (rel === 0) {
105
- resolveAttribute(entity2, r[attrHead], attrTail, value);
106
- }
107
- else if (rel === 2) {
108
- resolveAttribute(attrHead, r[attrHead], attrTail, value);
109
- }
110
- else {
111
- (0, assert_1.default)(typeof rel === 'string');
112
- resolveAttribute(rel, r[attrHead], attrTail, value);
113
- }
114
- }
115
- }
116
- else if (attributes[attr]) {
117
- var type = attributes[attr].type;
118
- switch (type) {
119
- case 'date':
120
- case 'time': {
121
- if (value instanceof Date) {
122
- r[attr] = value.valueOf();
123
- }
124
- else {
125
- r[attr] = value;
126
- }
127
- break;
128
- }
129
- case 'geometry': {
130
- if (typeof value === 'string') {
131
- r[attr] = convertGeoTextToObject(value);
132
- }
133
- else {
134
- r[attr] = value;
135
- }
136
- break;
137
- }
138
- case 'object':
139
- case 'array': {
140
- if (typeof value === 'string') {
141
- r[attr] = JSON.parse(value.replace(/[\r]/g, '\\r').replace(/[\n]/g, '\\n'));
142
- }
143
- else {
144
- r[attr] = value;
145
- }
146
- break;
147
- }
148
- case 'function': {
149
- if (typeof value === 'string') {
150
- // 函数的执行环境需要的参数只有创建函数者知悉,只能由上层再创建Function
151
- r[attr] = "return ".concat(Buffer.from(value, 'base64').toString());
152
- }
153
- else {
154
- r[attr] = value;
155
- }
156
- break;
157
- }
158
- case 'bool':
159
- case 'boolean': {
160
- if (value === 0) {
161
- r[attr] = false;
162
- }
163
- else if (value === 1) {
164
- r[attr] = true;
165
- }
166
- else {
167
- r[attr] = value;
168
- }
169
- break;
170
- }
171
- case 'decimal': {
172
- // mysql内部取回decimal是字符串
173
- if (typeof value === 'string') {
174
- r[attr] = parseFloat(value);
175
- }
176
- else {
177
- (0, assert_1.default)(value === null || typeof value === 'number');
178
- r[attr] = value;
179
- }
180
- break;
181
- }
182
- default: {
183
- r[attr] = value;
184
- }
185
- }
186
- }
187
- else {
188
- r[attr] = value;
189
- }
190
- }
191
- else {
192
- (0, lodash_1.assign)(r, (_a = {},
193
- _a[attr] = value,
194
- _a));
195
- }
196
- }
197
- function removeNullObjects(r, e) {
198
- // assert(r.id && typeof r.id === 'string', `对象${<string>e}取数据时发现id为非法值${r.id},rowId是${r.id}`)
199
- for (var attr in r) {
200
- var rel = (0, relation_1.judgeRelation)(schema, e, attr);
201
- if (rel === 2) {
202
- // 边界,如果是toModi的对象,这里的外键确实有可能为空
203
- (0, assert_1.default)(schema[e].toModi || r.entity !== attr || r.entityId === r[attr].id, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0entityId\u4E0E\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176entityId\u503C\u4E3A").concat(r.entityId, "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E3A").concat(r[attr].id));
204
- if (r[attr].id === null) {
205
- (0, assert_1.default)(schema[e].toModi || r.entity !== attr);
206
- delete r[attr];
207
- continue;
208
- }
209
- (0, assert_1.default)(r.entity === attr, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0entity\u503C\u4E0E\u8FDE\u63A5\u7684\u5916\u952E\u5BF9\u8C61\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176entity\u503C\u4E3A").concat(r.entity, "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u4E3A").concat(attr));
210
- removeNullObjects(r[attr], attr);
211
- }
212
- else if (typeof rel === 'string') {
213
- // 边界,如果是toModi的对象,这里的外键确实有可能为空
214
- (0, assert_1.default)(schema[e].toModi || r["".concat(attr, "Id")] === r[attr].id, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0\u5176\u5916\u952E\u4E0E\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176").concat(attr, "Id\u503C\u4E3A").concat(r["".concat(attr, "Id")], "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E3A").concat(r[attr].id));
215
- if (r[attr].id === null) {
216
- (0, assert_1.default)(schema[e].toModi || r["".concat(attr, "Id")] === null);
217
- delete r[attr];
218
- continue;
219
- }
220
- removeNullObjects(r[attr], rel);
221
- }
222
- }
223
- }
224
- function formSingleRow(r) {
225
- var result2 = {};
226
- for (var attr in r) {
227
- var value = r[attr];
228
- resolveAttribute(entity, result2, attr, value);
229
- }
230
- removeNullObjects(result2, entity);
231
- return result2;
232
- }
233
- if (result instanceof Array) {
234
- return result.map(function (r) { return formSingleRow(r); });
235
- }
236
- return formSingleRow(result);
237
- };
238
- MysqlStore.prototype.selectAbjointRowAsync = function (entity, selection, context, option) {
239
- return tslib_1.__awaiter(this, void 0, void 0, function () {
240
- var sql, result;
241
- return tslib_1.__generator(this, function (_a) {
242
- switch (_a.label) {
243
- case 0:
244
- sql = this.translator.translateSelect(entity, selection, option);
245
- return [4 /*yield*/, this.connector.exec(sql, context.getCurrentTxnId())];
246
- case 1:
247
- result = _a.sent();
248
- return [2 /*return*/, this.formResult(entity, result)];
249
- }
250
- });
251
- });
252
- };
253
- MysqlStore.prototype.updateAbjointRowAsync = function (entity, operation, context, option) {
254
- return tslib_1.__awaiter(this, void 0, void 0, function () {
255
- var _a, translator, connector, action, txn, _b, data, sql, sql, sql;
256
- return tslib_1.__generator(this, function (_c) {
257
- switch (_c.label) {
258
- case 0:
259
- _a = this, translator = _a.translator, connector = _a.connector;
260
- action = operation.action;
261
- txn = context.getCurrentTxnId();
262
- _b = action;
263
- switch (_b) {
264
- case 'create': return [3 /*break*/, 1];
265
- case 'remove': return [3 /*break*/, 3];
266
- }
267
- return [3 /*break*/, 5];
268
- case 1:
269
- data = operation.data;
270
- sql = translator.translateInsert(entity, data instanceof Array ? data : [data]);
271
- return [4 /*yield*/, connector.exec(sql, txn)];
272
- case 2:
273
- _c.sent();
274
- if (!(option === null || option === void 0 ? void 0 : option.dontCollect)) {
275
- context.opRecords.push({
276
- a: 'c',
277
- d: data,
278
- e: entity,
279
- });
280
- }
281
- return [2 /*return*/, data instanceof Array ? data.length : 1];
282
- case 3:
283
- sql = translator.translateRemove(entity, operation, option);
284
- return [4 /*yield*/, connector.exec(sql, txn)];
285
- case 4:
286
- _c.sent();
287
- // todo 这里对sorter和indexfrom/count的支持不完整
288
- if (!(option === null || option === void 0 ? void 0 : option.dontCollect)) {
289
- context.opRecords.push({
290
- a: 'r',
291
- e: entity,
292
- f: operation.filter,
293
- });
294
- }
295
- return [2 /*return*/, 1];
296
- case 5:
297
- (0, assert_1.default)(!['select', 'download', 'stat'].includes(action));
298
- sql = translator.translateUpdate(entity, operation, option);
299
- return [4 /*yield*/, connector.exec(sql, txn)];
300
- case 6:
301
- _c.sent();
302
- // todo 这里对sorter和indexfrom/count的支持不完整
303
- if (!(option === null || option === void 0 ? void 0 : option.dontCollect)) {
304
- context.opRecords.push({
305
- a: 'u',
306
- e: entity,
307
- d: operation.data,
308
- f: operation.filter,
309
- });
310
- }
311
- return [2 /*return*/, 1];
312
- }
313
- });
314
- });
315
- };
316
- MysqlStore.prototype.operate = function (entity, operation, context, option) {
317
- return tslib_1.__awaiter(this, void 0, void 0, function () {
318
- var action;
319
- return tslib_1.__generator(this, function (_a) {
320
- switch (_a.label) {
321
- case 0:
322
- action = operation.action;
323
- (0, assert_1.default)(!['select', 'download', 'stat'].includes(action), '现在不支持使用select operation');
324
- return [4 /*yield*/, _super.prototype.operateAsync.call(this, entity, operation, context, option)];
325
- case 1: return [2 /*return*/, _a.sent()];
326
- }
327
- });
328
- });
329
- };
330
- MysqlStore.prototype.select = function (entity, selection, context, option) {
331
- return tslib_1.__awaiter(this, void 0, void 0, function () {
332
- var result;
333
- return tslib_1.__generator(this, function (_a) {
334
- switch (_a.label) {
335
- case 0: return [4 /*yield*/, _super.prototype.selectAsync.call(this, entity, selection, context, option)];
336
- case 1:
337
- result = _a.sent();
338
- return [2 /*return*/, result];
339
- }
340
- });
341
- });
342
- };
343
- MysqlStore.prototype.count = function (entity, selection, context, option) {
344
- return tslib_1.__awaiter(this, void 0, void 0, function () {
345
- var sql, result;
346
- return tslib_1.__generator(this, function (_a) {
347
- switch (_a.label) {
348
- case 0:
349
- sql = this.translator.translateCount(entity, selection, option);
350
- return [4 /*yield*/, this.connector.exec(sql, context.getCurrentTxnId())];
351
- case 1:
352
- result = _a.sent();
353
- return [2 /*return*/, result[0].cnt];
354
- }
355
- });
356
- });
357
- };
358
- MysqlStore.prototype.begin = function (option) {
359
- return tslib_1.__awaiter(this, void 0, void 0, function () {
360
- var txn;
361
- return tslib_1.__generator(this, function (_a) {
362
- switch (_a.label) {
363
- case 0: return [4 /*yield*/, this.connector.startTransaction(option)];
364
- case 1:
365
- txn = _a.sent();
366
- return [2 /*return*/, txn];
367
- }
368
- });
369
- });
370
- };
371
- MysqlStore.prototype.commit = function (txnId) {
372
- return tslib_1.__awaiter(this, void 0, void 0, function () {
373
- return tslib_1.__generator(this, function (_a) {
374
- switch (_a.label) {
375
- case 0: return [4 /*yield*/, this.connector.commitTransaction(txnId)];
376
- case 1:
377
- _a.sent();
378
- return [2 /*return*/];
379
- }
380
- });
381
- });
382
- };
383
- MysqlStore.prototype.rollback = function (txnId) {
384
- return tslib_1.__awaiter(this, void 0, void 0, function () {
385
- return tslib_1.__generator(this, function (_a) {
386
- switch (_a.label) {
387
- case 0: return [4 /*yield*/, this.connector.rollbackTransaction(txnId)];
388
- case 1:
389
- _a.sent();
390
- return [2 /*return*/];
391
- }
392
- });
393
- });
394
- };
395
- MysqlStore.prototype.connect = function () {
396
- this.connector.connect();
397
- };
398
- MysqlStore.prototype.disconnect = function () {
399
- this.connector.disconnect();
400
- };
401
- MysqlStore.prototype.initialize = function (dropIfExists) {
402
- return tslib_1.__awaiter(this, void 0, void 0, function () {
403
- var schema, _a, _b, _i, entity, sqls, _c, sqls_1, sql;
404
- return tslib_1.__generator(this, function (_d) {
405
- switch (_d.label) {
406
- case 0:
407
- schema = this.getSchema();
408
- _a = [];
409
- for (_b in schema)
410
- _a.push(_b);
411
- _i = 0;
412
- _d.label = 1;
413
- case 1:
414
- if (!(_i < _a.length)) return [3 /*break*/, 6];
415
- entity = _a[_i];
416
- sqls = this.translator.translateCreateEntity(entity, { replace: dropIfExists });
417
- _c = 0, sqls_1 = sqls;
418
- _d.label = 2;
419
- case 2:
420
- if (!(_c < sqls_1.length)) return [3 /*break*/, 5];
421
- sql = sqls_1[_c];
422
- return [4 /*yield*/, this.connector.exec(sql)];
423
- case 3:
424
- _d.sent();
425
- _d.label = 4;
426
- case 4:
427
- _c++;
428
- return [3 /*break*/, 2];
429
- case 5:
430
- _i++;
431
- return [3 /*break*/, 1];
432
- case 6: return [2 /*return*/];
433
- }
434
- });
435
- });
436
- };
437
- return MysqlStore;
438
- }(CascadeStore_1.CascadeStore));
439
- exports.MysqlStore = MysqlStore;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MysqlStore = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var CascadeStore_1 = require("oak-domain/lib/store/CascadeStore");
6
+ var connector_1 = require("./connector");
7
+ var translator_1 = require("./translator");
8
+ var lodash_1 = require("lodash");
9
+ var assert_1 = tslib_1.__importDefault(require("assert"));
10
+ var relation_1 = require("oak-domain/lib/store/relation");
11
+ function convertGeoTextToObject(geoText) {
12
+ if (geoText.startsWith('POINT')) {
13
+ var coord = geoText.match((/(\d|\.)+(?=\)|\s)/g));
14
+ return {
15
+ type: 'Point',
16
+ coordinate: coord.map(function (ele) { return parseFloat(ele); }),
17
+ };
18
+ }
19
+ else {
20
+ throw new Error('only support Point now');
21
+ }
22
+ }
23
+ var MysqlStore = /** @class */ (function (_super) {
24
+ tslib_1.__extends(MysqlStore, _super);
25
+ function MysqlStore(storageSchema, configuration) {
26
+ var _this = _super.call(this, storageSchema) || this;
27
+ _this.connector = new connector_1.MySqlConnector(configuration);
28
+ _this.translator = new translator_1.MySqlTranslator(storageSchema);
29
+ return _this;
30
+ }
31
+ MysqlStore.prototype.aggregateSync = function (entity, aggregation, context, option) {
32
+ throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
33
+ };
34
+ MysqlStore.prototype.selectAbjointRow = function (entity, selection, context, option) {
35
+ throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
36
+ };
37
+ MysqlStore.prototype.updateAbjointRow = function (entity, operation, context, option) {
38
+ throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿');
39
+ };
40
+ MysqlStore.prototype.exec = function (script, txnId) {
41
+ return this.connector.exec(script, txnId);
42
+ };
43
+ MysqlStore.prototype.aggregateAsync = function (entity, aggregation, context, option) {
44
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
45
+ var sql, result;
46
+ return tslib_1.__generator(this, function (_a) {
47
+ switch (_a.label) {
48
+ case 0:
49
+ sql = this.translator.translateAggregate(entity, aggregation, option);
50
+ return [4 /*yield*/, this.connector.exec(sql, context.getCurrentTxnId())];
51
+ case 1:
52
+ result = _a.sent();
53
+ return [2 /*return*/, this.formResult(entity, result)];
54
+ }
55
+ });
56
+ });
57
+ };
58
+ MysqlStore.prototype.aggregate = function (entity, aggregation, context, option) {
59
+ return this.aggregateAsync(entity, aggregation, context, option);
60
+ };
61
+ MysqlStore.prototype.supportManyToOneJoin = function () {
62
+ return true;
63
+ };
64
+ MysqlStore.prototype.supportMultipleCreate = function () {
65
+ return true;
66
+ };
67
+ MysqlStore.prototype.formResult = function (entity, result) {
68
+ var schema = this.getSchema();
69
+ /* function resolveObject(r: Record<string, any>, path: string, value: any) {
70
+ const i = path.indexOf(".");
71
+ const bs = path.indexOf('[');
72
+ const be = path.indexOf(']');
73
+ if (i === -1 && bs === -1) {
74
+ r[i] = value;
75
+ }
76
+ else if (i === -1) {
77
+
78
+ }
79
+ else if (bs === -1) {
80
+ const attrHead = path.slice(0, i);
81
+ const attrTail = path.slice(i + 1);
82
+ if (!r[attrHead]) {
83
+ r[attrHead] = {};
84
+ }
85
+ resolveObject(r[attrHead], attrTail, value);
86
+ }
87
+ } */
88
+ function resolveAttribute(entity2, r, attr, value) {
89
+ var _a;
90
+ var _b = schema[entity2], attributes = _b.attributes, view = _b.view;
91
+ if (!view) {
92
+ var i = attr.indexOf(".");
93
+ if (i !== -1) {
94
+ var attrHead = attr.slice(0, i);
95
+ var attrTail = attr.slice(i + 1);
96
+ var rel = (0, relation_1.judgeRelation)(schema, entity2, attrHead);
97
+ if (rel === 1) {
98
+ (0, lodash_1.set)(r, attr, value);
99
+ }
100
+ else {
101
+ if (!r[attrHead]) {
102
+ r[attrHead] = {};
103
+ }
104
+ if (rel === 0) {
105
+ resolveAttribute(entity2, r[attrHead], attrTail, value);
106
+ }
107
+ else if (rel === 2) {
108
+ resolveAttribute(attrHead, r[attrHead], attrTail, value);
109
+ }
110
+ else {
111
+ (0, assert_1.default)(typeof rel === 'string');
112
+ resolveAttribute(rel, r[attrHead], attrTail, value);
113
+ }
114
+ }
115
+ }
116
+ else if (attributes[attr]) {
117
+ var type = attributes[attr].type;
118
+ switch (type) {
119
+ case 'date':
120
+ case 'time': {
121
+ if (value instanceof Date) {
122
+ r[attr] = value.valueOf();
123
+ }
124
+ else {
125
+ r[attr] = value;
126
+ }
127
+ break;
128
+ }
129
+ case 'geometry': {
130
+ if (typeof value === 'string') {
131
+ r[attr] = convertGeoTextToObject(value);
132
+ }
133
+ else {
134
+ r[attr] = value;
135
+ }
136
+ break;
137
+ }
138
+ case 'object':
139
+ case 'array': {
140
+ if (typeof value === 'string') {
141
+ r[attr] = JSON.parse(value.replace(/[\r]/g, '\\r').replace(/[\n]/g, '\\n'));
142
+ }
143
+ else {
144
+ r[attr] = value;
145
+ }
146
+ break;
147
+ }
148
+ case 'function': {
149
+ if (typeof value === 'string') {
150
+ // 函数的执行环境需要的参数只有创建函数者知悉,只能由上层再创建Function
151
+ r[attr] = "return ".concat(Buffer.from(value, 'base64').toString());
152
+ }
153
+ else {
154
+ r[attr] = value;
155
+ }
156
+ break;
157
+ }
158
+ case 'bool':
159
+ case 'boolean': {
160
+ if (value === 0) {
161
+ r[attr] = false;
162
+ }
163
+ else if (value === 1) {
164
+ r[attr] = true;
165
+ }
166
+ else {
167
+ r[attr] = value;
168
+ }
169
+ break;
170
+ }
171
+ case 'decimal': {
172
+ // mysql内部取回decimal是字符串
173
+ if (typeof value === 'string') {
174
+ r[attr] = parseFloat(value);
175
+ }
176
+ else {
177
+ (0, assert_1.default)(value === null || typeof value === 'number');
178
+ r[attr] = value;
179
+ }
180
+ break;
181
+ }
182
+ default: {
183
+ r[attr] = value;
184
+ }
185
+ }
186
+ }
187
+ else {
188
+ r[attr] = value;
189
+ }
190
+ }
191
+ else {
192
+ (0, lodash_1.assign)(r, (_a = {},
193
+ _a[attr] = value,
194
+ _a));
195
+ }
196
+ }
197
+ function removeNullObjects(r, e) {
198
+ // assert(r.id && typeof r.id === 'string', `对象${<string>e}取数据时发现id为非法值${r.id},rowId是${r.id}`)
199
+ for (var attr in r) {
200
+ var rel = (0, relation_1.judgeRelation)(schema, e, attr);
201
+ if (rel === 2) {
202
+ // 边界,如果是toModi的对象,这里的外键确实有可能为空
203
+ (0, assert_1.default)(schema[e].toModi || r.entity !== attr || r.entityId === r[attr].id, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0entityId\u4E0E\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176entityId\u503C\u4E3A").concat(r.entityId, "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E3A").concat(r[attr].id));
204
+ if (r[attr].id === null) {
205
+ (0, assert_1.default)(schema[e].toModi || r.entity !== attr);
206
+ delete r[attr];
207
+ continue;
208
+ }
209
+ (0, assert_1.default)(r.entity === attr, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0entity\u503C\u4E0E\u8FDE\u63A5\u7684\u5916\u952E\u5BF9\u8C61\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176entity\u503C\u4E3A").concat(r.entity, "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u4E3A").concat(attr));
210
+ removeNullObjects(r[attr], attr);
211
+ }
212
+ else if (typeof rel === 'string') {
213
+ // 边界,如果是toModi的对象,这里的外键确实有可能为空
214
+ (0, assert_1.default)(schema[e].toModi || r["".concat(attr, "Id")] === r[attr].id, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0\u5176\u5916\u952E\u4E0E\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176").concat(attr, "Id\u503C\u4E3A").concat(r["".concat(attr, "Id")], "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E3A").concat(r[attr].id));
215
+ if (r[attr].id === null) {
216
+ (0, assert_1.default)(schema[e].toModi || r["".concat(attr, "Id")] === null);
217
+ delete r[attr];
218
+ continue;
219
+ }
220
+ removeNullObjects(r[attr], rel);
221
+ }
222
+ }
223
+ }
224
+ function formSingleRow(r) {
225
+ var result2 = {};
226
+ for (var attr in r) {
227
+ var value = r[attr];
228
+ resolveAttribute(entity, result2, attr, value);
229
+ }
230
+ removeNullObjects(result2, entity);
231
+ return result2;
232
+ }
233
+ if (result instanceof Array) {
234
+ return result.map(function (r) { return formSingleRow(r); });
235
+ }
236
+ return formSingleRow(result);
237
+ };
238
+ MysqlStore.prototype.selectAbjointRowAsync = function (entity, selection, context, option) {
239
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
240
+ var sql, result;
241
+ return tslib_1.__generator(this, function (_a) {
242
+ switch (_a.label) {
243
+ case 0:
244
+ sql = this.translator.translateSelect(entity, selection, option);
245
+ return [4 /*yield*/, this.connector.exec(sql, context.getCurrentTxnId())];
246
+ case 1:
247
+ result = _a.sent();
248
+ return [2 /*return*/, this.formResult(entity, result)];
249
+ }
250
+ });
251
+ });
252
+ };
253
+ MysqlStore.prototype.updateAbjointRowAsync = function (entity, operation, context, option) {
254
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
255
+ var _a, translator, connector, action, txn, _b, data, sql, sql, sql;
256
+ return tslib_1.__generator(this, function (_c) {
257
+ switch (_c.label) {
258
+ case 0:
259
+ _a = this, translator = _a.translator, connector = _a.connector;
260
+ action = operation.action;
261
+ txn = context.getCurrentTxnId();
262
+ _b = action;
263
+ switch (_b) {
264
+ case 'create': return [3 /*break*/, 1];
265
+ case 'remove': return [3 /*break*/, 3];
266
+ }
267
+ return [3 /*break*/, 5];
268
+ case 1:
269
+ data = operation.data;
270
+ sql = translator.translateInsert(entity, data instanceof Array ? data : [data]);
271
+ return [4 /*yield*/, connector.exec(sql, txn)];
272
+ case 2:
273
+ _c.sent();
274
+ if (!(option === null || option === void 0 ? void 0 : option.dontCollect)) {
275
+ context.opRecords.push({
276
+ a: 'c',
277
+ d: data,
278
+ e: entity,
279
+ });
280
+ }
281
+ return [2 /*return*/, data instanceof Array ? data.length : 1];
282
+ case 3:
283
+ sql = translator.translateRemove(entity, operation, option);
284
+ return [4 /*yield*/, connector.exec(sql, txn)];
285
+ case 4:
286
+ _c.sent();
287
+ // todo 这里对sorter和indexfrom/count的支持不完整
288
+ if (!(option === null || option === void 0 ? void 0 : option.dontCollect)) {
289
+ context.opRecords.push({
290
+ a: 'r',
291
+ e: entity,
292
+ f: operation.filter,
293
+ });
294
+ }
295
+ return [2 /*return*/, 1];
296
+ case 5:
297
+ (0, assert_1.default)(!['select', 'download', 'stat'].includes(action));
298
+ sql = translator.translateUpdate(entity, operation, option);
299
+ return [4 /*yield*/, connector.exec(sql, txn)];
300
+ case 6:
301
+ _c.sent();
302
+ // todo 这里对sorter和indexfrom/count的支持不完整
303
+ if (!(option === null || option === void 0 ? void 0 : option.dontCollect)) {
304
+ context.opRecords.push({
305
+ a: 'u',
306
+ e: entity,
307
+ d: operation.data,
308
+ f: operation.filter,
309
+ });
310
+ }
311
+ return [2 /*return*/, 1];
312
+ }
313
+ });
314
+ });
315
+ };
316
+ MysqlStore.prototype.operate = function (entity, operation, context, option) {
317
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
318
+ var action;
319
+ return tslib_1.__generator(this, function (_a) {
320
+ switch (_a.label) {
321
+ case 0:
322
+ action = operation.action;
323
+ (0, assert_1.default)(!['select', 'download', 'stat'].includes(action), '现在不支持使用select operation');
324
+ return [4 /*yield*/, _super.prototype.operateAsync.call(this, entity, operation, context, option)];
325
+ case 1: return [2 /*return*/, _a.sent()];
326
+ }
327
+ });
328
+ });
329
+ };
330
+ MysqlStore.prototype.select = function (entity, selection, context, option) {
331
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
332
+ var result;
333
+ return tslib_1.__generator(this, function (_a) {
334
+ switch (_a.label) {
335
+ case 0: return [4 /*yield*/, _super.prototype.selectAsync.call(this, entity, selection, context, option)];
336
+ case 1:
337
+ result = _a.sent();
338
+ return [2 /*return*/, result];
339
+ }
340
+ });
341
+ });
342
+ };
343
+ MysqlStore.prototype.count = function (entity, selection, context, option) {
344
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
345
+ var sql, result;
346
+ return tslib_1.__generator(this, function (_a) {
347
+ switch (_a.label) {
348
+ case 0:
349
+ sql = this.translator.translateCount(entity, selection, option);
350
+ return [4 /*yield*/, this.connector.exec(sql, context.getCurrentTxnId())];
351
+ case 1:
352
+ result = _a.sent();
353
+ return [2 /*return*/, result[0].cnt];
354
+ }
355
+ });
356
+ });
357
+ };
358
+ MysqlStore.prototype.begin = function (option) {
359
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
360
+ var txn;
361
+ return tslib_1.__generator(this, function (_a) {
362
+ switch (_a.label) {
363
+ case 0: return [4 /*yield*/, this.connector.startTransaction(option)];
364
+ case 1:
365
+ txn = _a.sent();
366
+ return [2 /*return*/, txn];
367
+ }
368
+ });
369
+ });
370
+ };
371
+ MysqlStore.prototype.commit = function (txnId) {
372
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
373
+ return tslib_1.__generator(this, function (_a) {
374
+ switch (_a.label) {
375
+ case 0: return [4 /*yield*/, this.connector.commitTransaction(txnId)];
376
+ case 1:
377
+ _a.sent();
378
+ return [2 /*return*/];
379
+ }
380
+ });
381
+ });
382
+ };
383
+ MysqlStore.prototype.rollback = function (txnId) {
384
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
385
+ return tslib_1.__generator(this, function (_a) {
386
+ switch (_a.label) {
387
+ case 0: return [4 /*yield*/, this.connector.rollbackTransaction(txnId)];
388
+ case 1:
389
+ _a.sent();
390
+ return [2 /*return*/];
391
+ }
392
+ });
393
+ });
394
+ };
395
+ MysqlStore.prototype.connect = function () {
396
+ this.connector.connect();
397
+ };
398
+ MysqlStore.prototype.disconnect = function () {
399
+ this.connector.disconnect();
400
+ };
401
+ MysqlStore.prototype.initialize = function (dropIfExists) {
402
+ return tslib_1.__awaiter(this, void 0, void 0, function () {
403
+ var schema, _a, _b, _i, entity, sqls, _c, sqls_1, sql;
404
+ return tslib_1.__generator(this, function (_d) {
405
+ switch (_d.label) {
406
+ case 0:
407
+ schema = this.getSchema();
408
+ _a = [];
409
+ for (_b in schema)
410
+ _a.push(_b);
411
+ _i = 0;
412
+ _d.label = 1;
413
+ case 1:
414
+ if (!(_i < _a.length)) return [3 /*break*/, 6];
415
+ entity = _a[_i];
416
+ sqls = this.translator.translateCreateEntity(entity, { replace: dropIfExists });
417
+ _c = 0, sqls_1 = sqls;
418
+ _d.label = 2;
419
+ case 2:
420
+ if (!(_c < sqls_1.length)) return [3 /*break*/, 5];
421
+ sql = sqls_1[_c];
422
+ return [4 /*yield*/, this.connector.exec(sql)];
423
+ case 3:
424
+ _d.sent();
425
+ _d.label = 4;
426
+ case 4:
427
+ _c++;
428
+ return [3 /*break*/, 2];
429
+ case 5:
430
+ _i++;
431
+ return [3 /*break*/, 1];
432
+ case 6: return [2 /*return*/];
433
+ }
434
+ });
435
+ });
436
+ };
437
+ return MysqlStore;
438
+ }(CascadeStore_1.CascadeStore));
439
+ exports.MysqlStore = MysqlStore;