befly 3.7.4 → 3.7.5
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/lib/dbHelper.ts +39 -18
- package/package.json +2 -2
package/lib/dbHelper.ts
CHANGED
|
@@ -89,8 +89,8 @@ export class DbHelper {
|
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
// 2. 缓存未命中,查询数据库
|
|
92
|
-
const sql =
|
|
93
|
-
const result = await this.executeWithConn(sql
|
|
92
|
+
const sql = `SHOW COLUMNS FROM \`${table}\``;
|
|
93
|
+
const result = await this.executeWithConn(sql);
|
|
94
94
|
|
|
95
95
|
if (!result || result.length === 0) {
|
|
96
96
|
throw new Error(`表 ${table} 不存在或没有字段`);
|
|
@@ -295,7 +295,7 @@ export class DbHelper {
|
|
|
295
295
|
}
|
|
296
296
|
|
|
297
297
|
/**
|
|
298
|
-
* 执行 SQL(使用 sql.unsafe
|
|
298
|
+
* 执行 SQL(使用 sql.unsafe,带慢查询日志和错误处理)
|
|
299
299
|
*/
|
|
300
300
|
private async executeWithConn(sqlStr: string, params?: any[]): Promise<any> {
|
|
301
301
|
if (!this.sql) {
|
|
@@ -305,24 +305,45 @@ export class DbHelper {
|
|
|
305
305
|
// 记录开始时间
|
|
306
306
|
const startTime = Date.now();
|
|
307
307
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
308
|
+
try {
|
|
309
|
+
// 使用 sql.unsafe 执行查询
|
|
310
|
+
let result;
|
|
311
|
+
if (params && params.length > 0) {
|
|
312
|
+
result = await this.sql.unsafe(sqlStr, params);
|
|
313
|
+
} else {
|
|
314
|
+
result = await this.sql.unsafe(sqlStr);
|
|
315
|
+
}
|
|
315
316
|
|
|
316
|
-
|
|
317
|
-
|
|
317
|
+
// 计算执行时间
|
|
318
|
+
const duration = Date.now() - startTime;
|
|
318
319
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
320
|
+
// 慢查询警告(超过 1000ms)
|
|
321
|
+
if (duration > 1000) {
|
|
322
|
+
const sqlPreview = sqlStr.length > 100 ? sqlStr.substring(0, 100) + '...' : sqlStr;
|
|
323
|
+
Logger.warn(`🐌 检测到慢查询 (${duration}ms): ${sqlPreview}`);
|
|
324
|
+
}
|
|
324
325
|
|
|
325
|
-
|
|
326
|
+
return result;
|
|
327
|
+
} catch (error: any) {
|
|
328
|
+
const duration = Date.now() - startTime;
|
|
329
|
+
const truncatedSql = sqlStr.length > 200 ? sqlStr.substring(0, 200) + '...' : sqlStr;
|
|
330
|
+
|
|
331
|
+
Logger.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
332
|
+
Logger.error('SQL 执行错误');
|
|
333
|
+
Logger.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
334
|
+
Logger.error(`SQL 语句: ${truncatedSql}`);
|
|
335
|
+
Logger.error(`参数列表: ${JSON.stringify(params || [])}`);
|
|
336
|
+
Logger.error(`执行耗时: ${duration}ms`);
|
|
337
|
+
Logger.error(`错误信息: ${error.message}`);
|
|
338
|
+
Logger.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
339
|
+
|
|
340
|
+
const enhancedError: any = new Error(`SQL执行失败: ${error.message}`);
|
|
341
|
+
enhancedError.originalError = error;
|
|
342
|
+
enhancedError.sql = sqlStr;
|
|
343
|
+
enhancedError.params = params || [];
|
|
344
|
+
enhancedError.duration = duration;
|
|
345
|
+
throw enhancedError;
|
|
346
|
+
}
|
|
326
347
|
}
|
|
327
348
|
|
|
328
349
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly",
|
|
3
|
-
"version": "3.7.
|
|
3
|
+
"version": "3.7.5",
|
|
4
4
|
"description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -69,5 +69,5 @@
|
|
|
69
69
|
"es-toolkit": "^1.41.0",
|
|
70
70
|
"pathe": "^2.0.3"
|
|
71
71
|
},
|
|
72
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "389f7e3620a48a4fc7730519c05ab5b85b2c4179"
|
|
73
73
|
}
|