nsgm-cli 2.1.35 → 2.1.37
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.
|
@@ -10,7 +10,7 @@ const initialState = {
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
export const templateManageReducer = (state = initialState, { type, payload }) => {
|
|
13
|
-
const
|
|
13
|
+
const template = state.template || { totalCounts: 0, items: [] };
|
|
14
14
|
const { totalCounts, items } = template;
|
|
15
15
|
let newItems: any = [];
|
|
16
16
|
|
|
@@ -3,6 +3,7 @@ import { BaseGenerator } from "./base-generator";
|
|
|
3
3
|
* Resolver生成器
|
|
4
4
|
*/
|
|
5
5
|
export declare class ResolverGenerator extends BaseGenerator {
|
|
6
|
+
private getQuotedTableName;
|
|
6
7
|
generate(): string;
|
|
7
8
|
private generateSearchConditions;
|
|
8
9
|
private generateNewValidationCalls;
|
|
@@ -10,6 +11,7 @@ export declare class ResolverGenerator extends BaseGenerator {
|
|
|
10
11
|
private generateUpdateValidation;
|
|
11
12
|
private generateUpdateValues;
|
|
12
13
|
private generateBatchReturnObject;
|
|
14
|
+
private generateBatchInsertValues;
|
|
13
15
|
private generateDataLoaderSearchLogic;
|
|
14
16
|
private generateNewRecordObject;
|
|
15
17
|
}
|
|
@@ -6,12 +6,22 @@ const base_generator_1 = require("./base-generator");
|
|
|
6
6
|
* Resolver生成器
|
|
7
7
|
*/
|
|
8
8
|
class ResolverGenerator extends base_generator_1.BaseGenerator {
|
|
9
|
+
// 获取带反引号的表名(防止 MySQL 保留关键字冲突)
|
|
10
|
+
getQuotedTableName() {
|
|
11
|
+
return `\`${this.controller}\``;
|
|
12
|
+
}
|
|
9
13
|
generate() {
|
|
10
|
-
|
|
14
|
+
// Variables used in generated template string (prefixed with _ to satisfy ESLint)
|
|
15
|
+
// @ts-ignore - Variable is used in generated template string
|
|
16
|
+
const _selectFields = this.fields.map((f) => f.name).join(", ");
|
|
11
17
|
const insertFields = this.getFormFields();
|
|
12
18
|
const searchableFields = this.getSearchableFields();
|
|
13
|
-
|
|
14
|
-
const
|
|
19
|
+
// @ts-ignore - Variable is used in generated template string
|
|
20
|
+
const _quotedTableName = this.getQuotedTableName();
|
|
21
|
+
// @ts-ignore - Variable is used in generated template string
|
|
22
|
+
const _insertFieldNames = insertFields.map((f) => f.name).join(", ");
|
|
23
|
+
// @ts-ignore - Variable is used in generated template string
|
|
24
|
+
const _insertPlaceholders = insertFields.map(() => "?").join(", ");
|
|
15
25
|
const insertValues = insertFields
|
|
16
26
|
.map((f) => {
|
|
17
27
|
if (f.type === "integer") {
|
|
@@ -21,7 +31,8 @@ class ResolverGenerator extends base_generator_1.BaseGenerator {
|
|
|
21
31
|
})
|
|
22
32
|
.join(", ");
|
|
23
33
|
const searchConditions = this.generateSearchConditions(searchableFields);
|
|
24
|
-
|
|
34
|
+
// @ts-ignore - Variable is used in generated template string
|
|
35
|
+
const _updateFields = insertFields.map((f) => `${f.name} = ?`).join(", ");
|
|
25
36
|
return `const { executeQuery, executePaginatedQuery } = require('../../utils/common')
|
|
26
37
|
const { validateInteger, validatePagination, validateId } = require('../../utils/validation')
|
|
27
38
|
|
|
@@ -30,13 +41,13 @@ module.exports = {
|
|
|
30
41
|
${this.controller}: async ({ page = 0, pageSize = 10 }) => {
|
|
31
42
|
try {
|
|
32
43
|
const { page: validPage, pageSize: validPageSize } = validatePagination(page, pageSize);
|
|
33
|
-
|
|
34
|
-
const sql =
|
|
35
|
-
const countSql =
|
|
44
|
+
|
|
45
|
+
const sql = \`SELECT \${_selectFields} FROM \${_quotedTableName} LIMIT ? OFFSET ?\`;
|
|
46
|
+
const countSql = \`SELECT COUNT(*) as counts FROM \${_quotedTableName}\`;
|
|
36
47
|
const values = [validPageSize, validPage * validPageSize];
|
|
37
48
|
|
|
38
49
|
console.log('执行分页查询:', { sql, values, countSql });
|
|
39
|
-
|
|
50
|
+
|
|
40
51
|
return await executePaginatedQuery(sql, countSql, values);
|
|
41
52
|
} catch (error) {
|
|
42
53
|
console.error('获取${this.controller}列表失败:', error.message);
|
|
@@ -48,16 +59,16 @@ module.exports = {
|
|
|
48
59
|
${this.controller}Get: async ({ id }, context) => {
|
|
49
60
|
try {
|
|
50
61
|
const validId = validateId(id);
|
|
51
|
-
|
|
62
|
+
|
|
52
63
|
console.log('🚀 使用 DataLoader 根据ID查询${this.controller}:', { id: validId });
|
|
53
|
-
|
|
64
|
+
|
|
54
65
|
// 使用 DataLoader 批量加载,自动去重和缓存
|
|
55
66
|
const result = await context.dataloaders.${this.controller}.byId.load(validId);
|
|
56
|
-
|
|
67
|
+
|
|
57
68
|
if (!result) {
|
|
58
69
|
throw new Error(\`ID为 \${validId} 的${this.controller}不存在\`);
|
|
59
70
|
}
|
|
60
|
-
|
|
71
|
+
|
|
61
72
|
return result;
|
|
62
73
|
} catch (error) {
|
|
63
74
|
console.error('获取${this.controller}失败:', error.message);
|
|
@@ -71,15 +82,15 @@ module.exports = {
|
|
|
71
82
|
if (!Array.isArray(ids) || ids.length === 0) {
|
|
72
83
|
throw new Error('ID列表不能为空');
|
|
73
84
|
}
|
|
74
|
-
|
|
85
|
+
|
|
75
86
|
// 验证所有ID
|
|
76
87
|
const validIds = ids.map(id => validateId(id));
|
|
77
|
-
|
|
88
|
+
|
|
78
89
|
console.log('🚀 使用 DataLoader 批量查询${this.controller}:', { ids: validIds });
|
|
79
|
-
|
|
90
|
+
|
|
80
91
|
// DataLoader 自动批量处理,一次查询获取所有数据
|
|
81
92
|
const results = await context.dataloaders.${this.controller}.byId.loadMany(validIds);
|
|
82
|
-
|
|
93
|
+
|
|
83
94
|
// 过滤掉 null 结果(未找到的记录)
|
|
84
95
|
return results.filter(result => result !== null && !(result instanceof Error));
|
|
85
96
|
} catch (error) {
|
|
@@ -92,23 +103,23 @@ module.exports = {
|
|
|
92
103
|
${this.controller}Search: async ({ page = 0, pageSize = 10, data = {} }, context) => {
|
|
93
104
|
try {
|
|
94
105
|
const { page: validPage, pageSize: validPageSize } = validatePagination(page, pageSize);
|
|
95
|
-
|
|
106
|
+
|
|
96
107
|
${this.generateDataLoaderSearchLogic(searchableFields)}
|
|
97
|
-
|
|
108
|
+
|
|
98
109
|
// 原始查询方式(作为备用)
|
|
99
110
|
const values = [];
|
|
100
111
|
const countValues = [];
|
|
101
|
-
|
|
112
|
+
|
|
102
113
|
let whereSql = '';
|
|
103
114
|
${searchConditions}
|
|
104
115
|
|
|
105
|
-
const sql = \`SELECT
|
|
106
|
-
const countSql = \`SELECT COUNT(*) as counts FROM
|
|
107
|
-
|
|
116
|
+
const sql = \`SELECT \${_selectFields} FROM \${_quotedTableName} WHERE 1=1\${whereSql} LIMIT ? OFFSET ?\`;
|
|
117
|
+
const countSql = \`SELECT COUNT(*) as counts FROM \${_quotedTableName} WHERE 1=1\${whereSql}\`;
|
|
118
|
+
|
|
108
119
|
values.push(validPageSize, validPage * validPageSize);
|
|
109
|
-
|
|
120
|
+
|
|
110
121
|
console.log('搜索${this.controller}(备用查询):', { sql, values, countSql, countValues });
|
|
111
|
-
|
|
122
|
+
|
|
112
123
|
return await executePaginatedQuery(sql, countSql, values, countValues);
|
|
113
124
|
} catch (error) {
|
|
114
125
|
console.error('搜索${this.controller}失败:', error.message);
|
|
@@ -120,22 +131,22 @@ ${searchConditions}
|
|
|
120
131
|
${this.controller}Add: async ({ data }, context) => {
|
|
121
132
|
try {
|
|
122
133
|
${this.generateNewValidationCalls(insertFields)}
|
|
123
|
-
|
|
124
|
-
const sql =
|
|
134
|
+
|
|
135
|
+
const sql = \`INSERT INTO \${_quotedTableName} (\${_insertFieldNames}) VALUES (\${_insertPlaceholders})\`;
|
|
125
136
|
const values = [${insertValues}];
|
|
126
|
-
|
|
137
|
+
|
|
127
138
|
console.log('添加${this.controller}:', { sql, values });
|
|
128
|
-
|
|
139
|
+
|
|
129
140
|
const results = await executeQuery(sql, values);
|
|
130
141
|
const insertId = results.insertId;
|
|
131
|
-
|
|
142
|
+
|
|
132
143
|
// 预加载新数据到 DataLoader 缓存
|
|
133
144
|
if (insertId && context?.dataloaders?.${this.controller}) {
|
|
134
145
|
const newRecord = { id: insertId, ${this.generateNewRecordObject(insertFields)} };
|
|
135
146
|
context.dataloaders.${this.controller}.prime(insertId, newRecord);
|
|
136
147
|
console.log('🚀 新${this.controller}已预加载到 DataLoader 缓存:', newRecord);
|
|
137
148
|
}
|
|
138
|
-
|
|
149
|
+
|
|
139
150
|
return insertId;
|
|
140
151
|
} catch (error) {
|
|
141
152
|
console.error('添加${this.controller}失败:', error.message);
|
|
@@ -149,7 +160,7 @@ ${this.generateNewValidationCalls(insertFields)}
|
|
|
149
160
|
if (!Array.isArray(datas) || datas.length === 0) {
|
|
150
161
|
throw new Error('批量添加数据不能为空');
|
|
151
162
|
}
|
|
152
|
-
|
|
163
|
+
|
|
153
164
|
// 验证所有数据并转换
|
|
154
165
|
const validatedDatas = datas.map((data, index) => {
|
|
155
166
|
try {
|
|
@@ -159,10 +170,10 @@ ${this.generateBatchValidation(insertFields)}
|
|
|
159
170
|
throw new Error(\`第 \${index + 1} 条数据验证失败: \${error.message}\`);
|
|
160
171
|
}
|
|
161
172
|
});
|
|
162
|
-
|
|
163
|
-
const placeholders = validatedDatas.map(() =>
|
|
164
|
-
const sql = \`INSERT INTO
|
|
165
|
-
const values = validatedDatas.flatMap(data => [${
|
|
173
|
+
|
|
174
|
+
const placeholders = validatedDatas.map(() => \`(\${_insertPlaceholders})\`).join(',');
|
|
175
|
+
const sql = \`INSERT INTO \${_quotedTableName} (\${_insertFieldNames}) VALUES \${placeholders}\`;
|
|
176
|
+
const values = validatedDatas.flatMap(data => [${this.generateBatchInsertValues(insertFields)}]);
|
|
166
177
|
|
|
167
178
|
console.log('批量添加${this.controller}:', { sql, values });
|
|
168
179
|
|
|
@@ -185,7 +196,7 @@ ${this.generateBatchValidation(insertFields)}
|
|
|
185
196
|
|
|
186
197
|
${this.generateUpdateValidation(insertFields)}
|
|
187
198
|
|
|
188
|
-
const sql =
|
|
199
|
+
const sql = \`UPDATE \${_quotedTableName} SET \${_updateFields} WHERE id = ?\`;
|
|
189
200
|
const values = [${this.generateUpdateValues(insertFields)}, validId];
|
|
190
201
|
|
|
191
202
|
console.log('更新${this.controller}:', { sql, values });
|
|
@@ -214,7 +225,7 @@ ${this.generateUpdateValidation(insertFields)}
|
|
|
214
225
|
try {
|
|
215
226
|
const validId = validateId(id);
|
|
216
227
|
|
|
217
|
-
const sql =
|
|
228
|
+
const sql = \`DELETE FROM \${_quotedTableName} WHERE id = ?\`;
|
|
218
229
|
const values = [validId];
|
|
219
230
|
|
|
220
231
|
console.log('删除${this.controller}:', { sql, values });
|
|
@@ -255,7 +266,7 @@ ${this.generateUpdateValidation(insertFields)}
|
|
|
255
266
|
});
|
|
256
267
|
|
|
257
268
|
const placeholders = validIds.map(() => '?').join(',');
|
|
258
|
-
const sql = \`DELETE FROM
|
|
269
|
+
const sql = \`DELETE FROM \${_quotedTableName} WHERE id IN (\${placeholders})\`;
|
|
259
270
|
|
|
260
271
|
console.log('批量删除${this.controller}:', { sql, values: validIds });
|
|
261
272
|
|
|
@@ -388,9 +399,9 @@ ${this.generateUpdateValidation(insertFields)}
|
|
|
388
399
|
})
|
|
389
400
|
.join(", ");
|
|
390
401
|
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
402
|
+
generateBatchInsertValues(insertFields) {
|
|
403
|
+
return insertFields.map((f) => `data.${f.name}`).join(", ");
|
|
404
|
+
}
|
|
394
405
|
generateDataLoaderSearchLogic(searchableFields) {
|
|
395
406
|
if (searchableFields.length === 0)
|
|
396
407
|
return "";
|