oak-db 3.0.3 → 3.1.0

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.
@@ -7,14 +7,15 @@ import { MySqlTranslator, MySqlSelectOption, MysqlOperateOption } from './transl
7
7
  import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
8
8
  import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
9
9
  export declare class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends CascadeStore<ED> implements AsyncRowStore<ED, Cxt> {
10
- protected aggregateSync<T extends keyof ED, OP extends SelectOption, Cxt extends SyncContext<ED>>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): AggregationResult<ED[T]['Schema']>;
10
+ protected countAbjointRow<T extends keyof ED, OP extends SelectOption, Cxt extends SyncContext<ED>>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: OP): number;
11
+ protected aggregateAbjointRowSync<T extends keyof ED, OP extends SelectOption, Cxt extends SyncContext<ED>>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): AggregationResult<ED[T]['Schema']>;
11
12
  protected selectAbjointRow<T extends keyof ED, OP extends SelectOption>(entity: T, selection: ED[T]['Selection'], context: SyncContext<ED>, option: OP): Partial<ED[T]['Schema']>[];
12
13
  protected updateAbjointRow<T extends keyof ED, OP extends OperateOption>(entity: T, operation: ED[T]['Operation'], context: SyncContext<ED>, option: OP): number;
13
14
  exec(script: string, txnId?: string): Promise<any>;
14
15
  connector: MySqlConnector;
15
16
  translator: MySqlTranslator<ED>;
16
17
  constructor(storageSchema: StorageSchema<ED>, configuration: MySQLConfiguration);
17
- protected aggregateAsync<T extends keyof ED, OP extends SelectOption, Cxt extends AsyncContext<ED>>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): Promise<AggregationResult<ED[T]['Schema']>>;
18
+ protected aggregateAbjointRowAsync<T extends keyof ED, OP extends SelectOption, Cxt extends AsyncContext<ED>>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): Promise<AggregationResult<ED[T]['Schema']>>;
18
19
  aggregate<T extends keyof ED, OP extends SelectOption>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): Promise<AggregationResult<ED[T]['Schema']>>;
19
20
  protected supportManyToOneJoin(): boolean;
20
21
  protected supportMultipleCreate(): boolean;
@@ -23,7 +24,7 @@ export declare class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt exte
23
24
  protected updateAbjointRowAsync<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: AsyncContext<ED>, option?: MysqlOperateOption): Promise<number>;
24
25
  operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Cxt, option: OperateOption): Promise<OperationResult<ED>>;
25
26
  select<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Cxt, option: SelectOption): Promise<Partial<ED[T]['Schema']>[]>;
26
- protected countAsync<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: AsyncContext<ED>, option: SelectOption): Promise<number>;
27
+ protected countAbjointRowAsync<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: AsyncContext<ED>, option: SelectOption): Promise<number>;
27
28
  count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: SelectOption): Promise<number>;
28
29
  begin(option?: TxnOption): Promise<string>;
29
30
  commit(txnId: string): Promise<void>;
@@ -21,7 +21,10 @@ function convertGeoTextToObject(geoText) {
21
21
  }
22
22
  }
