oak-db 3.0.1 → 3.0.2

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,71 +1,61 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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");
4
+ const tslib_1 = require("tslib");
5
+ const CascadeStore_1 = require("oak-domain/lib/store/CascadeStore");
6
+ const connector_1 = require("./connector");
7
+ const translator_1 = require("./translator");
8
+ const lodash_1 = require("lodash");
9
+ const assert_1 = tslib_1.__importDefault(require("assert"));
10
+ const relation_1 = require("oak-domain/lib/store/relation");
11
11
  function convertGeoTextToObject(geoText) {
12
12
  if (geoText.startsWith('POINT')) {
13
- var coord = geoText.match((/(\d|\.)+(?=\)|\s)/g));
13
+ const coord = geoText.match((/(\d|\.)+(?=\)|\s)/g));
14
14
  return {
15
15
  type: 'Point',
16
- coordinate: coord.map(function (ele) { return parseFloat(ele); }),
16
+ coordinate: coord.map(ele => parseFloat(ele)),
17
17
  };
18
18
  }
19
19
  else {
20
20
  throw new Error('only support Point now');
21
21
  }
22
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) {
23
+ class MysqlStore extends CascadeStore_1.CascadeStore {
24
+ aggregateSync(entity, aggregation, context, option) {
32
25
  throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
33
- };
34
- MysqlStore.prototype.selectAbjointRow = function (entity, selection, context, option) {
26
+ }
27
+ selectAbjointRow(entity, selection, context, option) {
35
28
  throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
36
- };
37
- MysqlStore.prototype.updateAbjointRow = function (entity, operation, context, option) {
29
+ }
30
+ updateAbjointRow(entity, operation, context, option) {
38
31
  throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿');
39
- };
40
- MysqlStore.prototype.exec = function (script, txnId) {
32
+ }
33
+ exec(script, txnId) {
41
34
  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) {
35
+ }
36
+ connector;
37
+ translator;
38
+ constructor(storageSchema, configuration) {
39
+ super(storageSchema);
40
+ this.connector = new connector_1.MySqlConnector(configuration);
41
+ this.translator = new translator_1.MySqlTranslator(storageSchema);
42
+ }
43
+ async aggregateAsync(entity, aggregation, context, option) {
44
+ const sql = this.translator.translateAggregate(entity, aggregation, option);
45
+ const result = await this.connector.exec(sql, context.getCurrentTxnId());
46
+ return this.formResult(entity, result);
47
+ }
48
+ aggregate(entity, aggregation, context, option) {
59
49
  return this.aggregateAsync(entity, aggregation, context, option);
60
- };
61
- MysqlStore.prototype.supportManyToOneJoin = function () {
50
+ }
51
+ supportManyToOneJoin() {
62
52
  return true;
63
- };
64
- MysqlStore.prototype.supportMultipleCreate = function () {
53
+ }
54
+ supportMultipleCreate() {
65
55
  return true;
66
- };
67
- MysqlStore.prototype.formResult = function (entity, result) {
68
- var schema = this.getSchema();
56
+ }
57
+ formResult(entity, result) {
58
+ const schema = this.getSchema();
69
59
  /* function resolveObject(r: Record<string, any>, path: string, value: any) {
70
60
  const i = path.indexOf(".");
71
61
  const bs = path.indexOf('[');
@@ -86,14 +76,13 @@ var MysqlStore = /** @class */ (function (_super) {
86
76
  }
87
77
  } */
88
78
  function resolveAttribute(entity2, r, attr, value) {
89
- var _a;
90
- var _b = schema[entity2], attributes = _b.attributes, view = _b.view;
79
+ const { attributes, view } = schema[entity2];
91
80
  if (!view) {
92
- var i = attr.indexOf(".");
81
+ const i = attr.indexOf(".");
93
82
  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);
83
+ const attrHead = attr.slice(0, i);
84
+ const attrTail = attr.slice(i + 1);
85
+ const rel = (0, relation_1.judgeRelation)(schema, entity2, attrHead);
97
86
  if (rel === 1) {
98
87
  (0, lodash_1.set)(r, attr, value);
99
88
  }
@@ -114,7 +103,7 @@ var MysqlStore = /** @class */ (function (_super) {
114
103
  }
115
104
  }
116
105
  else if (attributes[attr]) {
117
- var type = attributes[attr].type;
106
+ const { type } = attributes[attr];
118
107
  switch (type) {
119
108
  case 'date':
120
109
  case 'time': {
@@ -148,7 +137,7 @@ var MysqlStore = /** @class */ (function (_super) {
148
137
  case 'function': {
149
138
  if (typeof value === 'string') {
150
139
  // 函数的执行环境需要的参数只有创建函数者知悉,只能由上层再创建Function
151
- r[attr] = "return ".concat(Buffer.from(value, 'base64').toString());
140
+ r[attr] = `return ${Buffer.from(value, 'base64').toString()}`;
152
141
  }
153
142
  else {
154
143
  r[attr] = value;
@@ -189,31 +178,31 @@ var MysqlStore = /** @class */ (function (_super) {
189
178
  }
190
179
  }
191
180
  else {
192
- (0, lodash_1.assign)(r, (_a = {},
193
- _a[attr] = value,
194
- _a));
181
+ (0, lodash_1.assign)(r, {
182
+ [attr]: value,
183
+ });
195
184
  }
196
185
  }
197
186
  function removeNullObjects(r, e) {
198
187
  // 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);
188
+ for (let attr in r) {
189
+ const rel = (0, relation_1.judgeRelation)(schema, e, attr);
201
190
  if (rel === 2) {
202
191
  // 边界,如果是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));
192
+ (0, assert_1.default)(schema[e].toModi || r.entity !== attr || r.entityId === r[attr].id, `对象${e}取数据时,发现entityId与连接的对象的主键不一致,rowId是${r.id},其entityId值为${r.entityId},连接的对象的主键为${r[attr].id}`);
204
193
  if (r[attr].id === null) {
205
194
  (0, assert_1.default)(schema[e].toModi || r.entity !== attr);
206
195
  delete r[attr];
207
196
  continue;
208
197
  }
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));
198
+ (0, assert_1.default)(r.entity === attr, `对象${e}取数据时,发现entity值与连接的外键对象不一致,rowId是${r.id},其entity值为${r.entity},连接的对象为${attr}`);
210
199
  removeNullObjects(r[attr], attr);
211
200
  }
212
201
  else if (typeof rel === 'string') {
213
202
  // 边界,如果是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));
203
+ (0, assert_1.default)(schema[e].toModi || r[`${attr}Id`] === r[attr].id, `对象${e}取数据时,发现其外键与连接的对象的主键不一致,rowId是${r.id},其${attr}Id值为${r[`${attr}Id`]},连接的对象的主键为${r[attr].id}`);
215
204
  if (r[attr].id === null) {
216
- (0, assert_1.default)(schema[e].toModi || r["".concat(attr, "Id")] === null);
205
+ (0, assert_1.default)(schema[e].toModi || r[`${attr}Id`] === null);
217
206
  delete r[attr];
218
207
  continue;
219
208
  }
@@ -222,218 +211,91 @@ var MysqlStore = /** @class */ (function (_super) {
222
211
  }
223
212
  }
224
213
  function formSingleRow(r) {
225
- var result2 = {};
226
- for (var attr in r) {
227
- var value = r[attr];
214
+ let result2 = {};
215
+ for (let attr in r) {
216
+ const value = r[attr];
228
217
  resolveAttribute(entity, result2, attr, value);
229
218
  }
230
219
  removeNullObjects(result2, entity);
231
220
  return result2;
232
221
  }
233
222
  if (result instanceof Array) {
234
- return result.map(function (r) { return formSingleRow(r); });
223
+ return result.map(r => formSingleRow(r));
235
224
  }
236
225
  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 () {
226
+ }
227
+ async selectAbjointRowAsync(entity, selection, context, option) {
228
+ const sql = this.translator.translateSelect(entity, selection, option);
229
+ const result = await this.connector.exec(sql, context.getCurrentTxnId());
230
+ return this.formResult(entity, result);
231
+ }
232
+ async updateAbjointRowAsync(entity, operation, context, option) {
233
+ const { translator, connector } = this;
234
+ const { action } = operation;
235
+ const txn = context.getCurrentTxnId();
236
+ switch (action) {
237
+ case 'create': {
238
+ const { data } = operation;
239
+ const sql = translator.translateInsert(entity, data instanceof Array ? data : [data]);
240
+ await connector.exec(sql, txn);
241
+ return data instanceof Array ? data.length : 1;
242
+ }
243
+ case 'remove': {
244
+ const sql = translator.translateRemove(entity, operation, option);
245
+ await connector.exec(sql, txn);
246
+ // todo 这里对sorter和indexfrom/count的支持不完整
247
+ return 1;
248
+ }
249
+ default: {
250
+ (0, assert_1.default)(!['select', 'download', 'stat'].includes(action));
251
+ const sql = translator.translateUpdate(entity, operation, option);
252
+ await connector.exec(sql, txn);
253
+ // todo 这里对sorter和indexfrom/count的支持不完整
254
+ return 1;
255
+ }
256
+ }
257
+ }
258
+ async operate(entity, operation, context, option) {
259
+ const { action } = operation;
260
+ (0, assert_1.default)(!['select', 'download', 'stat'].includes(action), '现在不支持使用select operation');
261
+ return await super.operateAsync(entity, operation, context, option);
262
+ }
263
+ async select(entity, selection, context, option) {
264
+ const result = await super.selectAsync(entity, selection, context, option);
265
+ return result;
266
+ }
267
+ async countAsync(entity, selection, context, option) {
268
+ const sql = this.translator.translateCount(entity, selection, option);
269
+ const result = await this.connector.exec(sql, context.getCurrentTxnId());
270
+ return result[0].cnt;
271
+ }
272
+ async count(entity, selection, context, option) {
273
+ return this.countAsync(entity, selection, context, option);
274
+ }
275
+ async begin(option) {
276
+ const txn = await this.connector.startTransaction(option);
277
+ return txn;
278
+ }
279
+ async commit(txnId) {
280
+ await this.connector.commitTransaction(txnId);
281
+ }
282
+ async rollback(txnId) {
283
+ await this.connector.rollbackTransaction(txnId);
284
+ }
285
+ connect() {
396
286
  this.connector.connect();
397
- };
398
- MysqlStore.prototype.disconnect = function () {
287
+ }
288
+ disconnect() {
399
289
  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));
290
+ }
291
+ async initialize(dropIfExists) {
292
+ const schema = this.getSchema();
293
+ for (const entity in schema) {
294
+ const sqls = this.translator.translateCreateEntity(entity, { replace: dropIfExists });
295
+ for (const sql of sqls) {
296
+ await this.connector.exec(sql);
297
+ }
298
+ }
299
+ }
300
+ }
439
301
  exports.MysqlStore = MysqlStore;
@@ -104,5 +104,5 @@ export declare class MySqlTranslator<ED extends EntityDict & BaseEntityDict> ext
104
104
  protected translateExpression<T extends keyof ED>(entity: T, alias: string, expression: RefOrExpression<keyof ED[T]["OpSchema"]>, refDict: Record<string, [string, keyof ED]>): string;
105
105
  protected populateSelectStmt<T extends keyof ED>(projectionText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, groupByText?: string, indexFrom?: number, count?: number, option?: MySqlSelectOption): string;
106
106
  protected populateUpdateStmt(updateText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: MysqlOperateOption): string;
107
- protected populateRemoveStmt(removeText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: MysqlOperateOption): string;
107
+ protected populateRemoveStmt(updateText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: MysqlOperateOption): string;
108
108
  }