midway-fatcms 0.0.1-beta.26 → 0.0.1-beta.29

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.
Files changed (188) hide show
  1. package/dist/config/config.default.js +6 -4
  2. package/dist/controller/home.controller.js +2 -1
  3. package/dist/controller/manage/CrudStandardDesignApi.js +8 -8
  4. package/dist/controller/render/AppRenderController.js +2 -1
  5. package/dist/middleware/global.middleware.js +22 -9
  6. package/dist/models/AsyncTaskModel.d.ts +2 -0
  7. package/dist/models/AsyncTaskModel.js +3 -1
  8. package/dist/models/RedisKeys.d.ts +2 -0
  9. package/dist/models/RedisKeys.js +2 -0
  10. package/dist/models/bizmodels.d.ts +1 -0
  11. package/dist/service/UserSessionService.d.ts +21 -0
  12. package/dist/service/UserSessionService.js +71 -1
  13. package/dist/service/WorkbenchService.d.ts +32 -0
  14. package/dist/service/WorkbenchService.js +54 -11
  15. package/dist/service/anyapi/AnyApiSandboxService.js +12 -12
  16. package/dist/service/asyncTask/AsyncTaskRunnerService.d.ts +14 -1
  17. package/dist/service/asyncTask/AsyncTaskRunnerService.js +70 -25
  18. package/dist/service/asyncTask/handler/ExportExcelAsyncTaskHandler.js +1 -1
  19. package/dist/service/asyncTask/handler/ExportExcelByInnerHttpHandler.js +11 -2
  20. package/dist/service/base/RedisCacheService.d.ts +7 -0
  21. package/dist/service/base/RedisCacheService.js +7 -0
  22. package/dist/service/crudstd/CrudStdService.d.ts +0 -6
  23. package/dist/service/crudstd/CrudStdService.js +13 -33
  24. package/package.json +1 -5
  25. package/src/config/config.default.ts +0 -207
  26. package/src/config/seed/aeskey.txt +0 -1
  27. package/src/config/utils.ts +0 -22
  28. package/src/configuration.ts +0 -109
  29. package/src/controller/base/BaseApiController.ts +0 -170
  30. package/src/controller/gateway/AnyApiGatewayController.ts +0 -33
  31. package/src/controller/gateway/AsyncTaskController.ts +0 -157
  32. package/src/controller/gateway/CrudMtdGatewayController.ts +0 -111
  33. package/src/controller/gateway/CrudStdGatewayController.ts +0 -101
  34. package/src/controller/gateway/DocGatewayController.ts +0 -173
  35. package/src/controller/gateway/FileController.ts +0 -109
  36. package/src/controller/gateway/ProxyApiGatewayController.ts +0 -47
  37. package/src/controller/gateway/PublicApiController.ts +0 -142
  38. package/src/controller/gateway/StaticController.ts +0 -298
  39. package/src/controller/helpers.controller.ts +0 -161
  40. package/src/controller/home.controller.ts +0 -64
  41. package/src/controller/manage/AnyApiMangeApi.ts +0 -66
  42. package/src/controller/manage/AppLogMangeApi.ts +0 -53
  43. package/src/controller/manage/AppMangeApi.ts +0 -53
  44. package/src/controller/manage/AppPageMangeApi.ts +0 -52
  45. package/src/controller/manage/AppSchemaHistoryApi.ts +0 -49
  46. package/src/controller/manage/CrudMethodsMangeApi.ts +0 -49
  47. package/src/controller/manage/CrudStandardDesignApi.ts +0 -346
  48. package/src/controller/manage/DataDictManageApi.ts +0 -78
  49. package/src/controller/manage/DeployManageApi.ts +0 -175
  50. package/src/controller/manage/DocLibManageApi.ts +0 -69
  51. package/src/controller/manage/DocManageApi.ts +0 -99
  52. package/src/controller/manage/FileManageApi.ts +0 -45
  53. package/src/controller/manage/LowCodeTplManageApi.ts +0 -52
  54. package/src/controller/manage/MenuManageApi.ts +0 -58
  55. package/src/controller/manage/ProxyApiMangeApi.ts +0 -52
  56. package/src/controller/manage/SuperAdminManageApi.ts +0 -139
  57. package/src/controller/manage/SysConfigMangeApi.ts +0 -95
  58. package/src/controller/manage/SystemInfoManageApi.ts +0 -53
  59. package/src/controller/manage/UserAccountManageApi.ts +0 -94
  60. package/src/controller/manage/WorkbenchMangeApi.ts +0 -72
  61. package/src/controller/myinfo/AuthController.ts +0 -108
  62. package/src/controller/myinfo/MyInfoController.ts +0 -32
  63. package/src/controller/render/AppRenderController.ts +0 -79
  64. package/src/controller/test.controller.ts +0 -37
  65. package/src/filter/default.filter.ts +0 -13
  66. package/src/filter/notfound.filter.ts +0 -10
  67. package/src/index.ts +0 -106
  68. package/src/interface.ts +0 -31
  69. package/src/libs/crud-pro/CrudPro.ts +0 -165
  70. package/src/libs/crud-pro/defaultConfigs.ts +0 -15
  71. package/src/libs/crud-pro/exceptions.ts +0 -124
  72. package/src/libs/crud-pro/interfaces.ts +0 -190
  73. package/src/libs/crud-pro/models/ExecuteContext.ts +0 -120
  74. package/src/libs/crud-pro/models/ExecuteContextFunc.ts +0 -96
  75. package/src/libs/crud-pro/models/FuncContext.ts +0 -21
  76. package/src/libs/crud-pro/models/RequestCfgModel.ts +0 -141
  77. package/src/libs/crud-pro/models/RequestModel.ts +0 -141
  78. package/src/libs/crud-pro/models/ResModel.ts +0 -19
  79. package/src/libs/crud-pro/models/ServiceHub.ts +0 -32
  80. package/src/libs/crud-pro/models/SqlCfgModel.ts +0 -52
  81. package/src/libs/crud-pro/models/SqlSegArg.ts +0 -13
  82. package/src/libs/crud-pro/models/Transaction.ts +0 -73
  83. package/src/libs/crud-pro/models/TransactionMySQL.ts +0 -79
  84. package/src/libs/crud-pro/models/TransactionPostgres.ts +0 -91
  85. package/src/libs/crud-pro/models/TransactionSqlServer.ts +0 -102
  86. package/src/libs/crud-pro/models/keys.ts +0 -159
  87. package/src/libs/crud-pro/services/CrudProCachedCfgService.ts +0 -83
  88. package/src/libs/crud-pro/services/CrudProExecuteFuncService.ts +0 -128
  89. package/src/libs/crud-pro/services/CrudProExecuteSqlService.ts +0 -262
  90. package/src/libs/crud-pro/services/CrudProFieldUpdateService.ts +0 -60
  91. package/src/libs/crud-pro/services/CrudProFieldValidateService.ts +0 -180
  92. package/src/libs/crud-pro/services/CrudProGenSqlCondition.ts +0 -354
  93. package/src/libs/crud-pro/services/CrudProGenSqlService.ts +0 -185
  94. package/src/libs/crud-pro/services/CrudProOriginToExecuteSql.ts +0 -393
  95. package/src/libs/crud-pro/services/CrudProServiceBase.ts +0 -94
  96. package/src/libs/crud-pro/services/CrudProTableMetaService.ts +0 -86
  97. package/src/libs/crud-pro/services/CurdProServiceHub.ts +0 -92
  98. package/src/libs/crud-pro/sql.txt +0 -120
  99. package/src/libs/crud-pro/utils/CompareUtils.ts +0 -23
  100. package/src/libs/crud-pro/utils/DatabaseName.ts +0 -60
  101. package/src/libs/crud-pro/utils/DateTimeUtils.ts +0 -20
  102. package/src/libs/crud-pro/utils/MemoryRefreshCache.ts +0 -64
  103. package/src/libs/crud-pro/utils/MessageParseUtils.ts +0 -33
  104. package/src/libs/crud-pro/utils/MixinUtils.ts +0 -285
  105. package/src/libs/crud-pro/utils/ModelUtils.ts +0 -55
  106. package/src/libs/crud-pro/utils/MultiKeyMap.ts +0 -72
  107. package/src/libs/crud-pro/utils/SqlFuncUtils.ts +0 -29
  108. package/src/libs/crud-pro/utils/TypeUtils.ts +0 -188
  109. package/src/libs/crud-pro/utils/ValidateUtils.ts +0 -165
  110. package/src/libs/crud-pro/utils/pool/MySQLUtils.ts +0 -20
  111. package/src/libs/crud-pro/utils/pool/PostgresUtils.ts +0 -22
  112. package/src/libs/crud-pro/utils/pool/SqlServerUtils.ts +0 -22
  113. package/src/libs/crud-pro/utils/sqlConvert/convertColumnName.ts +0 -26
  114. package/src/libs/crud-pro/utils/sqlConvert/convertMix.ts +0 -26
  115. package/src/libs/crud-pro/utils/sqlConvert/convertMsSql.ts +0 -11
  116. package/src/libs/crud-pro/utils/sqlConvert/convertPgSql.ts +0 -11
  117. package/src/libs/crud-pro/utils/sqlConvert/convertPgType.ts +0 -129
  118. package/src/libs/global-config/global-config.ts +0 -78
  119. package/src/libs/utils/common-dto.ts +0 -52
  120. package/src/libs/utils/crypto-utils.ts +0 -50
  121. package/src/libs/utils/errorToString.ts +0 -61
  122. package/src/libs/utils/fatcms-request.ts +0 -103
  123. package/src/libs/utils/functions.ts +0 -73
  124. package/src/libs/utils/ordernum-utils.ts +0 -14
  125. package/src/libs/utils/parseConfig.ts +0 -54
  126. package/src/libs/utils/parseCreateSql.ts +0 -91
  127. package/src/libs/utils/render-utils.ts +0 -184
  128. package/src/middleware/forbidden.middleware.ts +0 -52
  129. package/src/middleware/global.middleware.ts +0 -280
  130. package/src/middleware/permission.middleware.ts +0 -80
  131. package/src/middleware/tx.middleware.ts +0 -30
  132. package/src/models/AsyncTaskModel.ts +0 -82
  133. package/src/models/RedisKeys.ts +0 -13
  134. package/src/models/SystemEntities.ts +0 -115
  135. package/src/models/SystemPerm.ts +0 -105
  136. package/src/models/SystemTables.ts +0 -27
  137. package/src/models/bizmodels.ts +0 -120
  138. package/src/models/contextLogger.ts +0 -132
  139. package/src/models/devops.ts +0 -17
  140. package/src/models/userSession.ts +0 -216
  141. package/src/schedule/anonymousContext.ts +0 -73
  142. package/src/schedule/index.ts +0 -12
  143. package/src/schedule/runSchedule.ts +0 -82
  144. package/src/schedule/scheduleNames.ts +0 -21
  145. package/src/service/AuthService.ts +0 -272
  146. package/src/service/EnumInfoService.ts +0 -130
  147. package/src/service/FileCenterService.ts +0 -395
  148. package/src/service/SysConfigService.ts +0 -37
  149. package/src/service/UserAccountService.ts +0 -107
  150. package/src/service/UserSessionService.ts +0 -78
  151. package/src/service/VisitStatService.ts +0 -166
  152. package/src/service/WorkbenchService.ts +0 -165
  153. package/src/service/anyapi/AnyApiSandboxService.ts +0 -121
  154. package/src/service/anyapi/AnyApiService.ts +0 -186
  155. package/src/service/asyncTask/AsyncTaskRunnerService.ts +0 -266
  156. package/src/service/asyncTask/AsyncTaskService.ts +0 -21
  157. package/src/service/asyncTask/handler/ExcelInfoModel.ts +0 -11
  158. package/src/service/asyncTask/handler/ExportExcelAsyncTaskHandler.ts +0 -245
  159. package/src/service/asyncTask/handler/ExportExcelByInnerHttpHandler.ts +0 -147
  160. package/src/service/asyncTask/handler/ExportExcelByStdCrudHandler.ts +0 -138
  161. package/src/service/base/ApiBaseService.ts +0 -42
  162. package/src/service/base/ApiRateLimiter.ts +0 -59
  163. package/src/service/base/BaseService.ts +0 -100
  164. package/src/service/base/RedisCacheService.ts +0 -38
  165. package/src/service/crudstd/CrudStdActionService.ts +0 -27
  166. package/src/service/crudstd/CrudStdConstant.ts +0 -62
  167. package/src/service/crudstd/CrudStdRelationService.ts +0 -78
  168. package/src/service/crudstd/CrudStdService.ts +0 -283
  169. package/src/service/curd/CrudProQuick.ts +0 -131
  170. package/src/service/curd/CurdMixByAccountService.ts +0 -90
  171. package/src/service/curd/CurdMixByDictService.ts +0 -114
  172. package/src/service/curd/CurdMixByLinkToCustomService.ts +0 -219
  173. package/src/service/curd/CurdMixBySysConfigService.ts +0 -78
  174. package/src/service/curd/CurdMixByWorkbenchService.ts +0 -71
  175. package/src/service/curd/CurdMixService.ts +0 -97
  176. package/src/service/curd/CurdMixUtils.ts +0 -311
  177. package/src/service/curd/CurdProService.ts +0 -229
  178. package/src/service/curd/fixCfgModel.ts +0 -139
  179. package/src/service/proxyapi/ProxyApiLoadService.ts +0 -174
  180. package/src/service/proxyapi/ProxyApiService.ts +0 -262
  181. package/src/service/proxyapi/ProxyApiUtils.ts +0 -32
  182. package/src/service/proxyapi/RouteHandler.ts +0 -8
  183. package/src/service/proxyapi/RouteTrie.ts +0 -74
  184. package/src/service/proxyapi/WeightedRandom.ts +0 -37
  185. package/src/service/proxyapi/WeightedRoundRobin.ts +0 -44
  186. package/src/views/404_app.html +0 -31
  187. package/src/views/404_workbench.html +0 -34
  188. package/src/views/static/favicon.ico +0 -0
