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