nsgm-cli 2.1.37 → 2.1.39
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.
|
@@ -11,17 +11,12 @@ class ResolverGenerator extends base_generator_1.BaseGenerator {
|
|
|
11
11
|
return `\`${this.controller}\``;
|
|
12
12
|
}
|
|
13
13
|
generate() {
|
|
14
|
-
|
|
15
|
-
// @ts-ignore - Variable is used in generated template string
|
|
16
|
-
const _selectFields = this.fields.map((f) => f.name).join(", ");
|
|
14
|
+
const selectFields = this.fields.map((f) => f.name).join(", ");
|
|
17
15
|
const insertFields = this.getFormFields();
|
|
18
16
|
const searchableFields = this.getSearchableFields();
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
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(", ");
|
|
17
|
+
const quotedTableName = this.getQuotedTableName();
|
|
18
|
+
const insertFieldNames = insertFields.map((f) => f.name).join(", ");
|
|
19
|
+
const insertPlaceholders = insertFields.map(() => "?").join(", ");
|
|
25
20
|
const insertValues = insertFields
|
|
26
21
|
.map((f) => {
|
|
27
22
|
if (f.type === "integer") {
|
|
@@ -31,266 +26,267 @@ class ResolverGenerator extends base_generator_1.BaseGenerator {
|
|
|
31
26
|
})
|
|
32
27
|
.join(", ");
|
|
33
28
|
const searchConditions = this.generateSearchConditions(searchableFields);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const { validateInteger, validatePagination, validateId } = require('../../utils/validation')
|
|
38
|
-
|
|
39
|
-
module.exports = {
|
|
40
|
-
// 获取${this.controller}
|
|
41
|
-
${this.controller}: async ({ page = 0, pageSize = 10 }) => {
|
|
42
|
-
try {
|
|
43
|
-
const { page: validPage, pageSize: validPageSize } = validatePagination(page, pageSize)
|
|
44
|
-
|
|
45
|
-
const sql =
|
|
46
|
-
const countSql =
|
|
47
|
-
const values = [validPageSize, validPage * validPageSize]
|
|
48
|
-
|
|
49
|
-
console.log('执行分页查询:', { sql, values, countSql })
|
|
50
|
-
|
|
51
|
-
return await executePaginatedQuery(sql, countSql, values)
|
|
52
|
-
} catch (error) {
|
|
53
|
-
console.error('获取${this.controller}列表失败:', error.message)
|
|
54
|
-
throw error
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 根据ID获取${this.controller} - 使用 DataLoader
|
|
59
|
-
${this.controller}Get: async ({ id }, context) => {
|
|
60
|
-
try {
|
|
61
|
-
const validId = validateId(id)
|
|
62
|
-
|
|
63
|
-
console.log('🚀 使用 DataLoader 根据ID查询${this.controller}:', { id: validId })
|
|
64
|
-
|
|
65
|
-
// 使用 DataLoader
|
|
66
|
-
const result = await context.dataloaders.${this.controller}.byId.load(validId)
|
|
67
|
-
|
|
68
|
-
if (!result) {
|
|
69
|
-
throw new Error(\`ID为 \${validId} 的${this.controller}不存在\`)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return result
|
|
73
|
-
} catch (error) {
|
|
74
|
-
console.error('获取${this.controller}失败:', error.message)
|
|
75
|
-
throw error
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// 批量获取${this.controller} - 新增方法,展示 DataLoader
|
|
80
|
-
${this.controller}BatchGet: async ({ ids }, context) => {
|
|
81
|
-
try {
|
|
82
|
-
if (!Array.isArray(ids) || ids.length === 0) {
|
|
83
|
-
throw new Error('ID列表不能为空')
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// 验证所有ID
|
|
87
|
-
const validIds = ids.map(id => validateId(id))
|
|
88
|
-
|
|
89
|
-
console.log('🚀 使用 DataLoader 批量查询${this.controller}:', { ids: validIds })
|
|
90
|
-
|
|
91
|
-
// DataLoader
|
|
92
|
-
const results = await context.dataloaders.${this.controller}.byId.loadMany(validIds)
|
|
93
|
-
|
|
94
|
-
// 过滤掉 null
|
|
95
|
-
return results.filter(result => result !== null && !(result instanceof Error))
|
|
96
|
-
} catch (error) {
|
|
97
|
-
console.error('批量获取${this.controller}失败:', error.message)
|
|
98
|
-
throw error
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// 搜索${this.controller}(分页)- 使用 DataLoader
|
|
103
|
-
${this.controller}Search: async ({ page = 0, pageSize = 10, data = {} }, context) => {
|
|
104
|
-
try {
|
|
105
|
-
const { page: validPage, pageSize: validPageSize } = validatePagination(page, pageSize)
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
//
|
|
110
|
-
const values = []
|
|
111
|
-
const countValues = []
|
|
112
|
-
|
|
113
|
-
let whereSql = ''
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const sql =
|
|
117
|
-
const countSql =
|
|
118
|
-
|
|
119
|
-
values.push(validPageSize, validPage * validPageSize)
|
|
120
|
-
|
|
121
|
-
console.log('搜索${this.controller}(备用查询):', { sql, values, countSql, countValues })
|
|
122
|
-
|
|
123
|
-
return await executePaginatedQuery(sql, countSql, values, countValues)
|
|
124
|
-
} catch (error) {
|
|
125
|
-
console.error('搜索${this.controller}失败:', error.message)
|
|
126
|
-
throw error
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// 添加${this.controller} - 添加 DataLoader
|
|
131
|
-
${this.controller}Add: async ({ data }, context) => {
|
|
132
|
-
try {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const sql =
|
|
136
|
-
const values = [${insertValues}]
|
|
137
|
-
|
|
138
|
-
console.log('添加${this.controller}:', { sql, values })
|
|
139
|
-
|
|
140
|
-
const results = await executeQuery(sql, values)
|
|
141
|
-
const insertId = results.insertId
|
|
142
|
-
|
|
143
|
-
// 预加载新数据到 DataLoader
|
|
144
|
-
if (insertId && context?.dataloaders?.${this.controller}) {
|
|
145
|
-
const newRecord = { id: insertId, ${this.generateNewRecordObject(insertFields)} }
|
|
146
|
-
context.dataloaders.${this.controller}.prime(insertId, newRecord)
|
|
147
|
-
console.log('🚀 新${this.controller}已预加载到 DataLoader 缓存:', newRecord)
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return insertId
|
|
151
|
-
} catch (error) {
|
|
152
|
-
console.error('添加${this.controller}失败:', error.message)
|
|
153
|
-
throw error
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// 批量添加${this.controller}
|
|
158
|
-
${this.controller}BatchAdd: async ({ datas }) => {
|
|
159
|
-
try {
|
|
160
|
-
if (!Array.isArray(datas) || datas.length === 0) {
|
|
161
|
-
throw new Error('批量添加数据不能为空')
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
//
|
|
165
|
-
const validatedDatas = datas.map((data, index) => {
|
|
166
|
-
try {
|
|
167
|
-
|
|
168
|
-
return { ${this.generateBatchReturnObject(insertFields)} }
|
|
169
|
-
} catch (error) {
|
|
170
|
-
throw new Error(\`第 \${index + 1} 条数据验证失败: \${error.message}\`)
|
|
171
|
-
}
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
const placeholders = validatedDatas.map(() =>
|
|
175
|
-
const sql =
|
|
176
|
-
const values = validatedDatas.flatMap(data => [${this.generateBatchInsertValues(insertFields)}])
|
|
177
|
-
|
|
178
|
-
console.log('批量添加${this.controller}:', { sql, values })
|
|
179
|
-
|
|
180
|
-
const results = await executeQuery(sql, values)
|
|
181
|
-
return results.insertId
|
|
182
|
-
} catch (error) {
|
|
183
|
-
console.error('批量添加${this.controller}失败:', error.message)
|
|
184
|
-
throw error
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// 更新${this.controller} - 添加 DataLoader
|
|
189
|
-
${this.controller}Update: async ({ id, data }, context) => {
|
|
190
|
-
try {
|
|
191
|
-
const validId = validateId(id)
|
|
192
|
-
|
|
193
|
-
if (!data) {
|
|
194
|
-
throw new Error('更新数据不能为空')
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
const sql =
|
|
200
|
-
const values = [${this.generateUpdateValues(insertFields)}, validId]
|
|
201
|
-
|
|
202
|
-
console.log('更新${this.controller}:', { sql, values })
|
|
203
|
-
|
|
204
|
-
const results = await executeQuery(sql, values)
|
|
205
|
-
|
|
206
|
-
if (results.affectedRows === 0) {
|
|
207
|
-
throw new Error(\`ID为 \${validId} 的${this.controller}不存在\`)
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// 清除 DataLoader
|
|
211
|
-
if (context?.dataloaders?.${this.controller}) {
|
|
212
|
-
context.dataloaders.${this.controller}.clearById(validId)
|
|
213
|
-
console.log('🧹 已清除 DataLoader 缓存:', { id: validId })
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
return true
|
|
217
|
-
} catch (error) {
|
|
218
|
-
console.error('更新${this.controller}失败:', error.message)
|
|
219
|
-
throw error
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// 删除${this.controller} - 添加 DataLoader
|
|
224
|
-
${this.controller}Delete: async ({ id }, context) => {
|
|
225
|
-
try {
|
|
226
|
-
const validId = validateId(id)
|
|
227
|
-
|
|
228
|
-
const sql =
|
|
229
|
-
const values = [validId]
|
|
230
|
-
|
|
231
|
-
console.log('删除${this.controller}:', { sql, values })
|
|
232
|
-
|
|
233
|
-
const results = await executeQuery(sql, values)
|
|
234
|
-
|
|
235
|
-
if (results.affectedRows === 0) {
|
|
236
|
-
throw new Error(\`ID为 \${validId} 的${this.controller}不存在\`)
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// 清除 DataLoader
|
|
240
|
-
if (context?.dataloaders?.${this.controller}) {
|
|
241
|
-
context.dataloaders.${this.controller}.clearById(validId)
|
|
242
|
-
console.log('🧹 已清除 DataLoader 缓存:', { id: validId })
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
return true
|
|
246
|
-
} catch (error) {
|
|
247
|
-
console.error('删除${this.controller}失败:', error.message)
|
|
248
|
-
throw error
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// 批量删除${this.controller} - 添加 DataLoader
|
|
253
|
-
${this.controller}BatchDelete: async ({ ids }, context) => {
|
|
254
|
-
try {
|
|
255
|
-
if (!Array.isArray(ids) || ids.length === 0) {
|
|
256
|
-
throw new Error('批量删除的ID列表不能为空')
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
// 验证所有ID
|
|
260
|
-
const validIds = ids.map((id, index) => {
|
|
261
|
-
try {
|
|
262
|
-
return validateId(id, \`第\${index + 1}个ID\`)
|
|
263
|
-
} catch (error) {
|
|
264
|
-
throw new Error(\`第 \${index + 1} 个ID验证失败: \${error.message}\`)
|
|
265
|
-
}
|
|
266
|
-
})
|
|
267
|
-
|
|
268
|
-
const placeholders = validIds.map(() => '?').join(',')
|
|
269
|
-
const sql =
|
|
270
|
-
|
|
271
|
-
console.log('批量删除${this.controller}:', { sql, values: validIds })
|
|
272
|
-
|
|
273
|
-
const results = await executeQuery(sql, validIds)
|
|
274
|
-
|
|
275
|
-
if (results.affectedRows === 0) {
|
|
276
|
-
throw new Error('没有找到要删除的${this.controller}')
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
// 批量清除 DataLoader
|
|
280
|
-
if (context?.dataloaders?.${this.controller}) {
|
|
281
|
-
validIds.forEach(id => {
|
|
282
|
-
context.dataloaders.${this.controller}.clearById(id)
|
|
283
|
-
})
|
|
284
|
-
console.log('🧹 已批量清除 DataLoader 缓存:', { ids: validIds })
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return true
|
|
288
|
-
} catch (error) {
|
|
289
|
-
console.error('批量删除${this.controller}失败:', error.message)
|
|
290
|
-
throw error
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
}
|
|
29
|
+
const updateFields = insertFields.map((f) => `${f.name} = ?`).join(", ");
|
|
30
|
+
return [
|
|
31
|
+
"const { executeQuery, executePaginatedQuery } = require('../../utils/common')",
|
|
32
|
+
"const { validateInteger, validatePagination, validateId } = require('../../utils/validation')",
|
|
33
|
+
"",
|
|
34
|
+
"module.exports = {",
|
|
35
|
+
` // 获取${this.controller}列表(分页)`,
|
|
36
|
+
` ${this.controller}: async ({ page = 0, pageSize = 10 }) => {`,
|
|
37
|
+
` try {`,
|
|
38
|
+
` const { page: validPage, pageSize: validPageSize } = validatePagination(page, pageSize);`,
|
|
39
|
+
``,
|
|
40
|
+
` const sql = 'SELECT ${selectFields} FROM ${quotedTableName} LIMIT ? OFFSET ?';`,
|
|
41
|
+
` const countSql = 'SELECT COUNT(*) as counts FROM ${quotedTableName}';`,
|
|
42
|
+
` const values = [validPageSize, validPage * validPageSize];`,
|
|
43
|
+
``,
|
|
44
|
+
` console.log('执行分页查询:', { sql, values, countSql });`,
|
|
45
|
+
``,
|
|
46
|
+
` return await executePaginatedQuery(sql, countSql, values);`,
|
|
47
|
+
` } catch (error) {`,
|
|
48
|
+
` console.error('获取${this.controller}列表失败:', error.message);`,
|
|
49
|
+
` throw error;`,
|
|
50
|
+
` }`,
|
|
51
|
+
` },`,
|
|
52
|
+
``,
|
|
53
|
+
` // 根据ID获取${this.controller} - 使用 DataLoader 优化`,
|
|
54
|
+
` ${this.controller}Get: async ({ id }, context) => {`,
|
|
55
|
+
` try {`,
|
|
56
|
+
` const validId = validateId(id);`,
|
|
57
|
+
``,
|
|
58
|
+
` console.log('🚀 使用 DataLoader 根据ID查询${this.controller}:', { id: validId });`,
|
|
59
|
+
``,
|
|
60
|
+
` // 使用 DataLoader 批量加载,自动去重和缓存`,
|
|
61
|
+
` const result = await context.dataloaders.${this.controller}.byId.load(validId);`,
|
|
62
|
+
``,
|
|
63
|
+
` if (!result) {`,
|
|
64
|
+
` throw new Error(\`ID为 \${validId} 的${this.controller}不存在\`);`,
|
|
65
|
+
` }`,
|
|
66
|
+
``,
|
|
67
|
+
` return result;`,
|
|
68
|
+
` } catch (error) {`,
|
|
69
|
+
` console.error('获取${this.controller}失败:', error.message);`,
|
|
70
|
+
` throw error;`,
|
|
71
|
+
` }`,
|
|
72
|
+
` },`,
|
|
73
|
+
``,
|
|
74
|
+
` // 批量获取${this.controller} - 新增方法,展示 DataLoader 批量能力`,
|
|
75
|
+
` ${this.controller}BatchGet: async ({ ids }, context) => {`,
|
|
76
|
+
` try {`,
|
|
77
|
+
` if (!Array.isArray(ids) || ids.length === 0) {`,
|
|
78
|
+
` throw new Error('ID列表不能为空');`,
|
|
79
|
+
` }`,
|
|
80
|
+
``,
|
|
81
|
+
` // 验证所有ID`,
|
|
82
|
+
` const validIds = ids.map(id => validateId(id));`,
|
|
83
|
+
``,
|
|
84
|
+
` console.log('🚀 使用 DataLoader 批量查询${this.controller}:', { ids: validIds });`,
|
|
85
|
+
``,
|
|
86
|
+
` // DataLoader 自动批量处理,一次查询获取所有数据`,
|
|
87
|
+
` const results = await context.dataloaders.${this.controller}.byId.loadMany(validIds);`,
|
|
88
|
+
``,
|
|
89
|
+
` // 过滤掉 null 结果(未找到的记录)`,
|
|
90
|
+
` return results.filter(result => result !== null && !(result instanceof Error));`,
|
|
91
|
+
` } catch (error) {`,
|
|
92
|
+
` console.error('批量获取${this.controller}失败:', error.message);`,
|
|
93
|
+
` throw error;`,
|
|
94
|
+
` }`,
|
|
95
|
+
` },`,
|
|
96
|
+
``,
|
|
97
|
+
` // 搜索${this.controller}(分页)- 使用 DataLoader 优化搜索`,
|
|
98
|
+
` ${this.controller}Search: async ({ page = 0, pageSize = 10, data = {} }, context) => {`,
|
|
99
|
+
` try {`,
|
|
100
|
+
` const { page: validPage, pageSize: validPageSize } = validatePagination(page, pageSize);`,
|
|
101
|
+
``,
|
|
102
|
+
this.generateDataLoaderSearchLogic(searchableFields),
|
|
103
|
+
``,
|
|
104
|
+
` // 原始查询方式(作为备用)`,
|
|
105
|
+
` const values = [];`,
|
|
106
|
+
` const countValues = [];`,
|
|
107
|
+
``,
|
|
108
|
+
` let whereSql = '';`,
|
|
109
|
+
searchConditions,
|
|
110
|
+
``,
|
|
111
|
+
` const sql = 'SELECT ${selectFields} FROM ${quotedTableName} WHERE 1=1' + whereSql + ' LIMIT ? OFFSET ?';`,
|
|
112
|
+
` const countSql = 'SELECT COUNT(*) as counts FROM ${quotedTableName} WHERE 1=1' + whereSql;`,
|
|
113
|
+
``,
|
|
114
|
+
` values.push(validPageSize, validPage * validPageSize);`,
|
|
115
|
+
``,
|
|
116
|
+
` console.log('搜索${this.controller}(备用查询):', { sql, values, countSql, countValues });`,
|
|
117
|
+
``,
|
|
118
|
+
` return await executePaginatedQuery(sql, countSql, values, countValues);`,
|
|
119
|
+
` } catch (error) {`,
|
|
120
|
+
` console.error('搜索${this.controller}失败:', error.message);`,
|
|
121
|
+
` throw error;`,
|
|
122
|
+
` }`,
|
|
123
|
+
` },`,
|
|
124
|
+
``,
|
|
125
|
+
` // 添加${this.controller} - 添加 DataLoader 缓存预加载`,
|
|
126
|
+
` ${this.controller}Add: async ({ data }, context) => {`,
|
|
127
|
+
` try {`,
|
|
128
|
+
this.generateNewValidationCalls(insertFields),
|
|
129
|
+
``,
|
|
130
|
+
` const sql = 'INSERT INTO ${quotedTableName} (${insertFieldNames}) VALUES (${insertPlaceholders})';`,
|
|
131
|
+
` const values = [${insertValues}];`,
|
|
132
|
+
``,
|
|
133
|
+
` console.log('添加${this.controller}:', { sql, values });`,
|
|
134
|
+
``,
|
|
135
|
+
` const results = await executeQuery(sql, values);`,
|
|
136
|
+
` const insertId = results.insertId;`,
|
|
137
|
+
``,
|
|
138
|
+
` // 预加载新数据到 DataLoader 缓存`,
|
|
139
|
+
` if (insertId && context?.dataloaders?.${this.controller}) {`,
|
|
140
|
+
` const newRecord = { id: insertId, ${this.generateNewRecordObject(insertFields)} };`,
|
|
141
|
+
` context.dataloaders.${this.controller}.prime(insertId, newRecord);`,
|
|
142
|
+
` console.log('🚀 新${this.controller}已预加载到 DataLoader 缓存:', newRecord);`,
|
|
143
|
+
` }`,
|
|
144
|
+
``,
|
|
145
|
+
` return insertId;`,
|
|
146
|
+
` } catch (error) {`,
|
|
147
|
+
` console.error('添加${this.controller}失败:', error.message);`,
|
|
148
|
+
` throw error;`,
|
|
149
|
+
` }`,
|
|
150
|
+
` },`,
|
|
151
|
+
``,
|
|
152
|
+
` // 批量添加${this.controller}`,
|
|
153
|
+
` ${this.controller}BatchAdd: async ({ datas }) => {`,
|
|
154
|
+
` try {`,
|
|
155
|
+
` if (!Array.isArray(datas) || datas.length === 0) {`,
|
|
156
|
+
` throw new Error('批量添加数据不能为空');`,
|
|
157
|
+
` }`,
|
|
158
|
+
``,
|
|
159
|
+
` // 验证所有数据并转换`,
|
|
160
|
+
` const validatedDatas = datas.map((data, index) => {`,
|
|
161
|
+
` try {`,
|
|
162
|
+
this.generateBatchValidation(insertFields),
|
|
163
|
+
` return { ${this.generateBatchReturnObject(insertFields)} };`,
|
|
164
|
+
` } catch (error) {`,
|
|
165
|
+
` throw new Error(\`第 \${index + 1} 条数据验证失败: \${error.message}\`);`,
|
|
166
|
+
` }`,
|
|
167
|
+
` });`,
|
|
168
|
+
``,
|
|
169
|
+
` const placeholders = validatedDatas.map(() => '(${insertPlaceholders})').join(',');`,
|
|
170
|
+
` const sql = 'INSERT INTO ${quotedTableName} (${insertFieldNames}) VALUES ' + placeholders;`,
|
|
171
|
+
` const values = validatedDatas.flatMap(data => [${this.generateBatchInsertValues(insertFields)}]);`,
|
|
172
|
+
` `,
|
|
173
|
+
` console.log('批量添加${this.controller}:', { sql, values });`,
|
|
174
|
+
` `,
|
|
175
|
+
` const results = await executeQuery(sql, values);`,
|
|
176
|
+
` return results.insertId;`,
|
|
177
|
+
` } catch (error) {`,
|
|
178
|
+
` console.error('批量添加${this.controller}失败:', error.message);`,
|
|
179
|
+
` throw error;`,
|
|
180
|
+
` }`,
|
|
181
|
+
` },`,
|
|
182
|
+
``,
|
|
183
|
+
` // 更新${this.controller} - 添加 DataLoader 缓存清理`,
|
|
184
|
+
` ${this.controller}Update: async ({ id, data }, context) => {`,
|
|
185
|
+
` try {`,
|
|
186
|
+
` const validId = validateId(id);`,
|
|
187
|
+
` `,
|
|
188
|
+
` if (!data) {`,
|
|
189
|
+
` throw new Error('更新数据不能为空');`,
|
|
190
|
+
` }`,
|
|
191
|
+
` `,
|
|
192
|
+
this.generateUpdateValidation(insertFields),
|
|
193
|
+
` `,
|
|
194
|
+
` const sql = 'UPDATE ${quotedTableName} SET ${updateFields} WHERE id = ?';`,
|
|
195
|
+
` const values = [${this.generateUpdateValues(insertFields)}, validId];`,
|
|
196
|
+
` `,
|
|
197
|
+
` console.log('更新${this.controller}:', { sql, values });`,
|
|
198
|
+
` `,
|
|
199
|
+
` const results = await executeQuery(sql, values);`,
|
|
200
|
+
` `,
|
|
201
|
+
` if (results.affectedRows === 0) {`,
|
|
202
|
+
` throw new Error(\`ID为 \${validId} 的${this.controller}不存在\`);`,
|
|
203
|
+
` }`,
|
|
204
|
+
` `,
|
|
205
|
+
` // 清除 DataLoader 缓存,确保下次查询获取最新数据`,
|
|
206
|
+
` if (context?.dataloaders?.${this.controller}) {`,
|
|
207
|
+
` context.dataloaders.${this.controller}.clearById(validId);`,
|
|
208
|
+
` console.log('🧹 已清除 DataLoader 缓存:', { id: validId });`,
|
|
209
|
+
` }`,
|
|
210
|
+
` `,
|
|
211
|
+
` return true;`,
|
|
212
|
+
` } catch (error) {`,
|
|
213
|
+
` console.error('更新${this.controller}失败:', error.message);`,
|
|
214
|
+
` throw error;`,
|
|
215
|
+
` }`,
|
|
216
|
+
` },`,
|
|
217
|
+
``,
|
|
218
|
+
` // 删除${this.controller} - 添加 DataLoader 缓存清理`,
|
|
219
|
+
` ${this.controller}Delete: async ({ id }, context) => {`,
|
|
220
|
+
` try {`,
|
|
221
|
+
` const validId = validateId(id);`,
|
|
222
|
+
` `,
|
|
223
|
+
` const sql = 'DELETE FROM ${quotedTableName} WHERE id = ?';`,
|
|
224
|
+
` const values = [validId];`,
|
|
225
|
+
` `,
|
|
226
|
+
` console.log('删除${this.controller}:', { sql, values });`,
|
|
227
|
+
` `,
|
|
228
|
+
` const results = await executeQuery(sql, values);`,
|
|
229
|
+
` `,
|
|
230
|
+
` if (results.affectedRows === 0) {`,
|
|
231
|
+
` throw new Error(\`ID为 \${validId} 的${this.controller}不存在\`);`,
|
|
232
|
+
` }`,
|
|
233
|
+
` `,
|
|
234
|
+
` // 清除 DataLoader 缓存`,
|
|
235
|
+
` if (context?.dataloaders?.${this.controller}) {`,
|
|
236
|
+
` context.dataloaders.${this.controller}.clearById(validId);`,
|
|
237
|
+
` console.log('🧹 已清除 DataLoader 缓存:', { id: validId });`,
|
|
238
|
+
` }`,
|
|
239
|
+
` `,
|
|
240
|
+
` return true;`,
|
|
241
|
+
` } catch (error) {`,
|
|
242
|
+
` console.error('删除${this.controller}失败:', error.message);`,
|
|
243
|
+
` throw error;`,
|
|
244
|
+
` }`,
|
|
245
|
+
` },`,
|
|
246
|
+
``,
|
|
247
|
+
` // 批量删除${this.controller} - 添加 DataLoader 缓存清理`,
|
|
248
|
+
` ${this.controller}BatchDelete: async ({ ids }, context) => {`,
|
|
249
|
+
` try {`,
|
|
250
|
+
` if (!Array.isArray(ids) || ids.length === 0) {`,
|
|
251
|
+
` throw new Error('批量删除的ID列表不能为空');`,
|
|
252
|
+
` }`,
|
|
253
|
+
` `,
|
|
254
|
+
` // 验证所有ID`,
|
|
255
|
+
` const validIds = ids.map((id, index) => {`,
|
|
256
|
+
` try {`,
|
|
257
|
+
` return validateId(id, \`第\${index + 1}个ID\`);`,
|
|
258
|
+
` } catch (error) {`,
|
|
259
|
+
` throw new Error(\`第 \${index + 1} 个ID验证失败: \${error.message}\`);`,
|
|
260
|
+
` }`,
|
|
261
|
+
` });`,
|
|
262
|
+
` `,
|
|
263
|
+
` const placeholders = validIds.map(() => '?').join(',');`,
|
|
264
|
+
` const sql = 'DELETE FROM ${quotedTableName} WHERE id IN (' + placeholders + ')';`,
|
|
265
|
+
` `,
|
|
266
|
+
` console.log('批量删除${this.controller}:', { sql, values: validIds });`,
|
|
267
|
+
` `,
|
|
268
|
+
` const results = await executeQuery(sql, validIds);`,
|
|
269
|
+
` `,
|
|
270
|
+
` if (results.affectedRows === 0) {`,
|
|
271
|
+
` throw new Error('没有找到要删除的${this.controller}');`,
|
|
272
|
+
` }`,
|
|
273
|
+
` `,
|
|
274
|
+
` // 批量清除 DataLoader 缓存`,
|
|
275
|
+
` if (context?.dataloaders?.${this.controller}) {`,
|
|
276
|
+
` validIds.forEach(id => {`,
|
|
277
|
+
` context.dataloaders.${this.controller}.clearById(id);`,
|
|
278
|
+
` });`,
|
|
279
|
+
` console.log('🧹 已批量清除 DataLoader 缓存:', { ids: validIds });`,
|
|
280
|
+
` }`,
|
|
281
|
+
` `,
|
|
282
|
+
` return true;`,
|
|
283
|
+
` } catch (error) {`,
|
|
284
|
+
` console.error('批量删除${this.controller}失败:', error.message);`,
|
|
285
|
+
` throw error;`,
|
|
286
|
+
` }`,
|
|
287
|
+
` }`,
|
|
288
|
+
`};`,
|
|
289
|
+
].join("\n");
|
|
294
290
|
}
|
|
295
291
|
generateSearchConditions(searchableFields) {
|
|
296
292
|
if (searchableFields.length === 0)
|