oak-db 3.2.0 → 3.3.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.
@@ -1,108 +1,107 @@
1
- import { EntityDict, Q_FullTextValue, RefOrExpression, Ref, StorageSchema } from "oak-domain/lib/types";
2
- import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
3
- import { DataType } from "oak-domain/lib/types/schema/DataTypes";
4
- import { SqlOperateOption, SqlSelectOption, SqlTranslator } from "../sqlTranslator";
5
- export interface MySqlSelectOption extends SqlSelectOption {
6
- }
7
- export interface MysqlOperateOption extends SqlOperateOption {
8
- }
9
- export declare class MySqlTranslator<ED extends EntityDict & BaseEntityDict> extends SqlTranslator<ED> {
10
- protected getDefaultSelectFilter(alias: string, option?: MySqlSelectOption): string;
11
- private makeUpSchema;
12
- constructor(schema: StorageSchema<ED>);
13
- static supportedDataTypes: DataType[];
14
- static spatialTypes: DataType[];
15
- static withLengthDataTypes: DataType[];
16
- static withPrecisionDataTypes: DataType[];
17
- static withScaleDataTypes: DataType[];
18
- static unsignedAndZerofillTypes: DataType[];
19
- static withWidthDataTypes: DataType[];
20
- static dataTypeDefaults: {
21
- varchar: {
22
- length: number;
23
- };
24
- nvarchar: {
25
- length: number;
26
- };
27
- "national varchar": {
28
- length: number;
29
- };
30
- char: {
31
- length: number;
32
- };
33
- binary: {
34
- length: number;
35
- };
36
- varbinary: {
37
- length: number;
38
- };
39
- decimal: {
40
- precision: number;
41
- scale: number;
42
- };
43
- dec: {
44
- precision: number;
45
- scale: number;
46
- };
47
- numeric: {
48
- precision: number;
49
- scale: number;
50
- };
51
- fixed: {
52
- precision: number;
53
- scale: number;
54
- };
55
- float: {
56
- precision: number;
57
- };
58
- double: {
59
- precision: number;
60
- };
61
- time: {
62
- precision: number;
63
- };
64
- datetime: {
65
- precision: number;
66
- };
67
- timestamp: {
68
- precision: number;
69
- };
70
- bit: {
71
- width: number;
72
- };
73
- int: {
74
- width: number;
75
- };
76
- integer: {
77
- width: number;
78
- };
79
- tinyint: {
80
- width: number;
81
- };
82
- smallint: {
83
- width: number;
84
- };
85
- mediumint: {
86
- width: number;
87
- };
88
- bigint: {
89
- width: number;
90
- };
91
- };
92
- maxAliasLength: number;
93
- private populateDataTypeDef;
94
- protected translateAttrProjection(dataType: DataType, alias: string, attr: string): string;
95
- protected translateObjectPredicate(predicate: Record<string, any>, alias: string, attr: string): string;
96
- protected translateObjectProjection(projection: Record<string, any>, alias: string, attr: string, prefix: string): string;
97
- protected translateAttrValue(dataType: DataType | Ref, value: any): string;
98
- protected translateFullTextSearch<T extends keyof ED>(value: Q_FullTextValue, entity: T, alias: string): string;
99
- translateCreateEntity<T extends keyof ED>(entity: T, options?: {
100
- replace?: boolean;
101
- }): string[];
102
- private translateFnName;
103
- private translateAttrInExpression;
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
- 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
- protected populateUpdateStmt(updateText: 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
- }
1
+ import { EntityDict, Q_FullTextValue, RefOrExpression, Ref, StorageSchema } from "oak-domain/lib/types";
2
+ import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
3
+ import { DataType } from "oak-domain/lib/types/schema/DataTypes";
4
+ import { SqlOperateOption, SqlSelectOption, SqlTranslator } from "../sqlTranslator";
5
+ import { CreateEntityOption } from '../types/Translator';
6
+ export interface MySqlSelectOption extends SqlSelectOption {
7
+ }
8
+ export interface MysqlOperateOption extends SqlOperateOption {
9
+ }
10
+ export declare class MySqlTranslator<ED extends EntityDict & BaseEntityDict> extends SqlTranslator<ED> {
11
+ protected getDefaultSelectFilter(alias: string, option?: MySqlSelectOption): string;
12
+ private makeUpSchema;
13
+ constructor(schema: StorageSchema<ED>);
14
+ static supportedDataTypes: DataType[];
15
+ static spatialTypes: DataType[];
16
+ static withLengthDataTypes: DataType[];
17
+ static withPrecisionDataTypes: DataType[];
18
+ static withScaleDataTypes: DataType[];
19
+ static unsignedAndZerofillTypes: DataType[];
20
+ static withWidthDataTypes: DataType[];
21
+ static dataTypeDefaults: {
22
+ varchar: {
23
+ length: number;
24
+ };
25
+ nvarchar: {
26
+ length: number;
27
+ };
28
+ "national varchar": {
29
+ length: number;
30
+ };
31
+ char: {
32
+ length: number;
33
+ };
34
+ binary: {
35
+ length: number;
36
+ };
37
+ varbinary: {
38
+ length: number;
39
+ };
40
+ decimal: {
41
+ precision: number;
42
+ scale: number;
43
+ };
44
+ dec: {
45
+ precision: number;
46
+ scale: number;
47
+ };
48
+ numeric: {
49
+ precision: number;
50
+ scale: number;
51
+ };
52
+ fixed: {
53
+ precision: number;
54
+ scale: number;
55
+ };
56
+ float: {
57
+ precision: number;
58
+ };
59
+ double: {
60
+ precision: number;
61
+ };
62
+ time: {
63
+ precision: number;
64
+ };
65
+ datetime: {
66
+ precision: number;
67
+ };
68
+ timestamp: {
69
+ precision: number;
70
+ };
71
+ bit: {
72
+ width: number;
73
+ };
74
+ int: {
75
+ width: number;
76
+ };
77
+ integer: {
78
+ width: number;
79
+ };
80
+ tinyint: {
81
+ width: number;
82
+ };
83
+ smallint: {
84
+ width: number;
85
+ };
86
+ mediumint: {
87
+ width: number;
88
+ };
89
+ bigint: {
90
+ width: number;
91
+ };
92
+ };
93
+ maxAliasLength: number;
94
+ private populateDataTypeDef;
95
+ protected translateAttrProjection(dataType: DataType, alias: string, attr: string): string;
96
+ protected translateObjectPredicate(predicate: Record<string, any>, alias: string, attr: string): string;
97
+ protected translateObjectProjection(projection: Record<string, any>, alias: string, attr: string, prefix: string): string;
98
+ protected translateAttrValue(dataType: DataType | Ref, value: any): string;
99
+ protected translateFullTextSearch<T extends keyof ED>(value: Q_FullTextValue, entity: T, alias: string): string;
100
+ translateCreateEntity<T extends keyof ED>(entity: T, options?: CreateEntityOption): string[];
101
+ private translateFnName;
102
+ private translateAttrInExpression;
103
+ protected translateExpression<T extends keyof ED>(entity: T, alias: string, expression: RefOrExpression<keyof ED[T]["OpSchema"]>, refDict: Record<string, [string, keyof ED]>): string;
104
+ 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;
105
+ protected populateUpdateStmt(updateText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: MysqlOperateOption): string;
106
+ protected populateRemoveStmt(updateText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: MysqlOperateOption): string;
107
+ }
@@ -527,14 +527,17 @@ class MySqlTranslator extends sqlTranslator_1.SqlTranslator {
527
527
  return ` match(${columns2.join(',')}) against ('${$search}' in natural language mode)`;
528
528
  }
529
529
  translateCreateEntity(entity, options) {
530
- const replace = options?.replace;
530
+ const ifExists = options?.ifExists;
531
531
  const { schema } = this;
532
532
  const entityDef = schema[entity];
533
- const { storageName, attributes, indexes, view } = entityDef;
533
+ const { storageName, attributes, indexes, view, static: _static } = entityDef;
534
534
  let hasSequence = false;
535
535
  // todo view暂还不支持
536
536
  const entityType = view ? 'view' : 'table';
537
537
  let sql = `create ${entityType} `;
538
+ if (ifExists === 'omit' || (_static && ifExists === 'dropIfNotStatic')) {
539
+ sql += ' if not exists';
540
+ }
538
541
  if (storageName) {
539
542
  sql += `\`${storageName}\` `;
540
543
  }
@@ -629,10 +632,10 @@ class MySqlTranslator extends sqlTranslator_1.SqlTranslator {
629
632
  if (typeof hasSequence === 'number') {
630
633
  sql += `auto_increment = ${hasSequence}`;
631
634
  }
632
- if (!replace) {
633
- return [sql];
635
+ if (ifExists === 'drop' || (!_static && ifExists === 'dropIfNotStatic')) {
636
+ return [`drop ${entityType} if exists \`${storageName || entity}\`;`, sql];
634
637
  }
635
- return [`drop ${entityType} if exists \`${storageName || entity}\`;`, sql];
638
+ return [sql];
636
639
  }
637
640
  translateFnName(fnName, argumentNumber) {
638
641
  switch (fnName) {
@@ -1,11 +1,12 @@
1
- export type MySQLConfiguration = {
2
- host: string;
3
- user: string;
4
- password: string;
5
- database: string;
6
- charset: 'utf8mb4_general_ci';
7
- connectionLimit: number;
8
- };
9
- export type Configuration = {
10
- mysql: MySQLConfiguration;
11
- };
1
+ export type MySQLConfiguration = {
2
+ host: string;
3
+ user: string;
4
+ password: string;
5
+ database: string;
6
+ charset: 'utf8mb4_general_ci';
7
+ connectionLimit: number;
8
+ port?: number;
9
+ };
10
+ export type Configuration = {
11
+ mysql: MySQLConfiguration;
12
+ };
@@ -1,56 +1,55 @@
1
- import { EntityDict, OperateOption, Q_FullTextValue, Ref, RefOrExpression, SelectOption, StorageSchema } from "oak-domain/lib/types";
2
- import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
3
- import { DataType } from "oak-domain/lib/types/schema/DataTypes";
4
- export interface SqlSelectOption extends SelectOption {
5
- }
6
- export interface SqlOperateOption extends OperateOption {
7
- }
8
- export declare abstract class SqlTranslator<ED extends EntityDict & BaseEntityDict> {
9
- readonly schema: StorageSchema<ED>;
10
- constructor(schema: StorageSchema<ED>);
11
- private makeFullSchema;
12
- protected abstract getDefaultSelectFilter<OP extends SqlSelectOption>(alias: string, option?: OP): string;
13
- protected abstract translateAttrProjection(dataType: DataType, alias: string, attr: string): string;
14
- protected abstract translateObjectProjection(projection: Record<string, any>, alias: string, attr: string, prefix: string): string;
15
- protected abstract translateAttrValue(dataType: DataType | Ref, value: any): string;
16
- protected abstract translateFullTextSearch<T extends keyof ED>(value: Q_FullTextValue, entity: T, alias: string): string;
17
- abstract translateCreateEntity<T extends keyof ED>(entity: T, option: {
18
- replace?: boolean;
19
- }): string[];
20
- protected abstract translateObjectPredicate(predicate: Record<string, any>, alias: string, attr: string): string;
21
- 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;
22
- 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;
23
- protected abstract populateRemoveStmt<OP extends SqlOperateOption>(updateText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: OP): string;
24
- protected abstract translateExpression<T extends keyof ED>(entity: T, alias: string, expression: RefOrExpression<keyof ED[T]['OpSchema']>, refDict: Record<string, [string, keyof ED]>): string;
25
- private getStorageName;
26
- translateInsert<T extends keyof ED>(entity: T, data: ED[T]['CreateMulti']['data']): string;
27
- /**
28
- * analyze the join relations in projection/query/sort
29
- * 所有的层次关系都当成left join处理,如果有内表为空的情况,请手动处理
30
- * {
31
- * b: {
32
- * name: {
33
- * $exists: false,
34
- * }
35
- * }
36
- * }
37
- * 这样的query会把内表为空的行也返回
38
- * @param param0
39
- */
40
- private analyzeJoin;
41
- private translateComparison;
42
- private translateEvaluation;
43
- protected translatePredicate(predicate: string, value: any, type?: DataType | Ref): string;
44
- private translateFilter;
45
- private translateSorter;
46
- private translateProjection;
47
- private translateSelectInner;
48
- translateSelect<T extends keyof ED, OP extends SqlSelectOption>(entity: T, selection: ED[T]['Selection'], option?: OP): string;
49
- translateWhere<T extends keyof ED, OP extends SqlSelectOption>(entity: T, selection: ED[T]['Selection'], option?: OP): string;
50
- translateAggregate<T extends keyof ED, OP extends SqlSelectOption>(entity: T, aggregation: ED[T]['Aggregation'], option?: OP): string;
51
- translateCount<T extends keyof ED, OP extends SqlSelectOption>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, option?: OP): string;
52
- translateRemove<T extends keyof ED, OP extends SqlOperateOption>(entity: T, operation: ED[T]['Remove'], option?: OP): string;
53
- translateUpdate<T extends keyof ED, OP extends SqlOperateOption>(entity: T, operation: ED[T]['Update'], option?: OP): string;
54
- translateDestroyEntity(entity: string, truncate?: boolean): string;
55
- escapeStringValue(value: string): string;
56
- }
1
+ import { EntityDict, OperateOption, Q_FullTextValue, Ref, RefOrExpression, SelectOption, StorageSchema } from "oak-domain/lib/types";
2
+ import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
3
+ import { DataType } from "oak-domain/lib/types/schema/DataTypes";
4
+ import { CreateEntityOption } from './types/Translator';
5
+ export interface SqlSelectOption extends SelectOption {
6
+ }
7
+ export interface SqlOperateOption extends OperateOption {
8
+ }
9
+ export declare abstract class SqlTranslator<ED extends EntityDict & BaseEntityDict> {
10
+ readonly schema: StorageSchema<ED>;
11
+ constructor(schema: StorageSchema<ED>);
12
+ private makeFullSchema;
13
+ protected abstract getDefaultSelectFilter<OP extends SqlSelectOption>(alias: string, option?: OP): string;
14
+ protected abstract translateAttrProjection(dataType: DataType, alias: string, attr: string): string;
15
+ protected abstract translateObjectProjection(projection: Record<string, any>, alias: string, attr: string, prefix: string): string;
16
+ protected abstract translateAttrValue(dataType: DataType | Ref, value: any): string;
17
+ protected abstract translateFullTextSearch<T extends keyof ED>(value: Q_FullTextValue, entity: T, alias: string): string;
18
+ abstract translateCreateEntity<T extends keyof ED>(entity: T, option: CreateEntityOption): string[];
19
+ protected abstract translateObjectPredicate(predicate: Record<string, any>, alias: string, attr: string): string;
20
+ 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;
21
+ 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;
22
+ protected abstract populateRemoveStmt<OP extends SqlOperateOption>(updateText: string, fromText: string, aliasDict: Record<string, string>, filterText: string, sorterText?: string, indexFrom?: number, count?: number, option?: OP): string;
23
+ protected abstract translateExpression<T extends keyof ED>(entity: T, alias: string, expression: RefOrExpression<keyof ED[T]['OpSchema']>, refDict: Record<string, [string, keyof ED]>): string;
24
+ private getStorageName;
25
+ translateInsert<T extends keyof ED>(entity: T, data: ED[T]['CreateMulti']['data']): string;
26
+ /**
27
+ * analyze the join relations in projection/query/sort
28
+ * 所有的层次关系都当成left join处理,如果有内表为空的情况,请手动处理
29
+ * {
30
+ * b: {
31
+ * name: {
32
+ * $exists: false,
33
+ * }
34
+ * }
35
+ * }
36
+ * 这样的query会把内表为空的行也返回
37
+ * @param param0
38
+ */
39
+ private analyzeJoin;
40
+ private translateComparison;
41
+ private translateEvaluation;
42
+ protected translatePredicate(predicate: string, value: any, type?: DataType | Ref): string;
43
+ private translateFilter;
44
+ private translateSorter;
45
+ private translateProjection;
46
+ private translateSelectInner;
47
+ translateSelect<T extends keyof ED, OP extends SqlSelectOption>(entity: T, selection: ED[T]['Selection'], option?: OP): string;
48
+ translateWhere<T extends keyof ED, OP extends SqlSelectOption>(entity: T, selection: ED[T]['Selection'], option?: OP): string;
49
+ translateAggregate<T extends keyof ED, OP extends SqlSelectOption>(entity: T, aggregation: ED[T]['Aggregation'], option?: OP): string;
50
+ translateCount<T extends keyof ED, OP extends SqlSelectOption>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, option?: OP): string;
51
+ translateRemove<T extends keyof ED, OP extends SqlOperateOption>(entity: T, operation: ED[T]['Remove'], option?: OP): string;
52
+ translateUpdate<T extends keyof ED, OP extends SqlOperateOption>(entity: T, operation: ED[T]['Update'], option?: OP): string;
53
+ translateDestroyEntity(entity: string, truncate?: boolean): string;
54
+ escapeStringValue(value: string): string;
55
+ }
@@ -0,0 +1,3 @@
1
+ export type CreateEntityOption = {
2
+ ifExists?: 'drop' | 'omit' | 'dropIfNotStatic';
3
+ };
@@ -1 +1,2 @@
1
- "use strict";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-db",
3
- "version": "3.2.0",
3
+ "version": "3.3.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.3.0",
21
+ "oak-domain": "^5.0.0",
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.3.0",
34
+ "oak-general-business": "^5.0.0",
35
35
  "ts-node": "^10.9.1",
36
36
  "tslib": "^2.4.0",
37
37
  "typescript": "^5.2.2"