midway-fatcms 0.0.7 → 0.0.8
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/.qoder/skills/midway-fatcms/01-quick-start.md +231 -0
- package/.qoder/skills/midway-fatcms/02-crud-quick.md +337 -0
- package/.qoder/skills/midway-fatcms/03-crud-sharding.md +488 -0
- package/.qoder/skills/midway-fatcms/04-condition-operators.md +93 -0
- package/.qoder/skills/midway-fatcms/05-configuration.md +290 -0
- package/.qoder/skills/midway-fatcms/06-builtin-functions.md +241 -0
- package/.qoder/skills/midway-fatcms/07-examples.md +500 -0
- package/.qoder/skills/midway-fatcms/SKILL.md +96 -0
- package/README.md +9 -9
- package/dist/controller/base/BaseApiController.d.ts +1 -2
- package/dist/controller/base/BaseApiController.js +0 -4
- package/dist/controller/gateway/DocGatewayController.js +1 -1
- package/dist/controller/manage/FlowConfigManageApi.js +4 -2
- package/dist/controller/manage/SysConfigMangeApi.js +6 -1
- package/dist/controller/manage/UserAccountManageApi.js +7 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/libs/crud-pro/CrudPro.d.ts +23 -2
- package/dist/libs/crud-pro/CrudPro.js +53 -2
- package/dist/libs/crud-pro/interfaces.d.ts +82 -12
- package/dist/libs/crud-pro/models/CrudResult.d.ts +115 -0
- package/dist/libs/crud-pro/models/CrudResult.js +126 -0
- package/dist/libs/crud-pro/models/RequestModel.d.ts +2 -2
- package/dist/libs/crud-pro/services/CrudProExecuteSqlService.js +36 -2
- package/dist/libs/crud-pro/services/CrudProGenSqlCondition.js +8 -4
- package/dist/libs/crud-pro/services/CrudProTableMetaService.js +1 -2
- package/dist/libs/crud-pro-quick/CrudProQuick.d.ts +295 -0
- package/dist/libs/crud-pro-quick/CrudProQuick.js +529 -0
- package/dist/libs/crud-pro-quick/fixSoftDelete.d.ts +30 -0
- package/dist/{service/curd → libs/crud-pro-quick}/fixSoftDelete.js +3 -6
- package/dist/libs/crud-pro-quick/index.d.ts +36 -0
- package/dist/libs/crud-pro-quick/index.js +49 -0
- package/dist/libs/crud-pro-quick/models.d.ts +33 -0
- package/dist/libs/crud-pro-quick/models.js +2 -0
- package/dist/libs/crud-sharding/ShardingConfig.d.ts +15 -2
- package/dist/libs/crud-sharding/ShardingConfig.js +2 -2
- package/dist/libs/crud-sharding/ShardingCrudPro.d.ts +119 -274
- package/dist/libs/crud-sharding/ShardingCrudPro.js +544 -340
- package/dist/libs/crud-sharding/ShardingMerger.d.ts +10 -18
- package/dist/libs/crud-sharding/ShardingMerger.js +27 -44
- package/dist/libs/crud-sharding/ShardingResult.d.ts +33 -0
- package/dist/libs/crud-sharding/ShardingResult.js +16 -0
- package/dist/libs/crud-sharding/ShardingRouter.d.ts +1 -0
- package/dist/libs/crud-sharding/ShardingRouter.js +25 -6
- package/dist/libs/crud-sharding/ShardingTableCreator.d.ts +21 -4
- package/dist/libs/crud-sharding/ShardingTableCreator.js +193 -59
- package/dist/libs/crud-sharding/ShardingUtils.d.ts +48 -0
- package/dist/libs/crud-sharding/ShardingUtils.js +122 -1
- package/dist/libs/crud-sharding/TIME_COLUMN_CLEAN_SPEC.md +488 -0
- package/dist/libs/crud-sharding/index.d.ts +4 -3
- package/dist/libs/crud-sharding/index.js +14 -2
- package/dist/models/bizmodels.d.ts +2 -6
- package/dist/service/SysAppService.d.ts +2 -2
- package/dist/service/SysAppService.js +16 -5
- package/dist/service/SysConfigService.d.ts +1 -1
- package/dist/service/SysConfigService.js +7 -2
- package/dist/service/SysDictDataService.js +14 -4
- package/dist/service/SysMenuService.js +7 -2
- package/dist/service/curd/CurdMixService.d.ts +6 -4
- package/dist/service/curd/CurdMixService.js +16 -2
- package/dist/service/curd/CurdProService.d.ts +43 -27
- package/dist/service/curd/CurdProService.js +32 -33
- package/dist/service/flow/FlowConfigService.js +7 -2
- package/dist/service/flow/FlowInstanceCrudService.js +22 -19
- package/package.json +1 -1
- package/src/controller/base/BaseApiController.ts +0 -5
- package/src/controller/gateway/DocGatewayController.ts +1 -1
- package/src/controller/manage/CrudStandardDesignApi.ts +4 -3
- package/src/controller/manage/FlowConfigManageApi.ts +4 -2
- package/src/controller/manage/SysConfigMangeApi.ts +6 -1
- package/src/controller/manage/UserAccountManageApi.ts +7 -2
- package/src/index.ts +2 -2
- package/src/libs/crud-pro/CrudPro.ts +62 -4
- package/src/libs/crud-pro/interfaces.ts +110 -15
- package/src/libs/crud-pro/models/CrudResult.ts +178 -0
- package/src/libs/crud-pro/models/RequestModel.ts +2 -2
- package/src/libs/crud-pro/services/CrudProExecuteSqlService.ts +41 -2
- package/src/libs/crud-pro/services/CrudProGenSqlCondition.ts +11 -7
- package/src/libs/crud-pro/services/CrudProTableMetaService.ts +1 -2
- package/src/libs/crud-pro-quick/CrudProQuick.ts +594 -0
- package/src/{service/curd → libs/crud-pro-quick}/fixSoftDelete.ts +23 -13
- package/src/libs/crud-pro-quick/index.ts +52 -0
- package/src/libs/crud-pro-quick/models.ts +35 -0
- package/src/libs/crud-sharding/ShardingConfig.ts +18 -2
- package/src/libs/crud-sharding/ShardingCrudPro.ts +660 -390
- package/src/libs/crud-sharding/ShardingMerger.ts +35 -63
- package/src/libs/crud-sharding/ShardingResult.ts +29 -0
- package/src/libs/crud-sharding/ShardingRouter.ts +27 -6
- package/src/libs/crud-sharding/ShardingTableCreator.ts +214 -71
- package/src/libs/crud-sharding/ShardingUtils.ts +137 -0
- package/src/libs/crud-sharding/TIME_COLUMN_CLEAN_SPEC.md +488 -0
- package/src/libs/crud-sharding/index.ts +14 -3
- package/src/models/bizmodels.ts +4 -7
- package/src/service/SysAppService.ts +18 -7
- package/src/service/SysConfigService.ts +8 -3
- package/src/service/SysDictDataService.ts +14 -4
- package/src/service/SysMenuService.ts +7 -2
- package/src/service/crudstd/CrudStdService.ts +2 -2
- package/src/service/curd/CurdMixService.ts +26 -5
- package/src/service/curd/CurdProService.ts +58 -39
- package/src/service/flow/FlowConfigService.ts +7 -2
- package/src/service/flow/FlowInstanceCrudService.ts +23 -20
- package/.qoder/skills/midway-fatcms-crud/SKILL.md +0 -375
- package/.qoder/skills/midway-fatcms-crud/examples.md +0 -990
- package/.qoder/skills/midway-fatcms-crud/reference.md +0 -568
- package/dist/libs/crud-pro/README.md +0 -809
- package/dist/libs/crud-pro/README_FUNC.md +0 -193
- package/dist/libs/crud-sharding/ROUTING_LOGIC.md +0 -944
- package/dist/models/StandardColumns.d.ts +0 -71
- package/dist/models/StandardColumns.js +0 -28
- package/dist/service/curd/CrudProQuick.d.ts +0 -190
- package/dist/service/curd/CrudProQuick.js +0 -319
- package/dist/service/curd/README.md +0 -1100
- package/dist/service/curd/fixSoftDelete.d.ts +0 -20
- package/src/libs/crud-pro/README.md +0 -809
- package/src/libs/crud-pro/README_FUNC.md +0 -193
- package/src/libs/crud-sharding/ROUTING_LOGIC.md +0 -944
- package/src/models/StandardColumns.ts +0 -76
- package/src/service/curd/CrudProQuick.ts +0 -360
- package/src/service/curd/README.md +0 -1100
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ICrudProCfg, ILogger, IRequestCfgModel, IRequestModel, ISqlCfgModel, ITableListResult, ITableNamesOptions, ITableNamesQuery, IVisitor } from './interfaces';
|
|
1
|
+
import { ICrudProCfg, ILogger, IRequestCfgModel, IRequestModel, ISqlCfgModel, ITableListResult, ITableNamesOptions, ITableNamesQuery, IVisitor, ExecuteSQLResult } from './interfaces';
|
|
2
2
|
import { ExecuteContext } from './models/ExecuteContext';
|
|
3
3
|
import { RequestModel } from './models/RequestModel';
|
|
4
4
|
import { CurdProServiceHub } from './services/CurdProServiceHub';
|
|
@@ -13,11 +13,44 @@ class CrudPro {
|
|
|
13
13
|
private readonly executeContext: ExecuteContext;
|
|
14
14
|
private readonly serviceHub: CurdProServiceHub;
|
|
15
15
|
|
|
16
|
+
/** 标记是否已被使用(防止重复使用导致状态污染) */
|
|
17
|
+
private _isUsed: boolean = false;
|
|
18
|
+
/** 标记是否正在执行中(防止并发调用) */
|
|
19
|
+
private _isExecuting: boolean = false;
|
|
20
|
+
|
|
16
21
|
constructor() {
|
|
17
22
|
this.executeContext = new ExecuteContext();
|
|
18
23
|
this.serviceHub = new CurdProServiceHub(this.executeContext);
|
|
19
24
|
}
|
|
20
25
|
|
|
26
|
+
/**
|
|
27
|
+
* 检查是否可以开始执行
|
|
28
|
+
* @throws 如果已使用或正在执行中则抛出异常
|
|
29
|
+
*/
|
|
30
|
+
private checkAndMarkExecuting(): void {
|
|
31
|
+
if (this._isUsed) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
'[CrudPro] 此实例已被使用,不能重复使用。' +
|
|
34
|
+
'每次操作请创建新的 CrudPro 实例,可通过 curdProService.getCrudPro() 或工厂函数获取。'
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
if (this._isExecuting) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
'[CrudPro] 此实例正在执行中,不能并发调用。' +
|
|
40
|
+
'请等待当前操作完成后再进行下一次调用,或创建新的实例。'
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
this._isExecuting = true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 标记执行完成
|
|
48
|
+
*/
|
|
49
|
+
private markExecutionComplete(): void {
|
|
50
|
+
this._isExecuting = false;
|
|
51
|
+
this._isUsed = true;
|
|
52
|
+
}
|
|
53
|
+
|
|
21
54
|
// 必填
|
|
22
55
|
set transaction(transaction: Transaction) {
|
|
23
56
|
this.executeContext.setTransaction(transaction);
|
|
@@ -30,6 +63,10 @@ class CrudPro {
|
|
|
30
63
|
set visitor(visitor: IVisitor) {
|
|
31
64
|
this.executeContext.setVisitor(visitor);
|
|
32
65
|
}
|
|
66
|
+
// 必填
|
|
67
|
+
getVisitor() {
|
|
68
|
+
return this.executeContext.getVisitor();
|
|
69
|
+
}
|
|
33
70
|
|
|
34
71
|
set contextFunc(contextFunc: IExecuteContextFunc) {
|
|
35
72
|
contextFunc.setExecuteContext(this.executeContext);
|
|
@@ -43,8 +80,11 @@ class CrudPro {
|
|
|
43
80
|
/**
|
|
44
81
|
* 直接执行一个SQL
|
|
45
82
|
* @param sqlCfgModel
|
|
83
|
+
* @returns 返回未被包装的数据库原始结果(根据 resPicker 配置返回不同类型,无 {code, data, message} 包装层)
|
|
46
84
|
*/
|
|
47
|
-
public async executeSQL(sqlCfgModel: ISqlCfgModel): Promise<
|
|
85
|
+
public async executeSQL(sqlCfgModel: ISqlCfgModel): Promise<ExecuteSQLResult> {
|
|
86
|
+
this.checkAndMarkExecuting();
|
|
87
|
+
|
|
48
88
|
const RES_NAME = 'executeResult';
|
|
49
89
|
|
|
50
90
|
const sqlCfgModel2 = {
|
|
@@ -64,8 +104,12 @@ class CrudPro {
|
|
|
64
104
|
sqlCfgList: [sqlCfgModel2],
|
|
65
105
|
};
|
|
66
106
|
|
|
67
|
-
|
|
68
|
-
|
|
107
|
+
try {
|
|
108
|
+
const ss = await this.executeCrudByCfgInternal({}, cfgModel);
|
|
109
|
+
return ss.getResModelItem(RES_NAME);
|
|
110
|
+
} finally {
|
|
111
|
+
this.markExecutionComplete();
|
|
112
|
+
}
|
|
69
113
|
}
|
|
70
114
|
|
|
71
115
|
/**
|
|
@@ -87,6 +131,20 @@ class CrudPro {
|
|
|
87
131
|
* @param cfgJson
|
|
88
132
|
*/
|
|
89
133
|
public async executeCrudByCfg(reqJson: IRequestModel, cfgJson: IRequestCfgModel): Promise<ExecuteContext> {
|
|
134
|
+
this.checkAndMarkExecuting();
|
|
135
|
+
try {
|
|
136
|
+
return await this.executeCrudByCfgInternal(reqJson, cfgJson);
|
|
137
|
+
} finally {
|
|
138
|
+
this.markExecutionComplete();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* 内部执行方法(不含检查逻辑,供内部调用)
|
|
144
|
+
* @param reqJson
|
|
145
|
+
* @param cfgJson
|
|
146
|
+
*/
|
|
147
|
+
private async executeCrudByCfgInternal(reqJson: IRequestModel, cfgJson: IRequestCfgModel): Promise<ExecuteContext> {
|
|
90
148
|
const logger = this.executeContext.getLogger();
|
|
91
149
|
logger.info('CurdPro executeCrudByCfg', cfgJson);
|
|
92
150
|
|
|
@@ -53,7 +53,7 @@ export interface IPoolConnectionClient {
|
|
|
53
53
|
export interface IRequestModel {
|
|
54
54
|
method?: string;
|
|
55
55
|
columns?: string | string[]; // "columns": "id,name,age,sex,addr",
|
|
56
|
-
condition?:
|
|
56
|
+
condition?: IRequestCondition; // 拼接查询条件
|
|
57
57
|
data?: Record<string, any> | Record<string, any>[]; // 单条或批量插入/更新的数据部分
|
|
58
58
|
orderBy?: string | IOrderByItem[];
|
|
59
59
|
limit?: number;
|
|
@@ -64,23 +64,57 @@ export interface IRequestModel {
|
|
|
64
64
|
|
|
65
65
|
type IBasicType = boolean | string | number;
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* 比较操作符条件
|
|
69
|
+
* 支持 MongoDB 风格的所有查询操作符
|
|
70
|
+
*/
|
|
67
71
|
export interface ICompareCondition {
|
|
68
|
-
|
|
69
|
-
$
|
|
70
|
-
$lt?: IBasicType;
|
|
71
|
-
$lte?: IBasicType;
|
|
72
|
-
$
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
$
|
|
77
|
-
$
|
|
78
|
-
|
|
79
|
-
|
|
72
|
+
// 比较操作符
|
|
73
|
+
$ne?: IBasicType; // 不等于
|
|
74
|
+
$lt?: IBasicType; // 小于
|
|
75
|
+
$lte?: IBasicType; // 小于或等于
|
|
76
|
+
$gt?: IBasicType; // 大于
|
|
77
|
+
$gte?: IBasicType; // 大于或等于
|
|
78
|
+
|
|
79
|
+
// 集合操作符
|
|
80
|
+
$in?: IBasicType[]; // 在集合中
|
|
81
|
+
$nin?: IBasicType[]; // 不在集合中
|
|
82
|
+
|
|
83
|
+
// 范围操作符
|
|
84
|
+
$range?: [number, number]; // 范围查询 [min, max]
|
|
85
|
+
|
|
86
|
+
// 模糊匹配操作符
|
|
87
|
+
$like?: string; // 前缀匹配 A%
|
|
88
|
+
$notLike?: string; // 非前缀匹配
|
|
89
|
+
$likeInclude?: string; // 包含匹配 %A%
|
|
90
|
+
$notLikeInclude?: string; // 不包含匹配
|
|
91
|
+
|
|
92
|
+
// 全文索引操作符
|
|
93
|
+
$match?: string; // MySQL 全文索引
|
|
94
|
+
$matchBool?: string | boolean; // MySQL 布尔全文索引
|
|
95
|
+
|
|
96
|
+
// NULL 判断操作符
|
|
97
|
+
$null?: boolean; // IS NULL
|
|
98
|
+
$notNull?: boolean; // IS NOT NULL
|
|
99
|
+
|
|
100
|
+
// JSON 操作符
|
|
101
|
+
$jsonArrayContains?: string; // JSON 数组包含
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 逻辑操作符条件
|
|
106
|
+
*/
|
|
107
|
+
export interface ILogicalCondition {
|
|
108
|
+
$or?: IRequestCondition[]; // 逻辑或
|
|
109
|
+
$and?: IRequestCondition[]; // 逻辑与
|
|
80
110
|
}
|
|
81
111
|
|
|
82
|
-
|
|
83
|
-
|
|
112
|
+
/**
|
|
113
|
+
* 查询条件类型
|
|
114
|
+
* 支持字段值直接匹配或比较操作符
|
|
115
|
+
* 支持 $or, $and 逻辑组合
|
|
116
|
+
*/
|
|
117
|
+
export interface IRequestCondition extends Record<string, IBasicType | ICompareCondition | ILogicalCondition | IRequestCondition[] | undefined>, ILogicalCondition {
|
|
84
118
|
}
|
|
85
119
|
|
|
86
120
|
/**
|
|
@@ -163,6 +197,67 @@ export interface ISqlCfgModel extends IBaseCfgModel {
|
|
|
163
197
|
crudType?: string;
|
|
164
198
|
}
|
|
165
199
|
|
|
200
|
+
/**
|
|
201
|
+
* executeSQL 的返回结果类型说明(未被包装的直接结果)
|
|
202
|
+
*
|
|
203
|
+
* 注意:executeSQL 返回的是数据库查询的原始结果,没有 {code, data, message} 包装层。
|
|
204
|
+
* 返回类型取决于 resPicker 配置,请根据实际 SQL 类型选择合适的类型断言。
|
|
205
|
+
*
|
|
206
|
+
* 使用示例:
|
|
207
|
+
* // 查询列表 - 返回数组
|
|
208
|
+
* const rows = await executeSQL({ executeSql: 'SELECT * FROM users', resPicker: KeysOfSqlResPicker.RESULT_ROWS }) as ExecuteSQLRowResult<User>;
|
|
209
|
+
*
|
|
210
|
+
* // 查询单行 - 返回对象或null
|
|
211
|
+
* const user = await executeSQL({ executeSql: 'SELECT * FROM users WHERE id=?', executeSqlArgs: [1], resPicker: KeysOfSqlResPicker.RESULT_FIRST_ROW }) as ExecuteSQLSingleResult<User>;
|
|
212
|
+
*
|
|
213
|
+
* // 查询总数 - 返回数字
|
|
214
|
+
* const count = await executeSQL({ executeSql: 'SELECT COUNT(*) as total_count FROM users', resPicker: KeysOfSqlResPicker.RESULT_TOTAL_COUNT }) as ExecuteSQLCountResult;
|
|
215
|
+
*
|
|
216
|
+
* // 判断存在 - 返回布尔
|
|
217
|
+
* const exists = await executeSQL({ executeSql: 'SELECT 1 as is_exist FROM users WHERE id=?', executeSqlArgs: [1], resPicker: KeysOfSqlResPicker.RESULT_IS_EXIST }) as ExecuteSQLExistResult;
|
|
218
|
+
*
|
|
219
|
+
* // 增删改操作 - 返回受影响行数信息
|
|
220
|
+
* const result = await executeSQL({ executeSql: 'INSERT INTO users(name) VALUES(?)', executeSqlArgs: ['张三'], resPicker: KeysOfSqlResPicker.UPDATE_RESULT }) as ExecuteSQLUpdateResult;
|
|
221
|
+
*/
|
|
222
|
+
|
|
223
|
+
/** 查询多行时的返回类型(默认情况):原始数据行数组,无包装 */
|
|
224
|
+
export type ExecuteSQLRowsResult<T = Record<string, any>> = T[];
|
|
225
|
+
|
|
226
|
+
/** 查询单行时的返回类型:首行对象或null,无包装 */
|
|
227
|
+
export type ExecuteSQLSingleResult<T = Record<string, any>> = T | null;
|
|
228
|
+
|
|
229
|
+
/** 查询数量时的返回类型:数字,无包装 */
|
|
230
|
+
export type ExecuteSQLCountResult = number;
|
|
231
|
+
|
|
232
|
+
/** 判断存在时的返回类型:布尔值,无包装 */
|
|
233
|
+
export type ExecuteSQLExistResult = boolean;
|
|
234
|
+
|
|
235
|
+
/** 增删改操作时的返回类型:包含 insertId 和 affectedRows,无包装 */
|
|
236
|
+
export interface ExecuteSQLUpdateResult {
|
|
237
|
+
/** 插入记录的自增ID(仅INSERT操作有效) */
|
|
238
|
+
insertId: any;
|
|
239
|
+
/** 受影响的行数 */
|
|
240
|
+
affectedRows: number;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* executeSQL 的通用返回类型(未被包装的直接结果)
|
|
245
|
+
* 根据 resPicker 的不同配置,实际返回类型可能是:
|
|
246
|
+
* - ExecuteSQLRowResult<T> : 多行数据(默认)
|
|
247
|
+
* - ExecuteSQLSingleResult<T> : 单行数据或null
|
|
248
|
+
* - ExecuteSQLCountResult : 数字
|
|
249
|
+
* - ExecuteSQLExistResult : 布尔值
|
|
250
|
+
* - ExecuteSQLUpdateResult : 增删改结果
|
|
251
|
+
*
|
|
252
|
+
* 警告:此类型为联合类型,使用时建议根据实际 resPicker 进行类型断言以获得精确类型提示
|
|
253
|
+
*/
|
|
254
|
+
export type ExecuteSQLResult<T = Record<string, any>> =
|
|
255
|
+
| ExecuteSQLRowsResult<T>
|
|
256
|
+
| ExecuteSQLSingleResult<T>
|
|
257
|
+
| ExecuteSQLCountResult
|
|
258
|
+
| ExecuteSQLExistResult
|
|
259
|
+
| ExecuteSQLUpdateResult;
|
|
260
|
+
|
|
166
261
|
/**
|
|
167
262
|
* 自定义校验函数。如果不正确请抛出异常。{code:'xxx',message:'xxx'}
|
|
168
263
|
*/
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { ResModelAffected } from './ResModel';
|
|
2
|
+
import { ExecuteContext } from './ExecuteContext';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 结果基类的辅助定位信息
|
|
6
|
+
* 用于错误排查和日志记录
|
|
7
|
+
*/
|
|
8
|
+
export interface CrudResultDebugInfo {
|
|
9
|
+
/** 数据库名 */
|
|
10
|
+
sqlDatabase?: string;
|
|
11
|
+
/** 表名 */
|
|
12
|
+
sqlTable?: string;
|
|
13
|
+
/** 方法名(配置中的 method 字段) */
|
|
14
|
+
method?: string;
|
|
15
|
+
/** 查询条件(用于定位问题) */
|
|
16
|
+
condition?: Record<string, any>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 结果基类
|
|
21
|
+
*
|
|
22
|
+
* 使用 ES2022 私有字段 #rawContext 存储 ExecuteContext 引用。
|
|
23
|
+
* ES2022 私有字段特性:JSON.stringify 不会序列化 # 开头的私有字段。
|
|
24
|
+
* 因此结果对象可直接返回给 API 前端,自动过滤掉内部上下文。
|
|
25
|
+
*
|
|
26
|
+
* Node.js 兼容性:Node.js 14.6+ 完全支持 ES2022 私有字段。
|
|
27
|
+
*/
|
|
28
|
+
abstract class CrudResultBase {
|
|
29
|
+
#rawContext: ExecuteContext;
|
|
30
|
+
/** 辅助定位信息,用于错误排查 */
|
|
31
|
+
readonly debugInfo: CrudResultDebugInfo;
|
|
32
|
+
|
|
33
|
+
constructor(rawContext: ExecuteContext, debugInfo?: CrudResultDebugInfo) {
|
|
34
|
+
this.#rawContext = rawContext;
|
|
35
|
+
this.debugInfo = debugInfo || {};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
getRawContext(): ExecuteContext {
|
|
39
|
+
return this.#rawContext;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 生成带辅助定位信息的错误消息
|
|
44
|
+
* @param baseMessage 基础错误信息
|
|
45
|
+
* @returns 包含定位信息的完整错误消息
|
|
46
|
+
*/
|
|
47
|
+
protected buildErrorMessage(baseMessage: string): string {
|
|
48
|
+
const parts: string[] = [baseMessage];
|
|
49
|
+
|
|
50
|
+
if (this.debugInfo.sqlDatabase) {
|
|
51
|
+
parts.push(`数据库: ${this.debugInfo.sqlDatabase}`);
|
|
52
|
+
}
|
|
53
|
+
if (this.debugInfo.sqlTable) {
|
|
54
|
+
parts.push(`表: ${this.debugInfo.sqlTable}`);
|
|
55
|
+
}
|
|
56
|
+
if (this.debugInfo.method) {
|
|
57
|
+
parts.push(`方法: ${this.debugInfo.method}`);
|
|
58
|
+
}
|
|
59
|
+
if (this.debugInfo.condition) {
|
|
60
|
+
try {
|
|
61
|
+
parts.push(`条件: ${JSON.stringify(this.debugInfo.condition)}`);
|
|
62
|
+
} catch {
|
|
63
|
+
parts.push(`条件: [无法序列化]`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return parts.join(' | ');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 写操作结果(INSERT/UPDATE/DELETE) */
|
|
72
|
+
class CrudWriteResult extends CrudResultBase {
|
|
73
|
+
readonly affectedRows: number;
|
|
74
|
+
readonly insertId?: string | number;
|
|
75
|
+
|
|
76
|
+
constructor(data: {
|
|
77
|
+
affectedRows: number;
|
|
78
|
+
insertId?: string | number;
|
|
79
|
+
rawContext: ExecuteContext;
|
|
80
|
+
debugInfo?: CrudResultDebugInfo;
|
|
81
|
+
}) {
|
|
82
|
+
super(data.rawContext, data.debugInfo);
|
|
83
|
+
this.affectedRows = data.affectedRows;
|
|
84
|
+
this.insertId = data.insertId;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 单条查询结果 */
|
|
89
|
+
class CrudQueryOneResult<T = Record<string, any>> extends CrudResultBase {
|
|
90
|
+
readonly row: T | null;
|
|
91
|
+
readonly found: boolean;
|
|
92
|
+
|
|
93
|
+
constructor(data: { row: T | null; rawContext: ExecuteContext; debugInfo?: CrudResultDebugInfo }) {
|
|
94
|
+
super(data.rawContext, data.debugInfo);
|
|
95
|
+
this.row = data.row;
|
|
96
|
+
this.found = (this.row !== null && this.row !== undefined );
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 列表查询结果 */
|
|
101
|
+
class CrudQueryListResult<T = Record<string, any>> extends CrudResultBase {
|
|
102
|
+
readonly rows: T[];
|
|
103
|
+
readonly count: number;
|
|
104
|
+
|
|
105
|
+
constructor(data: { rows: T[]; rawContext: ExecuteContext; debugInfo?: CrudResultDebugInfo }) {
|
|
106
|
+
super(data.rawContext, data.debugInfo);
|
|
107
|
+
this.rows = data.rows || [];
|
|
108
|
+
this.count = this.rows.length;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** 分页查询结果 */
|
|
113
|
+
class CrudQueryPageResult<T = Record<string, any>> extends CrudResultBase {
|
|
114
|
+
readonly rows: T[];
|
|
115
|
+
readonly totalCount: number;
|
|
116
|
+
readonly count: number;
|
|
117
|
+
|
|
118
|
+
constructor(data: { rows: T[]; totalCount: number; rawContext: ExecuteContext; debugInfo?: CrudResultDebugInfo }) {
|
|
119
|
+
super(data.rawContext, data.debugInfo);
|
|
120
|
+
this.rows = data.rows || [];
|
|
121
|
+
this.totalCount = data.totalCount;
|
|
122
|
+
this.count = this.rows.length;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** 存在性判断结果 */
|
|
127
|
+
class CrudExistResult extends CrudResultBase {
|
|
128
|
+
readonly exists: boolean;
|
|
129
|
+
|
|
130
|
+
constructor(data: { exists: boolean; rawContext: ExecuteContext; debugInfo?: CrudResultDebugInfo }) {
|
|
131
|
+
super(data.rawContext, data.debugInfo);
|
|
132
|
+
this.exists = data.exists;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** 计数结果 */
|
|
137
|
+
class CrudCountResult extends CrudResultBase {
|
|
138
|
+
readonly count: number;
|
|
139
|
+
|
|
140
|
+
constructor(data: { count: number; rawContext: ExecuteContext; debugInfo?: CrudResultDebugInfo }) {
|
|
141
|
+
super(data.rawContext, data.debugInfo);
|
|
142
|
+
this.count = data.count;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** InsertOrUpdate 结果 */
|
|
147
|
+
class CrudUpsertResult extends CrudResultBase {
|
|
148
|
+
readonly affected?: ResModelAffected;
|
|
149
|
+
readonly insertAffected?: ResModelAffected;
|
|
150
|
+
readonly updateAffected?: ResModelAffected;
|
|
151
|
+
readonly isExist: boolean;
|
|
152
|
+
|
|
153
|
+
constructor(data: {
|
|
154
|
+
affected?: ResModelAffected;
|
|
155
|
+
insertAffected?: ResModelAffected;
|
|
156
|
+
updateAffected?: ResModelAffected;
|
|
157
|
+
isExist: boolean;
|
|
158
|
+
rawContext: ExecuteContext;
|
|
159
|
+
debugInfo?: CrudResultDebugInfo;
|
|
160
|
+
}) {
|
|
161
|
+
super(data.rawContext, data.debugInfo);
|
|
162
|
+
this.affected = data.affected;
|
|
163
|
+
this.insertAffected = data.insertAffected;
|
|
164
|
+
this.updateAffected = data.updateAffected;
|
|
165
|
+
this.isExist = data.isExist;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export {
|
|
170
|
+
CrudResultBase,
|
|
171
|
+
CrudWriteResult,
|
|
172
|
+
CrudQueryOneResult,
|
|
173
|
+
CrudQueryListResult,
|
|
174
|
+
CrudQueryPageResult,
|
|
175
|
+
CrudExistResult,
|
|
176
|
+
CrudCountResult,
|
|
177
|
+
CrudUpsertResult,
|
|
178
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _ from 'lodash';
|
|
2
|
-
import { ILimitOffset, IOrderByItem, IRequestModel, IVisitor } from '../interfaces';
|
|
2
|
+
import { ILimitOffset, IOrderByItem, IRequestModel, IVisitor, IRequestCondition } from '../interfaces';
|
|
3
3
|
import { MixinUtils } from '../utils/MixinUtils';
|
|
4
4
|
import { OrderByUtils } from '../utils/OrderByUtils';
|
|
5
5
|
import { CommonException, Exceptions } from '../exceptions';
|
|
@@ -10,7 +10,7 @@ class RequestModel {
|
|
|
10
10
|
|
|
11
11
|
method: string;
|
|
12
12
|
columns?: string[]; // "columns": "id,name,age,sex,addr",
|
|
13
|
-
condition?:
|
|
13
|
+
condition?: IRequestCondition;
|
|
14
14
|
data?: Record<string, any> | Record<string, any>[]; // 支持单条或批量数据
|
|
15
15
|
limit: number;
|
|
16
16
|
offset: number;
|
|
@@ -18,6 +18,9 @@ class CrudProExecuteSqlService extends CrudProServiceBase {
|
|
|
18
18
|
async executeSqlCfgModels(): Promise<void> {
|
|
19
19
|
const exeCtx = this.getExecuteContext();
|
|
20
20
|
const sqlCfgModels = exeCtx.getSqlCfgModels();
|
|
21
|
+
|
|
22
|
+
console.log("CrudProExecuteSqlService===> " + JSON.stringify(sqlCfgModels))
|
|
23
|
+
|
|
21
24
|
for (let i = 0; i < sqlCfgModels.length; i++) {
|
|
22
25
|
const sqlCfgModel = sqlCfgModels[i];
|
|
23
26
|
const checkResult = this.executeSqlCfgModelPreCheck(sqlCfgModel);
|
|
@@ -87,9 +90,45 @@ class CrudProExecuteSqlService extends CrudProServiceBase {
|
|
|
87
90
|
return false;
|
|
88
91
|
}
|
|
89
92
|
|
|
93
|
+
const errorMsg = `禁止执行的SQL: ${sqlCfgModel.executeSql}`;
|
|
94
|
+
|
|
90
95
|
if (KeyOfCrudTypes.NOT_CRUD === crudType) {
|
|
91
|
-
//
|
|
92
|
-
|
|
96
|
+
// 在 isNativeSQL 模式下,允许特定的 DDL 操作(CREATE TABLE、CREATE INDEX 等),但禁止 DROP TABLE
|
|
97
|
+
if (sqlCfgModel.isNativeSQL) {
|
|
98
|
+
// 禁止的 DDL 操作(破坏性操作)
|
|
99
|
+
const forbiddenPatterns = [
|
|
100
|
+
/^\s*DROP\s+TABLE/i,
|
|
101
|
+
/^\s*DROP\s+DATABASE/i,
|
|
102
|
+
/^\s*DROP\s+SCHEMA/i,
|
|
103
|
+
/^\s*TRUNCATE\s+TABLE/i,
|
|
104
|
+
];
|
|
105
|
+
|
|
106
|
+
for (const pattern of forbiddenPatterns) {
|
|
107
|
+
if (pattern.test(sqlCfgModel.executeSql)) {
|
|
108
|
+
throw new CommonException(Exceptions.CFG_NOT_SUPPORT_THE_SQL, errorMsg);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 允许的 DDL 操作白名单
|
|
113
|
+
const allowedPatterns = [
|
|
114
|
+
/^\s*CREATE\s+TABLE/i,
|
|
115
|
+
/^\s*CREATE\s+INDEX/i,
|
|
116
|
+
/^\s*CREATE\s+UNIQUE\s+INDEX/i,
|
|
117
|
+
/^\s*ALTER\s+TABLE/i,
|
|
118
|
+
];
|
|
119
|
+
|
|
120
|
+
const isAllowed = allowedPatterns.some(pattern => pattern.test(sqlCfgModel.executeSql));
|
|
121
|
+
|
|
122
|
+
if (isAllowed) {
|
|
123
|
+
// 允许的 DDL 操作,继续执行后续校验
|
|
124
|
+
} else {
|
|
125
|
+
// 非允许的 DDL 操作,仍然抛出异常
|
|
126
|
+
throw new CommonException(Exceptions.CFG_NOT_SUPPORT_THE_SQL, errorMsg);
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
// 非 isNativeSQL 模式下,只支持增删改查,其他语句不支持。
|
|
130
|
+
throw new CommonException(Exceptions.CFG_NOT_SUPPORT_THE_SQL, errorMsg);
|
|
131
|
+
}
|
|
93
132
|
}
|
|
94
133
|
|
|
95
134
|
// 此SQL不需要执行
|
|
@@ -86,7 +86,7 @@ class CrudProGenSqlCondition {
|
|
|
86
86
|
if (equalsIgnoreCase(KeysOfConditions.$OR, key)) {
|
|
87
87
|
TypeUtils.assertJsonArray(value, Exceptions.REQUEST_OR_PARAM_MUST_ARRAY);
|
|
88
88
|
|
|
89
|
-
const a = this.generateByLogicalOR(value);
|
|
89
|
+
const a = this.generateByLogicalOR(value as IRequestCondition[]);
|
|
90
90
|
if (a != null) {
|
|
91
91
|
tmpSqlList.push(a.sql);
|
|
92
92
|
if (isNotEmpty(a.args)) {
|
|
@@ -99,7 +99,7 @@ class CrudProGenSqlCondition {
|
|
|
99
99
|
if (equalsIgnoreCase(KeysOfConditions.$AND, key)) {
|
|
100
100
|
TypeUtils.assertJsonArray(value, Exceptions.REQUEST_OR_PARAM_MUST_ARRAY);
|
|
101
101
|
|
|
102
|
-
const a = this.generateByLogicalAnd(value);
|
|
102
|
+
const a = this.generateByLogicalAnd(value as IRequestCondition[]);
|
|
103
103
|
if (a != null) {
|
|
104
104
|
tmpSqlList.push(a.sql);
|
|
105
105
|
if (isNotEmpty(a.args)) {
|
|
@@ -114,7 +114,7 @@ class CrudProGenSqlCondition {
|
|
|
114
114
|
tmpArgList.push(value);
|
|
115
115
|
} else {
|
|
116
116
|
// AND
|
|
117
|
-
const a = this.generateByCompareKey(key, value);
|
|
117
|
+
const a = this.generateByCompareKey(key, value as ICompareCondition);
|
|
118
118
|
|
|
119
119
|
if (a != null) {
|
|
120
120
|
tmpSqlList.push(a.sql);
|
|
@@ -209,11 +209,15 @@ class CrudProGenSqlCondition {
|
|
|
209
209
|
let tmpSql = '';
|
|
210
210
|
|
|
211
211
|
if (equalsIgnoreCase(KeysOfConditions.$NULL, compare)) {
|
|
212
|
-
|
|
213
|
-
|
|
212
|
+
if (value0 === true) {
|
|
213
|
+
tmpSql = `${toSqlColumnName(key)} is null `;
|
|
214
|
+
hasNoArgSql = true;
|
|
215
|
+
}
|
|
214
216
|
} else if (equalsIgnoreCase(KeysOfConditions.$NOT_NULL, compare)) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
+
if (value0 === true) {
|
|
218
|
+
tmpSql = `${toSqlColumnName(key)} is not null `;
|
|
219
|
+
hasNoArgSql = true;
|
|
220
|
+
}
|
|
217
221
|
} else if (equalsIgnoreCase(KeysOfConditions.$GT, compare)) {
|
|
218
222
|
tmpArgList.push(value0);
|
|
219
223
|
tmpSql = `${toSqlColumnName(key)} > ?`;
|
|
@@ -84,8 +84,7 @@ class CrudProTableMetaService extends CrudProServiceBase {
|
|
|
84
84
|
|
|
85
85
|
// 3. 更新缓存
|
|
86
86
|
if (!skipCache || refreshCache) {
|
|
87
|
-
|
|
88
|
-
metaCache.setTableInfos(cacheKey, result, tableMetaCacheTime || 3600000);
|
|
87
|
+
metaCache.setTableInfos(cacheKey, result, 1000 * 5);
|
|
89
88
|
}
|
|
90
89
|
|
|
91
90
|
return result;
|