oak-db 2.2.0 → 2.2.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,4 +1,4 @@
1
- import { EntityDict, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, OperateOption, OperationResult, TxnOption, StorageSchema, DeduceCreateMultipleOperation, SelectOption, AggregationResult } from 'oak-domain/lib/types';
1
+ import { EntityDict, OperateOption, OperationResult, TxnOption, StorageSchema, SelectOption, AggregationResult } from 'oak-domain/lib/types';
2
2
  import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
3
3
  import { CascadeStore } from 'oak-domain/lib/store/CascadeStore';
4
4
  import { MySQLConfiguration } from './types/Configuration';
@@ -19,7 +19,7 @@ export declare class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt exte
19
19
  protected supportMultipleCreate(): boolean;
20
20
  private formResult;
21
21
  protected selectAbjointRowAsync<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: AsyncContext<ED>, option?: MySqlSelectOption): Promise<Partial<ED[T]['Schema']>[]>;
22
- protected updateAbjointRowAsync<T extends keyof ED>(entity: T, operation: DeduceCreateMultipleOperation<ED[T]['Schema']> | DeduceCreateSingleOperation<ED[T]['Schema']> | DeduceUpdateOperation<ED[T]['Schema']> | DeduceRemoveOperation<ED[T]['Schema']>, context: AsyncContext<ED>, option?: MysqlOperateOption): Promise<number>;
22
+ protected updateAbjointRowAsync<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: AsyncContext<ED>, option?: MysqlOperateOption): Promise<number>;
23
23
  operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Cxt, option: OperateOption): Promise<OperationResult<ED>>;
24
24
  select<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Cxt, option: SelectOption): Promise<Partial<ED[T]['Schema']>[]>;
25
25
  count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: SelectOption): Promise<number>;
@@ -97,7 +97,8 @@ export declare class MySqlTranslator<ED extends EntityDict> extends SqlTranslato
97
97
  replace?: boolean;
98
98
  }): string[];
99
99
  private translateFnName;
100
- protected translateExpression<T extends keyof ED>(alias: string, expression: RefOrExpression<keyof ED[T]["OpSchema"]>, refDict: Record<string, string>): string;
100
+ private translateAttrInExpression;
101
+ protected translateExpression<T extends keyof ED>(entity: T, alias: string, expression: RefOrExpression<keyof ED[T]["OpSchema"]>, refDict: Record<string, [string, keyof ED]>): string;
101
102
  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;
102
103
  protected populateUpdateStmt(updateText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: MysqlOperateOption): string;
103
104
  protected populateRemoveStmt(removeText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: MysqlOperateOption): string;
