@steedos/objectql 2.2.50 → 2.2.51-beta.4
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.
- package/lib/services/helpers/graphql/consts.d.ts +17 -0
- package/lib/services/helpers/graphql/consts.js +28 -0
- package/lib/services/helpers/graphql/consts.js.map +1 -0
- package/lib/services/helpers/graphql/getQueryFields.js +17 -2
- package/lib/services/helpers/graphql/getQueryFields.js.map +1 -1
- package/lib/services/helpers/graphql/index.d.ts +1 -1
- package/lib/services/helpers/graphql/index.js +31 -46
- package/lib/services/helpers/graphql/index.js.map +1 -1
- package/lib/util/index.js +7 -0
- package/lib/util/index.js.map +1 -1
- package/package.json +11 -12
- package/src/actions/field_updates.ts +0 -118
- package/src/actions/index.ts +0 -3
- package/src/actions/types/field_update_target.ts +0 -7
- package/src/actions/types/workflow_notification.ts +0 -9
- package/src/actions/types/workflow_rule.ts +0 -11
- package/src/actions/workflow_notifications.ts +0 -81
- package/src/actions/workflow_rule.ts +0 -136
- package/src/driver/driver.ts +0 -102
- package/src/driver/field-encrytion/index.ts +0 -8
- package/src/driver/field-encrytion/sharedconst.ts +0 -34
- package/src/driver/fieldDBType.ts +0 -14
- package/src/driver/format.ts +0 -36
- package/src/driver/index.ts +0 -20
- package/src/driver/metadata.ts +0 -226
- package/src/driver/meteorMongo.ts +0 -639
- package/src/driver/mongo.ts +0 -416
- package/src/driver/mysql.ts +0 -47
- package/src/driver/oracle.ts +0 -60
- package/src/driver/postgres.ts +0 -46
- package/src/driver/sqlite3.ts +0 -40
- package/src/driver/sqlserver.ts +0 -52
- package/src/dynamic-load/actions.ts +0 -146
- package/src/dynamic-load/approval_process.ts +0 -73
- package/src/dynamic-load/button.ts +0 -75
- package/src/dynamic-load/chart.ts +0 -22
- package/src/dynamic-load/client_script.ts +0 -65
- package/src/dynamic-load/field.ts +0 -77
- package/src/dynamic-load/flow_role.ts +0 -46
- package/src/dynamic-load/index.ts +0 -21
- package/src/dynamic-load/layout.ts +0 -53
- package/src/dynamic-load/listview.ts +0 -57
- package/src/dynamic-load/method.ts +0 -63
- package/src/dynamic-load/object_translations.ts +0 -61
- package/src/dynamic-load/originalObject.ts +0 -8
- package/src/dynamic-load/package.ts +0 -312
- package/src/dynamic-load/page.ts +0 -23
- package/src/dynamic-load/permission.ts +0 -71
- package/src/dynamic-load/permissionset.ts +0 -78
- package/src/dynamic-load/preload_data.ts +0 -104
- package/src/dynamic-load/profile.ts +0 -90
- package/src/dynamic-load/query.ts +0 -22
- package/src/dynamic-load/restrictionRules.ts +0 -23
- package/src/dynamic-load/role.ts +0 -46
- package/src/dynamic-load/shareRules.ts +0 -23
- package/src/dynamic-load/tab.ts +0 -17
- package/src/dynamic-load/tabs.ts +0 -13
- package/src/dynamic-load/translations.ts +0 -54
- package/src/dynamic-load/trigger.ts +0 -236
- package/src/dynamic-load/validation_rule.ts +0 -77
- package/src/dynamic-load/workflow.ts +0 -114
- package/src/errors/index.ts +0 -111
- package/src/formula/core.ts +0 -490
- package/src/formula/field_formula.ts +0 -107
- package/src/formula/index.ts +0 -81
- package/src/formula/params.ts +0 -197
- package/src/formula/recompute.ts +0 -65
- package/src/formula/simple_params.ts +0 -92
- package/src/formula/type.ts +0 -107
- package/src/formula/util.ts +0 -207
- package/src/index.ts +0 -24
- package/src/metadata-register/_base.ts +0 -85
- package/src/metadata-register/app.ts +0 -30
- package/src/metadata-register/chart.ts +0 -9
- package/src/metadata-register/index.ts +0 -123
- package/src/metadata-register/layout.ts +0 -38
- package/src/metadata-register/object.ts +0 -68
- package/src/metadata-register/page.ts +0 -9
- package/src/metadata-register/permissionFields.ts +0 -13
- package/src/metadata-register/permissionset.ts +0 -25
- package/src/metadata-register/process.ts +0 -16
- package/src/metadata-register/processTrigger.ts +0 -24
- package/src/metadata-register/profile.ts +0 -25
- package/src/metadata-register/query.ts +0 -9
- package/src/metadata-register/restrictionRules.ts +0 -12
- package/src/metadata-register/shareRules.ts +0 -13
- package/src/metadata-register/tab.ts +0 -9
- package/src/metadata-register/tabs.ts +0 -43
- package/src/services/datasourceServiceFactory.ts +0 -55
- package/src/services/helpers/graphql/getPrimaryFieldType.ts +0 -48
- package/src/services/helpers/graphql/getQueryFields.ts +0 -36
- package/src/services/helpers/graphql/index.ts +0 -681
- package/src/services/helpers/rest.ts +0 -57
- package/src/services/index.ts +0 -13
- package/src/services/objectService.ts +0 -846
- package/src/services/objectServiceDispatcher.ts +0 -209
- package/src/services/objectServiceFactory.ts +0 -29
- package/src/summary/core.ts +0 -263
- package/src/summary/field_summary.ts +0 -71
- package/src/summary/index.ts +0 -96
- package/src/summary/recompute.ts +0 -31
- package/src/summary/type.ts +0 -60
- package/src/ts-types/index.ts +0 -3
- package/src/ts-types/permissionset.ts +0 -8
- package/src/ts-types/profile.ts +0 -11
- package/src/ts-types/triggerActionParams.ts +0 -22
- package/src/typeorm/driver.ts +0 -379
- package/src/typeorm/index.ts +0 -3
- package/src/typeorm/util.ts +0 -147
- package/src/types/action.ts +0 -52
- package/src/types/app.ts +0 -280
- package/src/types/config.ts +0 -152
- package/src/types/connection.ts +0 -87
- package/src/types/dashboard.ts +0 -91
- package/src/types/datasource.ts +0 -570
- package/src/types/field.ts +0 -370
- package/src/types/field_permission.ts +0 -42
- package/src/types/field_types.ts +0 -1
- package/src/types/index.ts +0 -21
- package/src/types/list_view.ts +0 -56
- package/src/types/listeners.ts +0 -18
- package/src/types/object.ts +0 -2076
- package/src/types/object_dynamic_load.ts +0 -426
- package/src/types/object_events.ts +0 -43
- package/src/types/object_layouts.ts +0 -20
- package/src/types/object_permission.ts +0 -134
- package/src/types/query.ts +0 -28
- package/src/types/report.ts +0 -128
- package/src/types/restrictionRule.ts +0 -57
- package/src/types/schema.ts +0 -273
- package/src/types/shareRule.ts +0 -57
- package/src/types/trigger.ts +0 -88
- package/src/types/userSession.ts +0 -45
- package/src/types/validation_rules.ts +0 -29
- package/src/util/convert.ts +0 -131
- package/src/util/field.ts +0 -93
- package/src/util/function_expression.ts +0 -63
- package/src/util/index.ts +0 -1058
- package/src/util/locale.ts +0 -24
- package/src/util/permission_shares.ts +0 -25
- package/src/util/suffix.ts +0 -78
- package/src/util/transform.ts +0 -239
- package/src/validators/index.ts +0 -36
package/src/summary/recompute.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { getSteedosSchema } from '../index';
|
|
2
|
-
import { SteedosFieldSummaryTypeConfig } from './type';
|
|
3
|
-
import { getFieldSummaryConfig } from './field_summary';
|
|
4
|
-
import { updateReferenceTosFieldSummaryValue } from './core';
|
|
5
|
-
|
|
6
|
-
const runCurrentFieldSummary = async function (fieldSummaryConfig: SteedosFieldSummaryTypeConfig, userSession: any) {
|
|
7
|
-
const { object_name: objectName } = fieldSummaryConfig;
|
|
8
|
-
const docs = await getSteedosSchema().getObject(objectName).find({ filters: [], fields: ["_id"] })
|
|
9
|
-
await updateReferenceTosFieldSummaryValue(docs, fieldSummaryConfig, userSession);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 重算指定累计汇总字段Id对应的汇总值
|
|
14
|
-
* @param fieldId
|
|
15
|
-
*/
|
|
16
|
-
export const recomputeSummaryValues = async (fieldId: string, userSession: any) => {
|
|
17
|
-
let config = await getFieldSummaryConfig(fieldId);
|
|
18
|
-
if (!config) {
|
|
19
|
-
throw new Error(`recomputeSummaryValues:${fieldId} not found in field_summary configs.`);
|
|
20
|
-
}
|
|
21
|
-
return await recomputeFieldSummaryValues(config, userSession);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 重算指定累计汇总字段的汇总值
|
|
26
|
-
* @param fieldSummaryConfig
|
|
27
|
-
*/
|
|
28
|
-
export const recomputeFieldSummaryValues = async (fieldSummaryConfig: SteedosFieldSummaryTypeConfig, userSession: any) => {
|
|
29
|
-
await runCurrentFieldSummary(fieldSummaryConfig, userSession);
|
|
30
|
-
return true;
|
|
31
|
-
}
|
package/src/summary/type.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { SteedosQueryFilters } from '../types';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 累计汇总字段配置结构
|
|
5
|
-
* 低代码/零代码中可以配置object_name、field_name、summary_object、summary_type、summary_field属性,其他属性都是算出来的
|
|
6
|
-
*/
|
|
7
|
-
export type SteedosFieldSummaryTypeConfig = {
|
|
8
|
-
_id: string, //低代码值为${objectConfig.name}.${fieldConfig.name},零代码为数据库中的_id值
|
|
9
|
-
object_name: string, //主表对象
|
|
10
|
-
field_name: string, //主表上的汇总字段
|
|
11
|
-
summary_object: string, //对汇总的子表对象
|
|
12
|
-
summary_type: SteedosSummaryTypeValue, //要汇总的类型
|
|
13
|
-
data_type: string, //数据类型,summary_type为count则为number,其他类型则关联到summary_field的数据类型
|
|
14
|
-
summary_field?: string, //要聚合的字段,只能是数值、金额、日期、日期时间字段类型
|
|
15
|
-
summary_filters?: SteedosQueryFilters, //要汇总的子表记录的过滤条件
|
|
16
|
-
reference_to_field?: string //子表上的master_detail关联到主表对象的字段,必须能找到且只能找到一个
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 累计汇总字段支持的计算类型
|
|
21
|
-
*/
|
|
22
|
-
export enum SteedosSummaryTypeValue {
|
|
23
|
-
COUNT = "count",
|
|
24
|
-
SUM = "sum",
|
|
25
|
-
MIN = "min",
|
|
26
|
-
MAX = "max",
|
|
27
|
-
AVG = "avg"
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 累计汇总字段支持的字段类型(特指数据库中最终保存的结果类型)
|
|
32
|
-
* 注意这时也是支持公式类型字段的,不过最终使用的是公式字段本身的数据类型
|
|
33
|
-
*/
|
|
34
|
-
export enum SteedosSummaryDataTypeValue {
|
|
35
|
-
Number = "number",
|
|
36
|
-
Currency = "currency",
|
|
37
|
-
Percent = "percent",
|
|
38
|
-
Date = "date",
|
|
39
|
-
Datetime = "datetime"
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 各个累计汇总计算类型支持的字段类型,用于判断SteedosFieldSummaryTypeConfig中配置的summary_field所指向的字段类型是否符合规范
|
|
44
|
-
*/
|
|
45
|
-
export const SupportedSummaryFieldTypes = {
|
|
46
|
-
"sum": [SteedosSummaryDataTypeValue.Number, SteedosSummaryDataTypeValue.Currency, SteedosSummaryDataTypeValue.Percent],
|
|
47
|
-
"avg": [SteedosSummaryDataTypeValue.Number, SteedosSummaryDataTypeValue.Currency, SteedosSummaryDataTypeValue.Percent],
|
|
48
|
-
"min": [SteedosSummaryDataTypeValue.Number, SteedosSummaryDataTypeValue.Currency, SteedosSummaryDataTypeValue.Percent, SteedosSummaryDataTypeValue.Date, SteedosSummaryDataTypeValue.Datetime],
|
|
49
|
-
"max": [SteedosSummaryDataTypeValue.Number, SteedosSummaryDataTypeValue.Currency, SteedosSummaryDataTypeValue.Percent, SteedosSummaryDataTypeValue.Date, SteedosSummaryDataTypeValue.Datetime]
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* 各个累计汇总计算类型的空值处理方式
|
|
54
|
-
*/
|
|
55
|
-
export const SteedosSummaryTypeBlankValue = {
|
|
56
|
-
"count": 0,
|
|
57
|
-
"sum": 0,
|
|
58
|
-
"min": null,
|
|
59
|
-
"max": null
|
|
60
|
-
};
|
package/src/ts-types/index.ts
DELETED
package/src/ts-types/profile.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { JsonMap } from "@salesforce/ts-types";
|
|
2
|
-
import { SteedosIDType } from "..";
|
|
3
|
-
|
|
4
|
-
export type TriggerActionParams = {
|
|
5
|
-
objectName?: string
|
|
6
|
-
isExecuting?: boolean,
|
|
7
|
-
isInsert?: boolean,
|
|
8
|
-
isUpdate?: boolean,
|
|
9
|
-
isDelete?: boolean,
|
|
10
|
-
isBefore?: boolean,
|
|
11
|
-
isAfter?: boolean,
|
|
12
|
-
isUndelete?: boolean,
|
|
13
|
-
new?: [JsonMap],
|
|
14
|
-
newMap?: [SteedosIDType],
|
|
15
|
-
old?: [JsonMap],
|
|
16
|
-
oldMap?: [SteedosIDType],
|
|
17
|
-
operationType?: "BEFORE_FIND" | "BEFORE_AGGREGATE" | "BEFORE_INSERT" | "BEFORE_UPDATE" | "BEFORE_DELETE" | "AFTER_FIND" | "AFTER_AGGREGATE" | "AFTER_COUNT" | "AFTER_FINDONE" | "AFTER_INSERT" | "AFTER_UPDATE" | "AFTER_DELETE" | "AFTER_UNDELETE",
|
|
18
|
-
size?: number,
|
|
19
|
-
userId: SteedosIDType,
|
|
20
|
-
spaceId?: SteedosIDType,
|
|
21
|
-
}
|
|
22
|
-
|
package/src/typeorm/driver.ts
DELETED
|
@@ -1,379 +0,0 @@
|
|
|
1
|
-
import { JsonMap, Dictionary } from "@salesforce/ts-types";
|
|
2
|
-
import { SteedosDriver, SteedosFieldDBType, SteedosDriverConfig } from "../driver";
|
|
3
|
-
import { createConnection, QueryRunner, EntitySchema, ConnectionOptions } from "typeorm";
|
|
4
|
-
import { SteedosQueryOptions, SteedosQueryFilters } from "../types/query";
|
|
5
|
-
import { SteedosIDType, SteedosObjectType } from "../types";
|
|
6
|
-
import { formatFiltersToODataQuery } from "@steedos/filters";
|
|
7
|
-
import { executeQuery, executeCountQuery, SqlOptions } from '@steedos/odata-v4-typeorm';
|
|
8
|
-
import { SQLLang } from 'odata-v4-sql';
|
|
9
|
-
import { getPrimaryKeys } from "../typeorm";
|
|
10
|
-
|
|
11
|
-
import _ = require("underscore");
|
|
12
|
-
|
|
13
|
-
export abstract class SteedosTypeormDriver implements SteedosDriver {
|
|
14
|
-
getSupportedColumnTypes() {
|
|
15
|
-
return [
|
|
16
|
-
SteedosFieldDBType.varchar,
|
|
17
|
-
SteedosFieldDBType.text,
|
|
18
|
-
SteedosFieldDBType.number,
|
|
19
|
-
SteedosFieldDBType.date,
|
|
20
|
-
SteedosFieldDBType.dateTime
|
|
21
|
-
]
|
|
22
|
-
}
|
|
23
|
-
_url: string;
|
|
24
|
-
_client: any;
|
|
25
|
-
databaseVersion?: string;
|
|
26
|
-
|
|
27
|
-
private _config: SteedosDriverConfig;
|
|
28
|
-
|
|
29
|
-
public get config(): SteedosDriverConfig {
|
|
30
|
-
return this._config;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
_queryRunner: QueryRunner;
|
|
34
|
-
_entities: Dictionary<EntitySchema>;
|
|
35
|
-
|
|
36
|
-
abstract readonly sqlLang: SQLLang;
|
|
37
|
-
|
|
38
|
-
constructor(config: SteedosDriverConfig) {
|
|
39
|
-
this._config = config;
|
|
40
|
-
this._url = config.url;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
abstract getConnectionOptions(): ConnectionOptions;
|
|
44
|
-
|
|
45
|
-
async connect(reconnect?) {
|
|
46
|
-
if (!this._entities) {
|
|
47
|
-
throw new Error("Entities must be registered before connect");
|
|
48
|
-
}
|
|
49
|
-
if (!this._client || reconnect) {
|
|
50
|
-
let options = this.getConnectionOptions();
|
|
51
|
-
this._client = await createConnection(options);
|
|
52
|
-
this.databaseVersion = await this.getDatabaseVersion();
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async close() {
|
|
58
|
-
if (this._client) {
|
|
59
|
-
await this._client.close();
|
|
60
|
-
this._client = null;
|
|
61
|
-
this._queryRunner = null;
|
|
62
|
-
return true;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async createQueryRunner() {
|
|
67
|
-
if (this._queryRunner) {
|
|
68
|
-
return this._queryRunner;
|
|
69
|
-
}
|
|
70
|
-
if (!this._client) {
|
|
71
|
-
await this.connect();
|
|
72
|
-
}
|
|
73
|
-
this._queryRunner = await this._client.driver.createQueryRunner("master");
|
|
74
|
-
return this._queryRunner;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
formatFiltersToTypeormQuery(filters: any): JsonMap {
|
|
78
|
-
let odataQuery: string = "";
|
|
79
|
-
if (_.isString(filters)) {
|
|
80
|
-
odataQuery = filters;
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
odataQuery = formatFiltersToODataQuery(filters)
|
|
84
|
-
}
|
|
85
|
-
return {
|
|
86
|
-
$filter: odataQuery
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
getTypeormFilters(filters: SteedosQueryFilters): JsonMap {
|
|
91
|
-
let emptyFilters = {
|
|
92
|
-
};
|
|
93
|
-
if (_.isUndefined(filters)) {
|
|
94
|
-
return emptyFilters;
|
|
95
|
-
}
|
|
96
|
-
if (_.isString(filters) && !filters.length) {
|
|
97
|
-
return emptyFilters
|
|
98
|
-
}
|
|
99
|
-
if (_.isArray(filters) && !filters.length) {
|
|
100
|
-
return emptyFilters
|
|
101
|
-
}
|
|
102
|
-
let typeormFilters: JsonMap = this.formatFiltersToTypeormQuery(filters);
|
|
103
|
-
return typeormFilters
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
getTypeormFieldsOptions(fields: string[] | string, primaryKeys: string[]): JsonMap {
|
|
107
|
-
if (typeof fields === "string") {
|
|
108
|
-
fields = (<string>fields).split(",").map((n) => { return n.trim(); });
|
|
109
|
-
}
|
|
110
|
-
if (!(fields && fields.length)) {
|
|
111
|
-
return {}
|
|
112
|
-
}
|
|
113
|
-
let projection: string = "";
|
|
114
|
-
(<string[]>fields).forEach((field) => {
|
|
115
|
-
if (field && primaryKeys.indexOf(field) < 0) {
|
|
116
|
-
projection += `${field},`
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
projection = projection.replace(/,$/g, "");
|
|
120
|
-
if (primaryKeys && primaryKeys.length){
|
|
121
|
-
if (projection){
|
|
122
|
-
projection = `${primaryKeys.join(",")},${projection}`;
|
|
123
|
-
}
|
|
124
|
-
else{
|
|
125
|
-
projection = primaryKeys.join(",");
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return {
|
|
129
|
-
$select: projection
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
getTypeormSortOptions(sort: string, defaultSort?: string): JsonMap {
|
|
134
|
-
let result: JsonMap = {};
|
|
135
|
-
if (!(sort && typeof sort === "string" && sort.length)) {
|
|
136
|
-
if (defaultSort){
|
|
137
|
-
sort = defaultSort;
|
|
138
|
-
}
|
|
139
|
-
else{
|
|
140
|
-
return result;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
let arraySort: string[] = sort.split(",").map((n) => { return n.trim(); });
|
|
144
|
-
let stringSort: string = "";
|
|
145
|
-
arraySort.forEach((n) => {
|
|
146
|
-
if (n) {
|
|
147
|
-
stringSort += `${n},`
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
stringSort = stringSort.replace(/,$/g, "");
|
|
151
|
-
if (stringSort) {
|
|
152
|
-
result = {
|
|
153
|
-
$orderby: stringSort
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
return result;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
getTypeormTopAndSkipOptions(top: number = 0, skip: number = 0): JsonMap {
|
|
160
|
-
let result: JsonMap = {};
|
|
161
|
-
if (top > 0) {
|
|
162
|
-
result.$top = top;
|
|
163
|
-
}
|
|
164
|
-
if (skip > 0) {
|
|
165
|
-
result.$skip = skip;
|
|
166
|
-
}
|
|
167
|
-
return result;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
async find(tableName: string, query: SteedosQueryOptions) {
|
|
171
|
-
await this.connect();
|
|
172
|
-
let entity = this._entities[tableName];
|
|
173
|
-
if (!entity) {
|
|
174
|
-
throw new Error(`${tableName} is not exist or not registered in the connect`);
|
|
175
|
-
}
|
|
176
|
-
let repository = this._client.getRepository(entity);
|
|
177
|
-
let primaryKeys: string[] = getPrimaryKeys(repository);
|
|
178
|
-
let filterQuery: JsonMap = this.getTypeormFilters(query.filters);
|
|
179
|
-
let projection: JsonMap = this.getTypeormFieldsOptions(query.fields, primaryKeys);
|
|
180
|
-
let sort: JsonMap = this.getTypeormSortOptions(query.sort);
|
|
181
|
-
let topAndSkip: JsonMap = this.getTypeormTopAndSkipOptions(query.top, query.skip);
|
|
182
|
-
const queryBuilder = repository.createQueryBuilder(tableName);
|
|
183
|
-
let queryOptions = Object.assign(filterQuery, projection, sort, topAndSkip);
|
|
184
|
-
let sqlOptions: SqlOptions = { alias: tableName, type: this.sqlLang, version: this.databaseVersion };
|
|
185
|
-
let result = await executeQuery(queryBuilder, queryOptions, sqlOptions);
|
|
186
|
-
return result.map((item:any)=>{
|
|
187
|
-
if (primaryKeys){
|
|
188
|
-
if (primaryKeys.length === 1){
|
|
189
|
-
let key = primaryKeys[0];
|
|
190
|
-
item['_id'] = item[key] ? item[key].toString() : "";
|
|
191
|
-
}
|
|
192
|
-
else if (primaryKeys.length > 1){
|
|
193
|
-
item['_ids'] = {};
|
|
194
|
-
primaryKeys.forEach((key) => {
|
|
195
|
-
item['_ids'][key] = item[key] ? item[key].toString() : "";
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
return item;
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
async count(tableName: string, query: SteedosQueryOptions) {
|
|
204
|
-
await this.connect();
|
|
205
|
-
let entity = this._entities[tableName];
|
|
206
|
-
if (!entity) {
|
|
207
|
-
throw new Error(`${tableName} is not exist or not registered in the connect`);
|
|
208
|
-
}
|
|
209
|
-
let filterQuery: JsonMap = this.getTypeormFilters(query.filters);
|
|
210
|
-
let repository = this._client.getRepository(entity);
|
|
211
|
-
const queryBuilder = repository.createQueryBuilder(tableName);
|
|
212
|
-
let result = await executeCountQuery(queryBuilder, filterQuery, { alias: tableName, type: this.sqlLang });
|
|
213
|
-
return result;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
async findOne(tableName: string, id: SteedosIDType, query?: SteedosQueryOptions) {
|
|
217
|
-
await this.connect();
|
|
218
|
-
let entity = this._entities[tableName];
|
|
219
|
-
if (!entity) {
|
|
220
|
-
throw new Error(`${tableName} is not exist or not registered in the connect`);
|
|
221
|
-
}
|
|
222
|
-
let repository = this._client.getRepository(entity);
|
|
223
|
-
let primaryKeys: string[] = getPrimaryKeys(repository);
|
|
224
|
-
let filterQuery: JsonMap = this.getTypeormFilters([[primaryKeys[0], "=", id]]);
|
|
225
|
-
let projection: JsonMap = this.getTypeormFieldsOptions(query ? query.fields : [], primaryKeys);
|
|
226
|
-
const queryBuilder = repository.createQueryBuilder(tableName);
|
|
227
|
-
// 这里不可以用repository.findOne,也不可以用repository.createQueryBuilder(tableName).select(...).where(...).getOne();
|
|
228
|
-
// 因为sqlserver/sqlite3不兼容
|
|
229
|
-
let result = await executeQuery(queryBuilder, Object.assign(filterQuery, projection), { alias: tableName, type: this.sqlLang });
|
|
230
|
-
if (result && result[0]){
|
|
231
|
-
// result[0]['_id'] = result[0][primaryKey] ? result[0][primaryKey].toString() : "";
|
|
232
|
-
if (primaryKeys){
|
|
233
|
-
if (primaryKeys.length === 1){
|
|
234
|
-
let key = primaryKeys[0];
|
|
235
|
-
result[0]['_id'] = result[0][key] ? result[0][key].toString() : "";
|
|
236
|
-
}
|
|
237
|
-
else if (primaryKeys.length > 1){
|
|
238
|
-
result[0]['_ids'] = {};
|
|
239
|
-
primaryKeys.forEach((key) => {
|
|
240
|
-
result[0]['_ids'][key] = result[0][key] ? result[0][key].toString() : "";
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
return result[0];
|
|
245
|
-
}
|
|
246
|
-
else{
|
|
247
|
-
return null;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
async run(sql: string, param?: any) {
|
|
252
|
-
const queryRunner: QueryRunner = await this.createQueryRunner();
|
|
253
|
-
try {
|
|
254
|
-
return queryRunner.query(sql, param);
|
|
255
|
-
} finally {
|
|
256
|
-
// release函数会根据不同的数据为类型执行不同的操作,比如close数据库
|
|
257
|
-
if (queryRunner !== this._queryRunner) {
|
|
258
|
-
await queryRunner.release();
|
|
259
|
-
}
|
|
260
|
-
if (this._client.driver.options.type === "sqljs") {
|
|
261
|
-
// driver instanceof SqljsDriver
|
|
262
|
-
// SqljsDriver is not export from typeorm,so we user driver.options.type to check the SqljsDriver instance
|
|
263
|
-
await this._client.driver.autoSave();
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
async insert(tableName: string, data: Dictionary<any>) {
|
|
269
|
-
await this.connect();
|
|
270
|
-
let entity = this._entities[tableName];
|
|
271
|
-
if (!entity) {
|
|
272
|
-
throw new Error(`${tableName} is not exist or not registered in the connect`);
|
|
273
|
-
}
|
|
274
|
-
let repository = this._client.getRepository(entity);
|
|
275
|
-
let result = await repository.insert(data);
|
|
276
|
-
if (result.identifiers && result.identifiers.length) {
|
|
277
|
-
let primaryKeys: string[] = getPrimaryKeys(repository);
|
|
278
|
-
if (primaryKeys && primaryKeys.length === 1) {
|
|
279
|
-
let id = primaryKeys[0] && result.identifiers[0][primaryKeys[0]];
|
|
280
|
-
if (id) {
|
|
281
|
-
return await this.findOne(tableName, id);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
async update(tableName: string, id: SteedosIDType, data: Dictionary<any>) {
|
|
288
|
-
await this.connect();
|
|
289
|
-
let entity = this._entities[tableName];
|
|
290
|
-
if (!entity) {
|
|
291
|
-
throw new Error(`${tableName} is not exist or not registered in the connect`);
|
|
292
|
-
}
|
|
293
|
-
let fields: any[] = Object.keys(data);
|
|
294
|
-
if (!fields.length) {
|
|
295
|
-
throw new Error("the params 'data' must not be empty");
|
|
296
|
-
}
|
|
297
|
-
let repository = this._client.getRepository(entity);
|
|
298
|
-
await repository.update(id, data);
|
|
299
|
-
return await this.findOne(tableName, id);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
async updateOne(tableName: string, id: SteedosIDType, data: Dictionary<any>) {
|
|
303
|
-
await this.connect();
|
|
304
|
-
let entity = this._entities[tableName];
|
|
305
|
-
if (!entity) {
|
|
306
|
-
throw new Error(`${tableName} is not exist or not registered in the connect`);
|
|
307
|
-
}
|
|
308
|
-
let fields: any[] = Object.keys(data);
|
|
309
|
-
if (!fields.length) {
|
|
310
|
-
throw new Error("the params 'data' must not be empty");
|
|
311
|
-
}
|
|
312
|
-
let repository = this._client.getRepository(entity);
|
|
313
|
-
await repository.update(id, data);
|
|
314
|
-
return await this.findOne(tableName, id);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
async delete(tableName: string, id: SteedosIDType) {
|
|
318
|
-
await this.connect();
|
|
319
|
-
let entity = this._entities[tableName];
|
|
320
|
-
if (!entity) {
|
|
321
|
-
throw new Error(`${tableName} is not exist or not registered in the connect`);
|
|
322
|
-
}
|
|
323
|
-
let repository = this._client.getRepository(entity);
|
|
324
|
-
await repository.delete(id);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
async directFind(tableName: string, query: SteedosQueryOptions) {
|
|
328
|
-
return this.find(tableName, query)
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
async directInsert(tableName: string, data: Dictionary<any>) {
|
|
332
|
-
return this.insert(tableName, data)
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
async directUpdate(tableName: string, id: SteedosIDType, data: Dictionary<any>) {
|
|
336
|
-
return this.update(tableName, id, data)
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
async directDelete(tableName: string, id: SteedosIDType) {
|
|
340
|
-
return this.delete(tableName, id)
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
async getDatabaseVersion() {
|
|
344
|
-
// 各个driver层可以通过重写该函数来设置databaseVersion值
|
|
345
|
-
return "";
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
async dropEntities() {
|
|
349
|
-
let objects = {};
|
|
350
|
-
this._entities = this.getEntities(objects);
|
|
351
|
-
await this.connect();
|
|
352
|
-
await this._client.synchronize(true);
|
|
353
|
-
await this.close();
|
|
354
|
-
this._entities = null;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
registerEntities(objects: Dictionary<SteedosObjectType>) {
|
|
358
|
-
// if (!this._entities) {
|
|
359
|
-
// this._entities = this.getEntities(objects);
|
|
360
|
-
// }
|
|
361
|
-
this._entities = this.getEntities(objects);
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
async dropTables() {
|
|
365
|
-
await this.dropEntities();
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
async createTables(objects: Dictionary<SteedosObjectType>) {
|
|
369
|
-
await this.init(objects);
|
|
370
|
-
await this._client.synchronize();
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
async init(objects: Dictionary<SteedosObjectType>) {
|
|
374
|
-
this.registerEntities(objects);
|
|
375
|
-
await this.connect(true);
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
abstract getEntities(objects: Dictionary<SteedosObjectType>): Dictionary<EntitySchema>;
|
|
379
|
-
}
|
package/src/typeorm/index.ts
DELETED
package/src/typeorm/util.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { EntitySchema, ColumnType, EntitySchemaColumnOptions, EntitySchemaRelationOptions, Repository, DatabaseType } from "typeorm";
|
|
2
|
-
import { Dictionary } from "@salesforce/ts-types";
|
|
3
|
-
import { SteedosObjectType, SteedosFieldType } from "../types";
|
|
4
|
-
import { SteedosFieldDBType } from "../driver";
|
|
5
|
-
|
|
6
|
-
export interface EntitySchemaColumnDictionary {
|
|
7
|
-
[x: string]: EntitySchemaColumnOptions;
|
|
8
|
-
}
|
|
9
|
-
export interface EntitySchemaRelationDictionary {
|
|
10
|
-
[x: string]: EntitySchemaRelationOptions;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type RelationType = "one-to-one" | "one-to-many" | "many-to-one" | "many-to-many";
|
|
14
|
-
|
|
15
|
-
export function getTableColumnType(field: SteedosFieldType, databaseType: DatabaseType): ColumnType {
|
|
16
|
-
let columnType: SteedosFieldDBType = field.fieldDBType;
|
|
17
|
-
switch (columnType) {
|
|
18
|
-
case SteedosFieldDBType.varchar:
|
|
19
|
-
return String;
|
|
20
|
-
case SteedosFieldDBType.text:
|
|
21
|
-
if (databaseType === "oracle"){
|
|
22
|
-
return "blob";
|
|
23
|
-
}
|
|
24
|
-
return "text";
|
|
25
|
-
case SteedosFieldDBType.number:
|
|
26
|
-
let scale = field.scale === undefined ? 0 : field.scale;
|
|
27
|
-
if (scale === 0){
|
|
28
|
-
return "int";
|
|
29
|
-
}
|
|
30
|
-
return "decimal";
|
|
31
|
-
case SteedosFieldDBType.dateTime:
|
|
32
|
-
return Date;
|
|
33
|
-
case SteedosFieldDBType.date:
|
|
34
|
-
if (databaseType === "postgres") {
|
|
35
|
-
// postgres数据库日期类型返回的最终是字符串,类似于:2019-04-30
|
|
36
|
-
return "date";
|
|
37
|
-
}
|
|
38
|
-
return Date;
|
|
39
|
-
case SteedosFieldDBType.boolean:
|
|
40
|
-
return Boolean;
|
|
41
|
-
}
|
|
42
|
-
return columnType || String;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export function getTableColumns(fields: Dictionary<SteedosFieldType>, object: SteedosObjectType, databaseType: DatabaseType): EntitySchemaColumnDictionary {
|
|
46
|
-
let columns: EntitySchemaColumnDictionary = {};
|
|
47
|
-
let primaryColumnCount = 0;
|
|
48
|
-
for (let fieldName in fields) {
|
|
49
|
-
let field = fields[fieldName];
|
|
50
|
-
let fieldType: ColumnType = getTableColumnType(field, databaseType);
|
|
51
|
-
if (!fieldType){
|
|
52
|
-
continue;
|
|
53
|
-
}
|
|
54
|
-
let nullable = field.required ? false : true;
|
|
55
|
-
if (field.primary){
|
|
56
|
-
nullable = false;
|
|
57
|
-
primaryColumnCount++;
|
|
58
|
-
}
|
|
59
|
-
columns[fieldName] = {
|
|
60
|
-
type: fieldType,
|
|
61
|
-
nullable: nullable,
|
|
62
|
-
primary: field.primary,
|
|
63
|
-
generated: field.generated,
|
|
64
|
-
precision: field.precision ? field.precision : (field.scale ? 18 : undefined),
|
|
65
|
-
scale: field.scale
|
|
66
|
-
};
|
|
67
|
-
if(field.column_name){
|
|
68
|
-
(columns as any)[fieldName]["name"] = field.column_name
|
|
69
|
-
}
|
|
70
|
-
// 服务端增加_id字段统一代理主键字段处理查询
|
|
71
|
-
if (field.primary) {
|
|
72
|
-
(columns as any)['_id'] = {
|
|
73
|
-
...columns[fieldName],
|
|
74
|
-
name: field.column_name || fieldName,
|
|
75
|
-
generated: false,
|
|
76
|
-
primary: false,
|
|
77
|
-
nullable: true,
|
|
78
|
-
insert: false,
|
|
79
|
-
update: false
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
if (!primaryColumnCount){
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
return columns;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export function getEntity(object: SteedosObjectType, databaseType: DatabaseType): EntitySchema {
|
|
90
|
-
let tableName = object.table_name;
|
|
91
|
-
let fields = object.fields;
|
|
92
|
-
let columns: EntitySchemaColumnDictionary = getTableColumns(fields, object, databaseType);
|
|
93
|
-
if (!columns){
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
// typeorm支持对象类型:"regular" | "view" | "junction" | "closure" | "closure-junction" | "entity-child";
|
|
97
|
-
let type: any = "regular";
|
|
98
|
-
if (object.is_view){
|
|
99
|
-
type = "view";
|
|
100
|
-
}
|
|
101
|
-
return new EntitySchema({
|
|
102
|
-
name: tableName,
|
|
103
|
-
tableName: tableName,
|
|
104
|
-
columns: columns,
|
|
105
|
-
type: type
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export function getEntities(objects: Dictionary<SteedosObjectType>, databaseType: DatabaseType): Dictionary<EntitySchema> {
|
|
110
|
-
let entities: Dictionary<EntitySchema> = {};
|
|
111
|
-
let primaryColumnEmptyErrorTables = [];
|
|
112
|
-
let primaryColumnMultiErrorTables = [];
|
|
113
|
-
for (let name in objects) {
|
|
114
|
-
let object = objects[name];
|
|
115
|
-
let primaryKeys = object.idFieldNames;
|
|
116
|
-
if (primaryKeys.length === 1) {
|
|
117
|
-
let entitySchema = getEntity(object, databaseType);
|
|
118
|
-
if (entitySchema) {
|
|
119
|
-
entities[object.table_name] = entitySchema;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
else if (primaryKeys.length > 1) {
|
|
123
|
-
primaryColumnMultiErrorTables.push(object.table_name);
|
|
124
|
-
}
|
|
125
|
-
else{
|
|
126
|
-
primaryColumnEmptyErrorTables.push(object.table_name);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
if (primaryColumnEmptyErrorTables.length) {
|
|
130
|
-
console.error(`These tables: "${primaryColumnEmptyErrorTables.join(",")}" does not have a primary column. Primary column is not required in DB, but is required in yml object file.`);
|
|
131
|
-
}
|
|
132
|
-
if (primaryColumnMultiErrorTables.length) {
|
|
133
|
-
console.error(`These tables: "${primaryColumnMultiErrorTables.join(",")}" have multi primary columns. it is not supported yet.`);
|
|
134
|
-
}
|
|
135
|
-
return entities;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function getPrimaryKeys(repository: Repository<any>): Array<string> {
|
|
139
|
-
let primaryColumns: any = repository.metadata.primaryColumns;
|
|
140
|
-
let primaryKeys: [] = [];
|
|
141
|
-
if (primaryColumns && primaryColumns.length) {
|
|
142
|
-
primaryKeys = primaryColumns.map((item)=>{
|
|
143
|
-
return item.propertyPath;
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
return primaryKeys;
|
|
147
|
-
}
|