@@ -1,128 +0,0 @@
1
- import { CrudProServiceBase } from './CrudProServiceBase';
2
- import { IFuncCfgModel, IFunctionParam } from '../interfaces';
3
- import { FuncContext } from '../models/FuncContext';
4
- import { TypeUtils } from '../utils/TypeUtils';
5
- import { MixinUtils } from '../utils/MixinUtils';
6
- import * as _ from 'lodash';
7
- import { CommonException, Exceptions } from '../exceptions';
8
- import { KeysOfFunCtx } from '../models/keys';
9
-
10
- const { isNil, isNumeric } = TypeUtils;
11
- const { isEmpty } = MixinUtils;
12
-
13
- function isNotNil(obj: any): boolean {
14
- return !isNil(obj);
15
- }
16
-
17
- class CrudProExecuteFuncService extends CrudProServiceBase {
18
- executeFuncCfg(funCfg: IFuncCfgModel, funcContext: FuncContext) {
19
- if (isNil(funCfg)) {
20
- return null;
21
- }
22
-
23
- if (funCfg.___PLAIN_OBJECT___ === true) {
24
- return funCfg;
25
- }
26
-
27
- if (isNotNil(funCfg.const)) {
28
- return funCfg.const;
29
- }
30
- if (isNotNil(funCfg.constString)) {
31
- return '' + funCfg.constString;
32
- }
33
- if (isNotNil(funCfg.constNumber) && isNumeric(funCfg.constNumber)) {
34
- return Number(funCfg.constNumber);
35
- }
36
- if (isNotNil(funCfg.constBool)) {
37
- return Boolean(funCfg.constBool);
38
- }
39
-
40
- if (isNotNil(funCfg.context)) {
41
- return this.pickContextValue(funCfg.context, funcContext);
42
- }
43
- if (isNotNil(funCfg.contextAsString)) {
44
- return '' + this.pickContextValue(funCfg.contextAsString, funcContext);
45
- }
46
- if (isNotNil(funCfg.contextAsNumber)) {
47
- return Number(this.pickContextValue(funCfg.contextAsNumber, funcContext));
48
- }
49
- if (isNotNil(funCfg.contextAsBool)) {
50
- return Boolean(this.pickContextValue(funCfg.contextAsBool, funcContext));
51
- }
52
-
53
- if (isNotNil(funCfg.functionName)) {
54
- const func = this.getExecuteFunction(funCfg.functionName);
55
- if (!func) {
56
- throw new CommonException(Exceptions.RUN_FUNCTION_NOT_FOUND, funCfg.functionName);
57
- }
58
- const functionParamValues = this.getFunctionParamValues(funCfg.functionParams, funcContext);
59
- return func.apply(funcContext, functionParamValues);
60
- }
61
- return null;
62
- }
63
-
64
- private pickContextValue(pickString: string, funcCtx: FuncContext) {
65
- if (!pickString) {
66
- return null;
67
- }
68
-
69
- if (pickString === KeysOfFunCtx.FUNC_CTX) {
70
- return funcCtx;
71
- }
72
-
73
- if (pickString === KeysOfFunCtx.CURRENT_VALUE) {
74
- return funcCtx.currentValue;
75
- }
76
-
77
- // 例如:accountId, 访问此变量,用户必须登录。
78
- if (pickString.startsWith(KeysOfFunCtx.VISITOR_ATTR)) {
79
- const reqModel = funcCtx?.reqModel;
80
- const attrValue = _.get(reqModel, pickString); // visitor.nickName
81
- if (isNil(attrValue)) {
82
- throw new CommonException(Exceptions.RUN_PICK_ERR_VISITOR_NULL, '获取用户ID失败, 可能用户没有登陆');
83
- }
84
- return attrValue;
85
- }
86
-
87
- // 例如:accountId, 访问此变量,用户必须登录。
88
- if (pickString === KeysOfFunCtx.VISITOR) {
89
- const visitor = funcCtx?.reqModel?.visitor;
90
- if (isNil(visitor)) {
91
- throw new CommonException(Exceptions.RUN_PICK_ERR_VISITOR_NULL, '获取用户对象失败, 可能用户没有登陆');
92
- }
93
- return visitor;
94
- }
95
-
96
- // 例如: req.data.name
97
- if (pickString.startsWith(KeysOfFunCtx.REQUEST)) {
98
- const reqExpress = pickString.substring(KeysOfFunCtx.REQUEST.length);
99
- return _.get(funcCtx.reqModel, reqExpress);
100
- }
101
-
102
- // 例如: res.total_count
103
- if (pickString.startsWith(KeysOfFunCtx.RESULT)) {
104
- const reqExpress = pickString.substring(KeysOfFunCtx.RESULT.length);
105
- return _.get(funcCtx.resModel, reqExpress);
106
- }
107
-
108
- return null;
109
- }
110
-
111
- private getFunctionParamValues(functionParams: IFunctionParam[], funcContext: FuncContext): any[] {
112
- if (isNil(functionParams) || isEmpty(functionParams)) {
113
- return [];
114
- }
115
- return functionParams.map(p => {
116
- if (typeof p === 'undefined' || p === null) {
117
- return null;
118
- }
119
-
120
- if (typeof p === 'string' || typeof p === 'number' || typeof p === 'boolean') {
121
- return p;
122
- }
123
- return this.executeFuncCfg(p, funcContext);
124
- });
125
- }
126
- }
127
-
128
- export { CrudProExecuteFuncService };
@@ -1,262 +0,0 @@
1
- import * as _ from 'lodash';
2
- import { PoolClient } from 'pg';
3
- import { CrudProServiceBase } from './CrudProServiceBase';
4
- import { SqlCfgModel } from '../models/SqlCfgModel';
5
- import { KeyOfCrudTypes, KeysOfCustomSQL, KeysOfSqlResPicker, SqlDbType } from '../models/keys';
6
- import { CommonException, Exceptions } from '../exceptions';
7
- import { pickAndConvertRowsByMix } from '../utils/sqlConvert/convertMix';
8
- import { replaceQuestionMarks } from '../utils/sqlConvert/convertPgSql';
9
- import { replaceQuestionMarksForMssql } from '../utils/sqlConvert/convertMsSql';
10
- import { ModelUtils } from '../utils/ModelUtils';
11
- import { MixinUtils } from '../utils/MixinUtils';
12
- import { FuncContext } from '../models/FuncContext';
13
- import { ExecuteContext } from '../models/ExecuteContext';
14
-
15
- const { checkFuncCfgValid } = ModelUtils;
16
- const { isEmpty } = MixinUtils;
17
-
18
- class CrudProExecuteSqlService extends CrudProServiceBase {
19
- async executeSqlCfgModels(): Promise<void> {
20
- const exeCtx = this.getExecuteContext();
21
- const sqlCfgModels = exeCtx.getSqlCfgModels();
22
- for (let i = 0; i < sqlCfgModels.length; i++) {
23
- const sqlCfgModel = sqlCfgModels[i];
24
- const checkResult = this.executeSqlCfgModelPreCheck(sqlCfgModel);
25
- if (checkResult) {
26
- await this.executeSqlCfgModel(sqlCfgModel);
27
- }
28
- }
29
- }
30
-
31
- private async executeSqlCfgModel(sqlCfgModel: SqlCfgModel) {
32
- this.logger.info('executeSqlCfgModel1', sqlCfgModel.executeSql, sqlCfgModel.executeSqlArgs);
33
-
34
- const exeCtx = this.getExecuteContext();
35
- const connection = await this.getTxConnectionBySqlCfg(sqlCfgModel);
36
- const executeSqlArgs = this.handleExecuteSqlArgsByResModel(exeCtx, sqlCfgModel.executeSqlArgs || []);
37
-
38
- let queryRes: any;
39
-
40
- if (sqlCfgModel.sqlDbType === SqlDbType.postgres) {
41
- // import { PoolClient } from 'pg';
42
-
43
- const pgClient: PoolClient = connection as any;
44
- const pgSql = replaceQuestionMarks(sqlCfgModel.executeSql);
45
- this.logger.debug('[CrudProExecuteSqlService]executeSqlCfgModel_postgres', pgSql, executeSqlArgs, sqlCfgModel.resPicker);
46
- queryRes = await pgClient.query({
47
- text: pgSql,
48
- values: executeSqlArgs || [],
49
- });
50
- } else if (sqlCfgModel.sqlDbType === SqlDbType.sqlserver) {
51
- // SQLServer
52
-
53
- const mssql = replaceQuestionMarksForMssql(sqlCfgModel.executeSql);
54
- this.logger.debug('[CrudProExecuteSqlService]executeSqlCfgModel_sqlserver', mssql, executeSqlArgs, sqlCfgModel.resPicker);
55
- queryRes = await connection.query(mssql, executeSqlArgs);
56
- } else {
57
- // MYSQL
58
-
59
- this.logger.debug('[CrudProExecuteSqlService]executeSqlCfgModel_mysql', sqlCfgModel.executeSql, executeSqlArgs, sqlCfgModel.resPicker);
60
- queryRes = await connection.query(sqlCfgModel.executeSql, executeSqlArgs);
61
- }
62
-
63
- const sqlRes = pickAndConvertRowsByMix(queryRes, sqlCfgModel.sqlDbType);
64
- const resObject = this.toQueryResByResPicker(sqlRes, queryRes, sqlCfgModel);
65
- exeCtx.setResModelItem(sqlCfgModel.resName, resObject);
66
- }
67
-
68
- private handleExecuteSqlArgsByResModel(exeCtx: ExecuteContext, executeSqlArgs: any[]): any[] {
69
- return executeSqlArgs.map(arg => {
70
- if (arg && typeof arg === 'object' && typeof arg.___GENERATE_GET_RES_ATTR___ === 'string') {
71
- const word: string = arg.___GENERATE_GET_RES_ATTR___;
72
- if (word.startsWith(KeysOfCustomSQL.SQL_PICK_RES_AS_NUMBER)) {
73
- const resName = MixinUtils.removeStringPrefix(word, KeysOfCustomSQL.SQL_PICK_RES_AS_NUMBER);
74
- this.logger.info('handleExecuteSqlArgsByResModel resName', resName);
75
- this.logger.debug('handleExecuteSqlArgsByResModel getResModel', exeCtx.getResModel());
76
- return Number(exeCtx.getResModelItemLodash(resName) || 0);
77
- }
78
- if (word.startsWith(KeysOfCustomSQL.SQL_PICK_RES_AS_STRING)) {
79
- const resName = MixinUtils.removeStringPrefix(word, KeysOfCustomSQL.SQL_PICK_RES_AS_STRING);
80
- return String(exeCtx.getResModelItemLodash(resName) || '');
81
- }
82
- }
83
- return arg;
84
- });
85
- }
86
-
87
- private executeSqlCfgModelPreCheck(sqlCfgModel: SqlCfgModel): boolean {
88
- const crudType = sqlCfgModel.getCrudType();
89
-
90
- if (KeyOfCrudTypes.EMPTY_SQL === crudType) {
91
- // 允许空SQL,没有需要执行的正常。
92
- return false;
93
- }
94
-
95
- if (KeyOfCrudTypes.NOT_CRUD === crudType) {
96
- // 只支持增删改查,其他语句不支持。
97
- throw new CommonException(Exceptions.CFG_NOT_SUPPORT_THE_SQL, sqlCfgModel.executeSql);
98
- }
99
-
100
- // 此SQL不需要执行
101
- if (!this.isNeedExecute(sqlCfgModel)) {
102
- return false;
103
- }
104
-
105
- // 执行之前,根据配置校验一下此SQL是否可以执行
106
- this.assertByValidate(sqlCfgModel);
107
-
108
- return true;
109
- }
110
-
111
- private assertByValidate(sqlCfgModel: SqlCfgModel) {
112
- const exeCtx = this.getExecuteContext();
113
- const funcCfg = sqlCfgModel.validate;
114
- if (!funcCfg) {
115
- return; // 没有配置。
116
- }
117
-
118
- if (!checkFuncCfgValid(funcCfg)) {
119
- throw new CommonException(Exceptions.CFG_SQL_CFG_EXECUTE_VALIDATE_EMPTY, {
120
- funcCfg: funcCfg,
121
- originSql: sqlCfgModel.originSql,
122
- });
123
- }
124
-
125
- const errMsg = funcCfg.message || '执行前校验失败';
126
-
127
- let result;
128
- try {
129
- const exeFunCtx = new FuncContext();
130
-
131
- exeFunCtx.currentValue = null;
132
- exeFunCtx.reqModel = exeCtx.getReqModel();
133
- exeFunCtx.resModel = exeCtx.getResModel();
134
-
135
- result = this.serviceHub.executeFuncCfg(funcCfg, exeFunCtx); // 可以抛出异常
136
- } catch (e) {
137
- this.logger.debug('assertByValidate', e);
138
- throw new CommonException(Exceptions.RUN_EXECUTE_VALIDATE, {
139
- funcCfg: funcCfg,
140
- originSql: sqlCfgModel.originSql,
141
- error: '' + e,
142
- });
143
- }
144
-
145
- if (!(typeof result === 'boolean')) {
146
- // 自定义的校验函数,必须返回boolean类型的树枝
147
- throw new CommonException(Exceptions.CFG_IS_NEED_EXECUTE_FUNC_MUST_RETURN_BOOL, {
148
- funcCfg: funcCfg,
149
- originSql: sqlCfgModel.originSql,
150
- });
151
- }
152
-
153
- if (result === false) {
154
- throw new CommonException(Exceptions.RUN_EXECUTE_VALIDATE, errMsg);
155
- }
156
- }
157
-
158
- /**
159
- * 判断此SQL是否需要执行
160
- * @param sqlCfgModel
161
- * @param exeCtx
162
- * @private
163
- */
164
- private isNeedExecute(sqlCfgModel: SqlCfgModel) {
165
- const exeCtx = this.getExecuteContext();
166
- const funcCfg = sqlCfgModel.executeWhen;
167
- if (!funcCfg || Object.keys(funcCfg).length === 0) {
168
- return true; // 没有配置。默认,需要执行。
169
- }
170
-
171
- if (!checkFuncCfgValid(funcCfg)) {
172
- throw new CommonException(Exceptions.CFG_CHECK_EXECUTE_WHEN_EMPTY, {
173
- funcCfg: funcCfg,
174
- originSql: sqlCfgModel.originSql,
175
- });
176
- }
177
-
178
- let result;
179
-
180
- try {
181
- const exeFunCtx = new FuncContext();
182
-
183
- exeFunCtx.currentValue = null;
184
- exeFunCtx.reqModel = exeCtx.getReqModel();
185
- exeFunCtx.resModel = exeCtx.getResModel();
186
-
187
- result = this.serviceHub.executeFuncCfg(funcCfg, exeFunCtx);
188
- } catch (e) {
189
- this.logger.debug('isNeedExecute', e);
190
- throw new CommonException(Exceptions.RUN_IS_NEED_EXECUTE_ERR, {
191
- funcCfg: funcCfg,
192
- originSql: sqlCfgModel.originSql,
193
- error: '' + e,
194
- });
195
- }
196
-
197
- if (!(typeof result === 'boolean')) {
198
- // 自定义的校验函数,必须返回boolean类型的树枝
199
- throw new CommonException(Exceptions.CFG_IS_NEED_EXECUTE_FUNC_MUST_RETURN_BOOL, {
200
- funcCfg: funcCfg,
201
- originSql: sqlCfgModel.originSql,
202
- });
203
- }
204
-
205
- return result === true;
206
- }
207
-
208
- private toQueryResByResPicker(sqlRes: any, queryRes: any, sqlCfgModel: SqlCfgModel) {
209
- const resPicker = sqlCfgModel.resPicker;
210
-
211
- // 返回第一行
212
- if (KeysOfSqlResPicker.RESULT_FIRST_ROW === resPicker) {
213
- const rows = sqlRes as any[];
214
- if (rows && rows.length > 0) {
215
- return rows[0];
216
- }
217
- return null;
218
- }
219
-
220
- // $ResultSet[0].total_count , 只取第一行的的total_count
221
- if (KeysOfSqlResPicker.RESULT_TOTAL_COUNT === resPicker) {
222
- const rows = sqlRes as any[];
223
- if (isEmpty(rows)) {
224
- return 0;
225
- }
226
- const map0 = rows[0];
227
- return Number(map0['total_count'] || 0);
228
- }
229
-
230
- // 增删改res的内容是修改结果:包括: insert\delete\update
231
- if (KeysOfSqlResPicker.UPDATE_RESULT === resPicker) {
232
- if (sqlCfgModel.sqlDbType === SqlDbType.postgres) {
233
- return {
234
- insertId: _.get(queryRes, 'rows[0].id'),
235
- affectedRows: _.get(queryRes, 'rowCount'),
236
- };
237
- }
238
-
239
- if (sqlCfgModel.sqlDbType === SqlDbType.sqlserver) {
240
- return {
241
- insertId: _.get(queryRes, 'recordset[0].id'),
242
- affectedRows: _.get(queryRes, 'rowsAffected[0]'),
243
- };
244
- }
245
-
246
- return sqlRes;
247
- }
248
-
249
- //其他配置:形如: sqlRes[0].total_count
250
- if (typeof resPicker === 'string') {
251
- if (resPicker.startsWith('sqlRes')) {
252
- return _.get({ sqlRes }, resPicker);
253
- } else {
254
- throw new CommonException(Exceptions.RUN_EXECUTE_VALIDATE, 'resPicker必须是以sqlRes开头');
255
- }
256
- }
257
-
258
- return sqlRes;
259
- }
260
- }
261
-
262
- export { CrudProExecuteSqlService };
@@ -1,60 +0,0 @@
1
- import { RequestModel } from '../models/RequestModel';
2
- import { CrudProServiceBase } from './CrudProServiceBase';
3
- import { RequestCfgModel } from '../models/RequestCfgModel';
4
- import { MixinUtils } from '../utils/MixinUtils';
5
- import { IFuncCfgModel } from '../interfaces';
6
- import * as _ from 'lodash';
7
- import { ModelUtils } from '../utils/ModelUtils';
8
- import { CommonException, Exceptions } from '../exceptions';
9
- import { FuncContext } from '../models/FuncContext';
10
- const { checkFuncCfgValid } = ModelUtils;
11
- const { isEmpty } = MixinUtils;
12
-
13
- class CrudProFieldUpdateService extends CrudProServiceBase {
14
- updateByCfg(cfgModel: RequestCfgModel, reqModel: RequestModel) {
15
- const updateCfgMap = cfgModel.updateCfg;
16
- if (isEmpty(updateCfgMap)) {
17
- return;
18
- }
19
-
20
- const keys = Object.keys(updateCfgMap);
21
- for (let i = 0; i < keys.length; i++) {
22
- const key = keys[i]; // data.name
23
- const updateCfg = updateCfgMap[key];
24
- this.updateByJsonObject(updateCfg, key, reqModel);
25
- }
26
- }
27
-
28
- private updateByJsonObject(updateCfg: IFuncCfgModel, key: string, reqModel: RequestModel) {
29
- if (isEmpty(updateCfg)) {
30
- return;
31
- }
32
- const itemOldValue = _.get(reqModel, key);
33
- const newValue = this.getUpdateNewValueByCfg(updateCfg, itemOldValue, key, reqModel);
34
- _.set(reqModel, key, newValue);
35
- }
36
-
37
- private getUpdateNewValueByCfg(updateCfg: IFuncCfgModel, itemOldValue: any, key: string, reqModel: RequestModel) {
38
- if (!checkFuncCfgValid(updateCfg)) {
39
- throw new CommonException(Exceptions.CFG_UPDATE_ITEM_MUST_FUNC_CFG, 'updateCfg配置项必须是满足IFuncCfgModel' + key);
40
- }
41
-
42
- let result;
43
-
44
- try {
45
- const exeFunCtx = new FuncContext();
46
-
47
- exeFunCtx.currentValue = itemOldValue;
48
- exeFunCtx.reqModel = reqModel;
49
-
50
- result = this.serviceHub.executeFuncCfg(updateCfg, exeFunCtx);
51
- } catch (e) {
52
- this.logger.error('getUpdateNewValueByCfg->executeFuncCfg', e);
53
- throw new CommonException(Exceptions.UPDATE_EXCEPTION, key + '.' + e);
54
- }
55
-
56
- return result;
57
- }
58
- }
59
-
60
- export { CrudProFieldUpdateService };
@@ -1,180 +0,0 @@
1
- import { RequestModel } from '../models/RequestModel';
2
- import { CrudProServiceBase } from './CrudProServiceBase';
3
- import { RequestCfgModel } from '../models/RequestCfgModel';
4
- import { MixinUtils } from '../utils/MixinUtils';
5
- import { KeysOfConditions, KeysOfValidators } from '../models/keys';
6
- import { CommonException, Exceptions } from '../exceptions';
7
- import { ModelUtils } from '../utils/ModelUtils';
8
- import { ICustomValidateFunc, IFuncCfgModel, IValidatorCfgItem } from '../interfaces';
9
- import { FuncContext } from '../models/FuncContext';
10
- import { validateByCfgString } from '../utils/ValidateUtils';
11
-
12
- const { checkFuncCfgValid } = ModelUtils;
13
-
14
- const { isEmpty, isNotEmpty, deepTravelObject } = MixinUtils;
15
-
16
- class CrudProFieldValidateService extends CrudProServiceBase {
17
- validateByAllow(cfgModel: RequestCfgModel, reqModel: RequestModel) {
18
- const allowCfg = cfgModel.allowCfg; // allowCfg: {data: ['login_name', 'nick_name', 'avatar', 'status'],},
19
- if (isEmpty(allowCfg)) {
20
- return;
21
- }
22
-
23
- const keys = Object.keys(allowCfg);
24
-
25
- const validateAllowByJsonObject = (allowList: string[], targetObj: any, key: string) => {
26
- if (isEmpty(allowList)) {
27
- return;
28
- }
29
-
30
- const isCondition = key.startsWith('condition');
31
-
32
- // 任意一个key,不在allow列表中,就不合法
33
- deepTravelObject(
34
- targetObj,
35
- (value, key, index, keyPath) => {
36
- if (isCondition) {
37
- if (!KeysOfConditions.ALL_KEYS.has(key) && !allowList.includes(key)) {
38
- throw new CommonException(Exceptions.VALIDATE_ALLOW_ERR, keyPath);
39
- }
40
- } else {
41
- if (!allowList.includes(key)) {
42
- throw new CommonException(Exceptions.VALIDATE_ALLOW_ERR, keyPath);
43
- }
44
- }
45
- },
46
- key
47
- );
48
- };
49
-
50
- for (let i = 0; i < keys.length; i++) {
51
- const key = keys[i]; // demo: data or condition
52
- const allowList = MixinUtils.parseStringArray(allowCfg[key]);
53
- if (Array.isArray(allowList) && allowList.length > 0) {
54
- const targetObj = reqModel.getCondOrDataJsonObject(key);
55
- validateAllowByJsonObject(allowList, targetObj, key);
56
- }
57
- }
58
- }
59
-
60
- validateByReject(cfgModel: RequestCfgModel, reqModel: RequestModel) {
61
- const rejectCfgMap = cfgModel.rejectCfg;
62
- if (isEmpty(rejectCfgMap)) {
63
- return;
64
- }
65
-
66
- const keys = Object.keys(rejectCfgMap);
67
-
68
- const validateRejectByJsonObject = (rejectList: string[], targetObj: any, conditionName: string) => {
69
- if (isEmpty(rejectList)) {
70
- return;
71
- }
72
- // 任意一个key,出现在reject列表中,就不合法
73
- deepTravelObject(
74
- targetObj,
75
- (value, key, index, keyPath) => {
76
- if (rejectList.includes(key)) {
77
- throw new CommonException(Exceptions.VALIDATE_REJECT_ERR, keyPath);
78
- }
79
- },
80
- conditionName
81
- );
82
- };
83
-
84
- for (let i = 0; i < keys.length; i++) {
85
- const conditionName = keys[i];
86
- const rejectList = MixinUtils.parseStringArray(rejectCfgMap[conditionName]);
87
- if (Array.isArray(rejectList) && rejectList.length > 0) {
88
- const targetObj = reqModel.getCondOrDataJsonObject(conditionName);
89
- validateRejectByJsonObject(rejectList, targetObj, conditionName);
90
- }
91
- }
92
- }
93
-
94
- validateByCfg(cfgModel: RequestCfgModel, reqModel: RequestModel) {
95
- const validateCfg = cfgModel.validateCfg;
96
- if (isEmpty(validateCfg)) {
97
- return;
98
- }
99
- const keys = Object.keys(validateCfg);
100
- for (const key of keys) {
101
- const itemValue = reqModel.getJsonObjectValue(key);
102
- const cfgList = validateCfg[key];
103
-
104
- if (isNotEmpty(cfgList)) {
105
- // 先校验是否必填。
106
- this.validateRequired(cfgList, itemValue, key);
107
-
108
- // 除REQUIRED之外的其他配置
109
- const cfgList2 = cfgList.filter(cfg => {
110
- return cfg !== KeysOfValidators.REQUIRED;
111
- });
112
-
113
- // 只有数值不为空时,才需要校验。非必填情况下:内容为空,不需要校验
114
- if (isNotEmpty(itemValue)) {
115
- for (const cfg of cfgList2) {
116
- if (typeof cfg === 'string') {
117
- this.validateByCfgString(cfg, reqModel, itemValue, key);
118
- } else if (typeof cfg === 'function') {
119
- this.validateByCfgCustomFunction(cfg, reqModel, itemValue, key);
120
- } else {
121
- this.validateByCfgFunc(cfg, reqModel, itemValue, key);
122
- }
123
- }
124
- }
125
- }
126
- }
127
- }
128
-
129
- private validateByCfgCustomFunction(cfg: ICustomValidateFunc, reqModel: RequestModel, itemValue: any, key: string) {
130
- // 使用自定义函数校验
131
- cfg(itemValue);
132
- }
133
-
134
- private validateByCfgString(itemCfg: KeysOfValidators | string, reqModel: RequestModel, itemValue: any, errPath: string) {
135
- return validateByCfgString(itemCfg, itemValue, errPath);
136
- }
137
-
138
- private validateByCfgFunc(funcCfg: IFuncCfgModel, reqModel: RequestModel, itemValue: any, errPath: string) {
139
- if (!checkFuncCfgValid(funcCfg)) {
140
- throw new CommonException(Exceptions.CFG_VALIDATE_CUSTOM_FUNCTION_EMPTY);
141
- }
142
-
143
- let result;
144
-
145
- try {
146
- const exeFunCtx = new FuncContext();
147
-
148
- exeFunCtx.currentValue = itemValue;
149
- exeFunCtx.reqModel = reqModel;
150
-
151
- result = this.serviceHub.executeFuncCfg(funcCfg, exeFunCtx);
152
- } catch (e) {
153
- this.logger.error('validateByCfgFunc', e);
154
- throw new CommonException(Exceptions.VALIDATE_EXCEPTION, errPath + '.' + e);
155
- }
156
-
157
- if (!(typeof result === 'boolean')) {
158
- // 自定义的校验函数,必须返回boolean类型的树枝
159
- throw new CommonException(Exceptions.CFG_VALIDATE_CUSTOM_FUNCTION_MUST_RETURN_BOOL, errPath);
160
- }
161
-
162
- if (result === false) {
163
- throw new CommonException(Exceptions.VALIDATE_ERR, funcCfg.message || errPath);
164
- }
165
- }
166
-
167
- private validateRequired(cfgList: IValidatorCfgItem[], itemValue: any, errPath: string) {
168
- const required = cfgList.find(cfg => {
169
- return cfg === KeysOfValidators.REQUIRED;
170
- });
171
-
172
- if (required) {
173
- if (isEmpty(itemValue)) {
174
- throw new CommonException(Exceptions.VALIDATE_ERR, '参数校验错误,缺少必填字段:' + errPath);
175
- }
176
- }
177
- }
178
- }
179
-
180
- export { CrudProFieldValidateService };