23
23
  class MysqlStore extends CascadeStore_1.CascadeStore {
24
- aggregateSync(entity, aggregation, context, option) {
24
+ countAbjointRow(entity, selection, context, option) {
25
+ throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
26
+ }
27
+ aggregateAbjointRowSync(entity, aggregation, context, option) {
25
28
  throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
26
29
  }
27
30
  selectAbjointRow(entity, selection, context, option) {
@@ -40,7 +43,7 @@ class MysqlStore extends CascadeStore_1.CascadeStore {
40
43
  this.connector = new connector_1.MySqlConnector(configuration);
41
44
  this.translator = new translator_1.MySqlTranslator(storageSchema);
42
45
  }
43
- async aggregateAsync(entity, aggregation, context, option) {
46
+ async aggregateAbjointRowAsync(entity, aggregation, context, option) {
44
47
  const sql = this.translator.translateAggregate(entity, aggregation, option);
45
48
  const result = await this.connector.exec(sql, context.getCurrentTxnId());
46
49
  return this.formResult(entity, result);
@@ -264,7 +267,7 @@ class MysqlStore extends CascadeStore_1.CascadeStore {
264
267
  const result = await super.selectAsync(entity, selection, context, option);
265
268
  return result;
266
269
  }
267
- async countAsync(entity, selection, context, option) {
270
+ async countAbjointRowAsync(entity, selection, context, option) {
268
271
  const sql = this.translator.translateCount(entity, selection, option);
269
272
  const result = await this.connector.exec(sql, context.getCurrentTxnId());
270
273
  return result[0].cnt;
@@ -522,6 +522,10 @@ class SqlTranslator {
522
522
  // between是所有数据库都支持的语法吗?
523
523
  return ` between ${values[0]} and ${values[1]}`;
524
524
  }
525
+ else if (predicate === '$mod') {
526
+ // %是所有数据库都支持的语法吗?
527
+ return ` % ${value[0]} = ${value[1]}`;
528
+ }
525
529
  else {
526
530
  (0, assert_1.default)(predicate === '$exists');
527
531
  if (value) {
@@ -818,7 +822,7 @@ class SqlTranslator {
818
822
  };
819
823
  }
820
824
  translateSelectInner(entity, selection, initialNumber, refAlias, option) {
821
- const { data, filter, sorter, indexFrom, count } = selection;
825
+ const { data, filter, sorter, indexFrom, count, distinct } = selection;
822
826
  const { from: fromText, aliasDict, projectionRefAlias, filterRefAlias, currentNumber } = this.analyzeJoin(entity, {
823
827
  projection: data,
824
828
  filter,
@@ -826,7 +830,10 @@ class SqlTranslator {
826
830
  }, initialNumber);
827
831
  (0, assert_1.default)((0, lodash_1.intersection)((0, lodash_1.keys)(refAlias), (0, lodash_1.keys)(filterRefAlias)).length === 0, 'filter中的#node结点定义有重复');
828
832
  (0, lodash_1.assign)(refAlias, filterRefAlias);
829
- const { projText } = this.translateProjection(entity, data, aliasDict, projectionRefAlias);
833
+ let { projText } = this.translateProjection(entity, data, aliasDict, projectionRefAlias);
834
+ if (distinct) {
835
+ projText = `distinct ${projText}`;
836
+ }
830
837
  const { stmt: filterText, currentNumber: currentNumber2 } = this.translateFilter(entity, filter, aliasDict, refAlias, currentNumber, option);
831
838
  const sorterText = sorter && this.translateSorter(entity, sorter, aliasDict);
832
839
  return {
@@ -844,7 +851,7 @@ class SqlTranslator {
844
851
  return filterStmt;
845
852
  }
846
853
  translateAggregate(entity, aggregation, option) {
847
- const { data, filter, sorter, indexFrom, count } = aggregation;
854
+ const { data, filter, sorter, indexFrom, count, distinct } = aggregation;
848
855
  const { from: fromText, aliasDict, projectionRefAlias, filterRefAlias, currentNumber } = this.analyzeJoin(entity, {
849
856
  aggregation: data,
850
857
  filter,
@@ -863,8 +870,8 @@ class SqlTranslator {
863
870
  }
864
871
  groupByText = as;
865
872
  }
866
- else {
867
- const { projText: projSubText } = this.translateProjection(entity, data[k], aliasDict, projectionRefAlias, undefined, true);
873
+ else if (k.startsWith('#')) {
874
+ let { projText: projSubText } = this.translateProjection(entity, data[k], aliasDict, projectionRefAlias, undefined, true);
868
875
  let projSubText2 = '';
869
876
  if (k.startsWith('#max')) {
870
877
  projSubText2 = `max(${projSubText}) as \`${k}\``;
@@ -873,12 +880,21 @@ class SqlTranslator {
873
880
  projSubText2 = `min(${projSubText}) as \`${k}\``;
874
881
  }
875
882
  else if (k.startsWith('#count')) {
883
+ if (data.distinct) {
884
+ projSubText = `distinct ${projSubText}`;
885
+ }
876
886
  projSubText2 = `count(${projSubText}) as \`${k}\``;
877
887
  }
878
888
  else if (k.startsWith('#sum')) {
889
+ if (data.distinct) {
890
+ projSubText = `distinct ${projSubText}`;
891
+ }
879
892
  projSubText2 = `sum(${projSubText}) as \`${k}\``;
880
893
  }
881
894
  else {
895
+ if (data.distinct) {
896
+ projSubText = `distinct ${projSubText}`;
897
+ }
882
898
  (0, assert_1.default)(k.startsWith('#avg'));
883
899
  projSubText2 = `avg(${projSubText}) as \`${k}\``;
884
900
  }
@@ -902,7 +918,7 @@ class SqlTranslator {
902
918
  const projText = 'count(1) cnt';
903
919
  const { stmt: filterText } = this.translateFilter(entity, filter, aliasDict, filterRefAlias, currentNumber, option);
904
920
  if (count && count > 0) {
905
- const subQuerySql = this.populateSelectStmt('1', fromText, aliasDict, filterText, undefined, undefined, undefined, undefined, option, Object.assign({}, selection, { indexFrom: 0, count }));
921
+ const subQuerySql = this.populateSelectStmt('1', fromText, aliasDict, filterText, undefined, undefined, 0, count, option, Object.assign({}, selection, { indexFrom: 0, count }));
906
922
  return `select count(1) cnt from (${subQuerySql}) __tmp`;
907
923
  }
908
924
  return this.populateSelectStmt(projText, fromText, aliasDict, filterText, undefined, undefined, undefined, undefined, option, selection);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-db",
3
- "version": "3.0.3",
3
+ "version": "3.1.0",
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": "^4.0.0",
21
+ "oak-domain": "^4.0.2",
22
22
  "uuid": "^8.3.2"
23
23
  },
24
24
  "license": "ISC",
@@ -31,7 +31,7 @@
31
31
  "@types/uuid": "^8.3.4",
32
32
  "cross-env": "^7.0.3",
33
33
  "mocha": "^10.2.0",
34
- "oak-general-business": "^4.0.0",
34
+ "oak-general-business": "^4.0.3",
35
35
  "ts-node": "^10.9.1",
36
36
  "tslib": "^2.4.0",
37
37
  "typescript": "^5.2.2"