@@ -83,7 +83,7 @@ var MySqlTranslator = /** @class */ (function (_super) {
83
83
  if (option === null || option === void 0 ? void 0 : option.includedDeleted) {
84
84
  return '';
85
85
  }
86
- return " `".concat(alias, "`.`$$deleteAt$$` is null");
86
+ return " (`".concat(alias, "`.`$$deleteAt$$` is null)");
87
87
  };
88
88
  MySqlTranslator.prototype.makeUpSchema = function () {
89
89
  for (var entity in this.schema) {
@@ -357,21 +357,32 @@ var MySqlTranslator = /** @class */ (function (_super) {
357
357
  MySqlTranslator.prototype.translateFnName = function (fnName, argumentNumber) {
358
358
  switch (fnName) {
359
359
  case '$add': {
360
- return '%s + %s';
360
+ var result = '%s';
361
+ while (--argumentNumber > 0) {
362
+ result += ' + %s';
363
+ }
364
+ return result;
361
365
  }
362
366
  case '$subtract': {
367
+ (0, assert_1.default)(argumentNumber === 2);
363
368
  return '%s - %s';
364
369
  }
365
370
  case '$multiply': {
366
- return '%s * %s';
371
+ var result = '%s';
372
+ while (--argumentNumber > 0) {
373
+ result += ' * %s';
374
+ }
375
+ return result;
367
376
  }
368
377
  case '$divide': {
378
+ (0, assert_1.default)(argumentNumber === 2);
369
379
  return '%s / %s';
370
380
  }
371
381
  case '$abs': {
372
382
  return 'ABS(%s)';
373
383
  }
374
384
  case '$round': {
385
+ (0, assert_1.default)(argumentNumber === 2);
375
386
  return 'ROUND(%s, %s)';
376
387
  }
377
388
  case '$ceil': {
@@ -381,33 +392,42 @@ var MySqlTranslator = /** @class */ (function (_super) {
381
392
  return 'FLOOR(%s)';
382
393
  }
383
394
  case '$pow': {
395
+ (0, assert_1.default)(argumentNumber === 2);
384
396
  return 'POW(%s, %s)';
385
397
  }
386
398
  case '$gt': {
399
+ (0, assert_1.default)(argumentNumber === 2);
387
400
  return '%s > %s';
388
401
  }
389
402
  case '$gte': {
403
+ (0, assert_1.default)(argumentNumber === 2);
390
404
  return '%s >= %s';
391
405
  }
392
406
  case '$lt': {
407
+ (0, assert_1.default)(argumentNumber === 2);
393
408
  return '%s < %s';
394
409
  }
395
410
  case '$lte': {
396
411
  return '%s <= %s';
397
412
  }
398
413
  case '$eq': {
414
+ (0, assert_1.default)(argumentNumber === 2);
399
415
  return '%s = %s';
400
416
  }
401
417
  case '$ne': {
418
+ (0, assert_1.default)(argumentNumber === 2);
402
419
  return '%s <> %s';
403
420
  }
404
421
  case '$startsWith': {
422
+ (0, assert_1.default)(argumentNumber === 2);
405
423
  return '%s like CONCAT(%s, \'%\')';
406
424
  }
407
425
  case '$endsWith': {
426
+ (0, assert_1.default)(argumentNumber === 2);
408
427
  return '%s like CONCAT(\'%\', %s)';
409
428
  }
410
429
  case '$includes': {
430
+ (0, assert_1.default)(argumentNumber === 2);
411
431
  return '%s like CONCAT(\'%\', %s, \'%\')';
412
432
  }
413
433
  case '$true': {
@@ -464,27 +484,47 @@ var MySqlTranslator = /** @class */ (function (_super) {
464
484
  return 'DAYOFYEAR(%s)';
465
485
  }
466
486
  case '$dateDiff': {
487
+ (0, assert_1.default)(argumentNumber === 3);
467
488
  return 'DATEDIFF(%s, %s, %s)';
468
489
  }
469
490
  case '$contains': {
491
+ (0, assert_1.default)(argumentNumber === 2);
470
492
  return 'ST_CONTAINS(%s, %s)';
471
493
  }
472
494
  case '$distance': {
495
+ (0, assert_1.default)(argumentNumber === 2);
473
496
  return 'ST_DISTANCE(%s, %s)';
474
497
  }
498
+ case '$concat': {
499
+ var result = ' concat(%s';
500
+ while (--argumentNumber > 0) {
501
+ result += ', %s';
502
+ }
503
+ result += ')';
504
+ return result;
505
+ }
475
506
  default: {
476
507
  throw new Error("unrecoganized function ".concat(fnName));
477
508
  }
478
509
  }
479
510
  };
480
- MySqlTranslator.prototype.translateExpression = function (alias, expression, refDict) {
511
+ MySqlTranslator.prototype.translateAttrInExpression = function (entity, attr, exprText) {
512
+ var attributes = this.schema[entity].attributes;
513
+ var type = attributes[attr].type;
514
+ if (['date', 'time', 'datetime'].includes(type)) {
515
+ // 从unix时间戵转成date类型参加expr的运算
516
+ return "from_unixtime(".concat(exprText, " / 1000)");
517
+ }
518
+ return exprText;
519
+ };
520
+ MySqlTranslator.prototype.translateExpression = function (entity, alias, expression, refDict) {
481
521
  var _this = this;
482
522
  var translateConstant = function (constant) {
483
- if (typeof constant === 'string') {
484
- return " ".concat(new Date(constant).valueOf());
523
+ if (constant instanceof Date) {
524
+ return " from_unixtime(".concat(constant.valueOf(), "/1000)");
485
525
  }
486
- else if (constant instanceof Date) {
487
- return " ".concat(constant.valueOf());
526
+ else if (typeof constant === 'string') {
527
+ return " '".concat(constant, "'");
488
528
  }
489
529
  else {
490
530
  (0, assert_1.default)(typeof constant === 'number');
@@ -496,14 +536,14 @@ var MySqlTranslator = /** @class */ (function (_super) {
496
536
  var result;
497
537
  if (k.includes('#attr')) {
498
538
  var attrText = "`".concat(alias, "`.`").concat((expr)['#attr'], "`");
499
- result = attrText;
539
+ result = _this.translateAttrInExpression(entity, (expr)['#attr'], attrText);
500
540
  }
501
541
  else if (k.includes('#refId')) {
502
542
  var refId = (expr)['#refId'];
503
543
  var refAttr = (expr)['#refAttr'];
504
544
  (0, assert_1.default)(refDict[refId]);
505
- var attrText = "`".concat(refDict[refId], "`.`").concat(refAttr, "`");
506
- result = attrText;
545
+ var attrText = "`".concat(refDict[refId][0], "`.`").concat(refAttr, "`");
546
+ result = _this.translateAttrInExpression(entity, (expr)['#refAttr'], attrText);
507
547
  }
508
548
  else {
509
549
  (0, assert_1.default)(k.length === 1);
@@ -1,4 +1,4 @@
1
- import { DeduceCreateOperationData, EntityDict, OperateOption, Q_FullTextValue, Ref, RefOrExpression, SelectOption, StorageSchema } from "oak-domain/lib/types";
1
+ import { EntityDict, OperateOption, Q_FullTextValue, Ref, RefOrExpression, SelectOption, StorageSchema } from "oak-domain/lib/types";
2
2
  import { DataType } from "oak-domain/lib/types/schema/DataTypes";
3
3
  export interface SqlSelectOption extends SelectOption {
4
4
  }
@@ -18,9 +18,9 @@ export declare abstract class SqlTranslator<ED extends EntityDict> {
18
18
  protected abstract populateSelectStmt<T extends keyof ED, OP extends SqlSelectOption>(projectionText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, groupByText?: string, indexFrom?: number, count?: number, option?: OP, selection?: ED[T]['Selection'], aggregation?: ED[T]['Aggregation']): string;
19
19
  protected abstract populateUpdateStmt<OP extends SqlOperateOption>(updateText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: OP): string;
20
20
  protected abstract populateRemoveStmt<OP extends SqlOperateOption>(removeText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: OP): string;
21
- protected abstract translateExpression<T extends keyof ED>(alias: string, expression: RefOrExpression<keyof ED[T]['OpSchema']>, refDict: Record<string, string>): string;
21
+ protected abstract translateExpression<T extends keyof ED>(entity: T, alias: string, expression: RefOrExpression<keyof ED[T]['OpSchema']>, refDict: Record<string, [string, keyof ED]>): string;
22
22
  private getStorageName;
23
- translateInsert<T extends keyof ED>(entity: T, data: DeduceCreateOperationData<ED[T]['OpSchema']>[]): string;
23
+ translateInsert<T extends keyof ED>(entity: T, data: ED[T]['CreateMulti']['data']): string;
24
24
  /**
25
25
  * analyze the join relations in projection/query/sort
26
26
  * 所有的层次关系都当成left join处理,如果有内表为空的情况,请手动处理
@@ -195,6 +195,8 @@ var SqlTranslator = /** @class */ (function () {
195
195
  alias: alias,
196
196
  });
197
197
  }
198
+ else if (['$text'].includes(op)) {
199
+ }
198
200
  else {
199
201
  var rel = (0, relation_1.judgeRelation)(_this.schema, entityName, op);
200
202
  if (typeof rel === 'string') {
@@ -246,7 +248,7 @@ var SqlTranslator = /** @class */ (function () {
246
248
  if (node['#id']) {
247
249
  (0, assert_1.default)(!filterRefAlias[node['#id']]);
248
250
  (0, lodash_1.assign)(filterRefAlias, (_b = {},
249
- _b[node['#id']] = alias,
251
+ _b[node['#id']] = [alias, entityName],
250
252
  _b));
251
253
  }
252
254
  };
@@ -368,7 +370,7 @@ var SqlTranslator = /** @class */ (function () {
368
370
  if (node['#id']) {
369
371
  (0, assert_1.default)(!projectionRefAlias[node['#id']], "projection\u4E0A\u6709\u91CD\u590D\u7684#id\u5B9A\u4E49\u300C".concat(node['#id'], "\u300D"));
370
372
  (0, lodash_1.assign)(projectionRefAlias, (_b = {},
371
- _b[node['#id']] = alias,
373
+ _b[node['#id']] = [alias, entityName],
372
374
  _b));
373
375
  }
374
376
  };
@@ -514,9 +516,9 @@ var SqlTranslator = /** @class */ (function () {
514
516
  if (whereText) {
515
517
  whereText += ' and ';
516
518
  }
517
- whereText + '(';
518
519
  if (['$and', '$or', '$xor', '$not'].includes(attr)) {
519
520
  var result = '';
521
+ whereText += '(';
520
522
  switch (attr) {
521
523
  case '$and':
522
524
  case '$or':
@@ -543,10 +545,11 @@ var SqlTranslator = /** @class */ (function () {
543
545
  }
544
546
  }
545
547
  }
548
+ whereText += ')';
546
549
  }
547
550
  else if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
548
551
  // expression
549
- whereText += " (".concat(_this.translateExpression(alias, filter2[attr], filterRefAlias), ")");
552
+ whereText += " (".concat(_this.translateExpression(entity2, alias, filter2[attr], filterRefAlias), ")");
550
553
  }
551
554
  else if (['$gt', '$gte', '$lt', '$lte', '$eq', '$ne', '$startsWith', '$endsWith', '$includes'].includes(attr)) {
552
555
  whereText += _this.translateComparison(attr, filter2[attr], type);
@@ -562,24 +565,23 @@ var SqlTranslator = /** @class */ (function () {
562
565
  else {
563
566
  var rel = (0, relation_1.judgeRelation)(_this.schema, entity2, attr);
564
567
  if (rel === 2) {
565
- whereText += " ".concat(translateInner(attr, "".concat(path).concat(attr, "/"), filter2[attr]));
568
+ whereText += " (".concat(translateInner(attr, "".concat(path).concat(attr, "/"), filter2[attr]), ")");
566
569
  }
567
570
  else if (typeof rel === 'string') {
568
- whereText += " ".concat(translateInner(rel, "".concat(path).concat(attr, "/"), filter2[attr]));
571
+ whereText += " (".concat(translateInner(rel, "".concat(path).concat(attr, "/"), filter2[attr]), ")");
569
572
  }
570
573
  else {
571
574
  (0, assert_1.default)(attributes.hasOwnProperty(attr), "\u975E\u6CD5\u7684\u5C5E\u6027".concat(attr));
572
575
  var type2 = attributes[attr].type;
573
576
  // assert (type2 !== 'ref');
574
577
  if (typeof filter2[attr] === 'object' && Object.keys(filter2[attr])[0] && Object.keys(filter2[attr])[0].startsWith('$')) {
575
- whereText += " `".concat(alias, "`.`").concat(attr, "` ").concat(translateInner(entity2, path, filter2[attr], type2));
578
+ whereText += " (`".concat(alias, "`.`").concat(attr, "` ").concat(translateInner(entity2, path, filter2[attr], type2), ")");
576
579
  }
577
580
  else {
578
- whereText += " `".concat(alias, "`.`").concat(attr, "` = ").concat(_this.translateAttrValue(type2, filter2[attr]));
581
+ whereText += " (`".concat(alias, "`.`").concat(attr, "` = ").concat(_this.translateAttrValue(type2, filter2[attr]), ")");
579
582
  }
580
583
  }
581
584
  }
582
- whereText + ')';
583
585
  });
584
586
  }
585
587
  if (!whereText) {
@@ -606,7 +608,7 @@ var SqlTranslator = /** @class */ (function () {
606
608
  var attr = Object.keys(sortAttr)[0];
607
609
  var alias = aliasDict[path];
608
610
  if (attr.toLocaleLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
609
- return _this.translateExpression(alias, sortAttr[attr], {});
611
+ return _this.translateExpression(entity2, alias, sortAttr[attr], {});
610
612
  }
611
613
  else if (sortAttr[attr] === 1) {
612
614
  return "`".concat(alias, "`.`").concat(attr, "`");
@@ -654,7 +656,7 @@ var SqlTranslator = /** @class */ (function () {
654
656
  attrs.forEach(function (attr, idx) {
655
657
  var prefix2 = commonPrefix ? "".concat(commonPrefix, ".").concat(prefix) : prefix;
656
658
  if (attr.toLowerCase().startsWith(types_1.EXPRESSION_PREFIX)) {
657
- var exprText = _this.translateExpression(alias, projection2[attr], projectionRefAlias);
659
+ var exprText = _this.translateExpression(entity2, alias, projection2[attr], projectionRefAlias);
658
660
  if (disableAs) {
659
661
  projText += " ".concat(exprText);
660
662
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-db",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "oak-db",
5
5
  "main": "lib/index",
6
6
  "author": {
@@ -18,7 +18,7 @@
18
18
  "lodash": "^4.17.21",
19
19
  "mysql": "^2.18.1",
20
20
  "mysql2": "^2.3.3",
21
- "oak-domain": "^2.3.0",
21
+ "oak-domain": "^2.4.0",
22
22
  "uuid": "^8.3.2"
23
23
  },
24
24
  "license": "ISC",
@@ -29,7 +29,7 @@
29
29
  "@types/node": "^17.0.42",
30
30
  "@types/uuid": "^8.3.4",
31
31
  "mocha": "^10.0.0",
32
- "oak-general-business": "^2.3.0",
32
+ "oak-general-business": "^2.3.2",
33
33
  "ts-node": "~10.9.1",
34
34
  "tslib": "^2.4.0",
35
35
  "typescript": "~4.7.4"