baja-lite 1.8.3 → 1.8.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.
Files changed (3) hide show
  1. package/package.json +2 -2
  2. package/sql.d.ts +1 -1
  3. package/sql.js +16 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baja-lite",
3
- "version": "1.8.3",
3
+ "version": "1.8.5",
4
4
  "description": "some util for self",
5
5
  "homepage": "https://github.com/void-soul/baja-lite",
6
6
  "repository": {
@@ -43,7 +43,7 @@
43
43
  "pino": "10.3.1",
44
44
  "pino-pretty": "13.1.3",
45
45
  "reflect-metadata": "0.2.2",
46
- "sql-formatter": "15.7.3",
46
+ "sql-formatter": "15.8.0",
47
47
  "sqlstring": "2.3.3",
48
48
  "tslib": "2.8.1"
49
49
  },
package/sql.d.ts CHANGED
@@ -2139,7 +2139,7 @@ export declare function clearMethodCache(key: string): Promise<void>;
2139
2139
  */
2140
2140
  export declare function excuteWithCache<T>(config: {
2141
2141
  /** 返回缓存key,参数=方法的参数+当前用户对象,可以用来清空缓存。 */
2142
- key: string;
2142
+ key: ((...args: any[]) => string) | string;
2143
2143
  /** 返回缓存清除key,参数=方法的参数+当前用户对象,可以用来批量清空缓存 */
2144
2144
  clearKey?: string[];
2145
2145
  /** 自动清空缓存的时间,单位分钟 */
package/sql.js CHANGED
@@ -18,7 +18,7 @@ import pino from 'pino';
18
18
  import { formatDialect, mysql, postgresql, sqlite } from 'sql-formatter';
19
19
  import tslib from 'tslib';
20
20
  import { convert } from './convert-xml.js';
21
- import { DatabaseError, Throw } from './error.js';
21
+ import { Throw } from './error.js';
22
22
  import { excuteSplit, ExcuteSplitMode, sleep } from './fn.js';
23
23
  import { add, calc, ten2Any } from './math.js';
24
24
  import { C2P, C2P2, P2C } from './object.js';
@@ -248,9 +248,8 @@ class MysqlConnection {
248
248
  resolve({ affectedRows: result.affectedRows, insertId: result.insertId });
249
249
  }
250
250
  catch (error) {
251
- const dbError = DatabaseError.query('MySQL execute failed', sql, params, error);
252
- globalThis[_LoggerService].error(dbError.getSafeMessage(), { cause: error });
253
- reject(dbError);
251
+ globalThis[_LoggerService].error(error.message, { cause: error });
252
+ reject(error);
254
253
  }
255
254
  });
256
255
  }
@@ -443,9 +442,8 @@ export class Mysql {
443
442
  resolve(new MysqlConnection(connection));
444
443
  }
445
444
  catch (error) {
446
- const dbError = DatabaseError.connection('Failed to create MySQL connection', error);
447
- globalThis[_LoggerService].error(dbError.getSafeMessage(), { cause: error });
448
- reject(dbError);
445
+ globalThis[_LoggerService].error(error.message, { cause: error });
446
+ reject(error);
449
447
  }
450
448
  });
451
449
  }
@@ -484,9 +482,8 @@ export class Mysql {
484
482
  await conn[_daoConnection].rollback();
485
483
  globalThis[_LoggerService].debug?.('rollback end!');
486
484
  }
487
- const dbError = error instanceof DatabaseError ? error : DatabaseError.transaction('MySQL transaction failed', error);
488
- globalThis[_LoggerService].error(dbError.getSafeMessage(), { cause: error });
489
- reject(dbError);
485
+ globalThis[_LoggerService].error(error.message, { cause: error });
486
+ reject(error);
490
487
  }
491
488
  finally {
492
489
  try {
@@ -802,9 +799,8 @@ export class Postgresql {
802
799
  await conn[_daoConnection].query('ROLLBACK');
803
800
  globalThis[_LoggerService].debug?.('rollback end!');
804
801
  }
805
- const dbError = error instanceof DatabaseError ? error : DatabaseError.transaction('PostgreSQL transaction failed', error);
806
- globalThis[_LoggerService].error(dbError.getSafeMessage(), { cause: error });
807
- reject(dbError);
802
+ globalThis[_LoggerService].error(error.message, { cause: error });
803
+ reject(error);
808
804
  }
809
805
  finally {
810
806
  try {
@@ -5166,7 +5162,7 @@ export async function GetRedisLock(key, lockMaxActive) {
5166
5162
  ;
5167
5163
  /** 对FN加锁、缓存执行 */
5168
5164
  export async function excuteWithLock(config, fn__) {
5169
- const key = `[lock]${typeof config.key === 'function' ? config.key() : config.key}`;
5165
+ const key = config.key_real ? `[lock]${config.key_real}` : `[lock]${typeof config.key === 'function' ? config.key() : config.key}`;
5170
5166
  const db = getRedisDB();
5171
5167
  let wait_time = 0;
5172
5168
  const fn = async () => {
@@ -5202,7 +5198,7 @@ export function MethodLock(config) {
5202
5198
  return function (target, _propertyKey, descriptor) {
5203
5199
  const fn__ = descriptor.value;
5204
5200
  descriptor.value = async function (...args) {
5205
- config.key = typeof config.key === 'function' ? config.key.call(target, ...args) : config.key;
5201
+ config.key_real = typeof config.key === 'function' ? config.key.call(target, ...args) : config.key;
5206
5202
  return await excuteWithLock(config, async () => await fn__.call(this, ...args));
5207
5203
  };
5208
5204
  };
@@ -5283,16 +5279,17 @@ async function clearParent(clearKey) {
5283
5279
  */
5284
5280
  export async function excuteWithCache(config, fn) {
5285
5281
  const db = getRedisDB();
5286
- const cache = await db.get(`[cache]${config.key}`);
5282
+ const key = typeof config.key === 'function' ? config.key() : config.key;
5283
+ const cache = await db.get(`[cache]${key}`);
5287
5284
  if (cache) {
5288
- globalThis[_LoggerService].debug?.(`cache ${config.key} hit!`);
5285
+ globalThis[_LoggerService].debug?.(`cache ${key} hit!`);
5289
5286
  return JSON.parse(cache);
5290
5287
  }
5291
5288
  else {
5292
- globalThis[_LoggerService].debug?.(`cache ${config.key} miss!`);
5293
5289
  const result = await fn();
5290
+ globalThis[_LoggerService].debug?.(`cache ${key} miss!`);
5294
5291
  await setMethodCache({
5295
- key: config.key,
5292
+ key,
5296
5293
  clearKey: config.clearKey,
5297
5294
  autoClearTime: config.autoClearTime,
5298
5295
  result