@maiyunnet/kebab 9.17.0 → 9.17.1

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/doc/kebab-rag.md CHANGED
@@ -1428,7 +1428,7 @@ index/variables/VER.md
1428
1428
 
1429
1429
  # Variable: VER
1430
1430
 
1431
- > `const` **VER**: `"9.17.0"` = `'9.17.0'`
1431
+ > `const` **VER**: `"9.17.1"` = `'9.17.1'`
1432
1432
 
1433
1433
  Defined in: [index.ts:10](https://github.com/maiyunnet/kebab/blob/master/index.ts#L10)
1434
1434
 
@@ -113464,21 +113464,33 @@ Defined in: [lib/db/conn.ts:64](https://github.com/maiyunnet/kebab/blob/master/l
113464
113464
 
113465
113465
  ### beginTransaction()
113466
113466
 
113467
- > **beginTransaction**(): `Promise`\<`boolean`\>
113467
+ > **beginTransaction**(`logError?`): `Promise`\<`boolean`\>
113468
113468
 
113469
- Defined in: [lib/db/conn.ts:326](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L326)
113469
+ Defined in: [lib/db/conn.ts:337](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L337)
113470
+
113471
+ 开启事务,只能在独占连接中使用
113472
+
113473
+ #### Parameters
113474
+
113475
+ ##### logError?
113476
+
113477
+ `boolean` = `true`
113478
+
113479
+ 失败时是否记录错误,连接池在非最后一次重试时传 false
113470
113480
 
113471
113481
  #### Returns
113472
113482
 
113473
113483
  `Promise`\<`boolean`\>
113474
113484
 
113485
+ 是否开启成功
113486
+
113475
113487
  ***
113476
113488
 
113477
113489
  ### commit()
113478
113490
 
113479
113491
  > **commit**(): `Promise`\<`boolean`\>
113480
113492
 
113481
- Defined in: [lib/db/conn.ts:352](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L352)
113493
+ Defined in: [lib/db/conn.ts:367](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L367)
113482
113494
 
113483
113495
  #### Returns
113484
113496
 
@@ -113490,7 +113502,7 @@ Defined in: [lib/db/conn.ts:352](https://github.com/maiyunnet/kebab/blob/master/
113490
113502
 
113491
113503
  > **end**(): `Promise`\<`boolean`\>
113492
113504
 
113493
- Defined in: [lib/db/conn.ts:315](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L315)
113505
+ Defined in: [lib/db/conn.ts:320](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L320)
113494
113506
 
113495
113507
  关闭连接,一般情况下不使用
113496
113508
 
@@ -113504,7 +113516,7 @@ Defined in: [lib/db/conn.ts:315](https://github.com/maiyunnet/kebab/blob/master/
113504
113516
 
113505
113517
  > **execute**(`sql`, `values?`): `Promise`\<[`IPacket`](../../interfaces/IPacket.md)\>
113506
113518
 
113507
- Defined in: [lib/db/conn.ts:241](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L241)
113519
+ Defined in: [lib/db/conn.ts:246](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L246)
113508
113520
 
113509
113521
  执行一条 SQL 并获得影响行数对象 packet
113510
113522
 
@@ -113588,7 +113600,7 @@ Defined in: [lib/db/conn.ts:78](https://github.com/maiyunnet/kebab/blob/master/l
113588
113600
 
113589
113601
  > **isAvailable**(`last?`): `Promise`\<`boolean`\>
113590
113602
 
113591
- Defined in: [lib/db/conn.ts:159](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L159)
113603
+ Defined in: [lib/db/conn.ts:164](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L164)
113592
113604
 
113593
113605
  通过执行一条语句判断当前连接是否可用
113594
113606
 
@@ -113652,7 +113664,7 @@ Defined in: [lib/db/conn.ts:123](https://github.com/maiyunnet/kebab/blob/master/
113652
113664
 
113653
113665
  > **query**(`sql`, `values?`): `Promise`\<[`IData`](../../interfaces/IData.md)\>
113654
113666
 
113655
- Defined in: [lib/db/conn.ts:182](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L182)
113667
+ Defined in: [lib/db/conn.ts:187](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L187)
113656
113668
 
113657
113669
  执行一条 SQL 并获得返回数据
113658
113670
 
@@ -113680,7 +113692,7 @@ Defined in: [lib/db/conn.ts:182](https://github.com/maiyunnet/kebab/blob/master/
113680
113692
 
113681
113693
  > **refreshLast**(): `void`
113682
113694
 
113683
- Defined in: [lib/db/conn.ts:151](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L151)
113695
+ Defined in: [lib/db/conn.ts:156](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L156)
113684
113696
 
113685
113697
  设定最后使用时间
113686
113698
 
@@ -113694,7 +113706,7 @@ Defined in: [lib/db/conn.ts:151](https://github.com/maiyunnet/kebab/blob/master/
113694
113706
 
113695
113707
  > **rollback**(): `Promise`\<`boolean`\>
113696
113708
 
113697
- Defined in: [lib/db/conn.ts:372](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L372)
113709
+ Defined in: [lib/db/conn.ts:387](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L387)
113698
113710
 
113699
113711
  #### Returns
113700
113712
 
@@ -113720,7 +113732,7 @@ Defined in: [lib/db/conn.ts:102](https://github.com/maiyunnet/kebab/blob/master/
113720
113732
 
113721
113733
  > **used**(): `void`
113722
113734
 
113723
- Defined in: [lib/db/conn.ts:144](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L144)
113735
+ Defined in: [lib/db/conn.ts:149](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L149)
113724
113736
 
113725
113737
  取消占用
113726
113738
 
@@ -113732,11 +113744,21 @@ Defined in: [lib/db/conn.ts:144](https://github.com/maiyunnet/kebab/blob/master/
113732
113744
 
113733
113745
  ### using()
113734
113746
 
113735
- > **using**(): `boolean`
113747
+ > **using**(`opt?`): `boolean`
113748
+
113749
+ Defined in: [lib/db/conn.ts:131](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L131)
113736
113750
 
113737
- Defined in: [lib/db/conn.ts:130](https://github.com/maiyunnet/kebab/blob/master/lib/db/conn.ts#L130)
113751
+ 判断是否可用(丢失的也算不可用),返回 true 代表获取成功
113738
113752
 
113739
- 判断是否可用(丢失的也算不可用),返回 true 代表获取成功并自动刷新最后时间
113753
+ #### Parameters
113754
+
113755
+ ##### opt?
113756
+
113757
+ 获取选项,连接巡检独占时不刷新最后使用时间
113758
+
113759
+ ###### refreshLast?
113760
+
113761
+ `boolean`
113740
113762
 
113741
113763
  #### Returns
113742
113764
 
@@ -114044,7 +114066,7 @@ lib/db/pool/classes/Pool.md
114044
114066
 
114045
114067
  # Class: Pool
114046
114068
 
114047
- Defined in: [lib/db/pool.ts:122](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L122)
114069
+ Defined in: [lib/db/pool.ts:127](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L127)
114048
114070
 
114049
114071
  数据库连接池对象
114050
114072
 
@@ -114054,7 +114076,7 @@ Defined in: [lib/db/pool.ts:122](https://github.com/maiyunnet/kebab/blob/master/
114054
114076
 
114055
114077
  > **new Pool**(`etc`, `opt`): `Pool`
114056
114078
 
114057
- Defined in: [lib/db/pool.ts:133](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L133)
114079
+ Defined in: [lib/db/pool.ts:138](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L138)
114058
114080
 
114059
114081
  #### Parameters
114060
114082
 
@@ -114080,7 +114102,7 @@ Defined in: [lib/db/pool.ts:133](https://github.com/maiyunnet/kebab/blob/master/
114080
114102
 
114081
114103
  > **beginTransaction**(`ctr`): `Promise`\<[`Transaction`](../../tran/classes/Transaction.md) \| `null`\>
114082
114104
 
114083
- Defined in: [lib/db/pool.ts:201](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L201)
114105
+ Defined in: [lib/db/pool.ts:206](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L206)
114084
114106
 
114085
114107
  开启事务,返回事务对象并锁定连接,别人任何人不可用,有 ctr 的话必传 this,独立执行时可传 null
114086
114108
 
@@ -114100,7 +114122,7 @@ Defined in: [lib/db/pool.ts:201](https://github.com/maiyunnet/kebab/blob/master/
114100
114122
 
114101
114123
  > **execute**(`sql`, `values?`): `Promise`\<[`IPacket`](../../interfaces/IPacket.md)\>
114102
114124
 
114103
- Defined in: [lib/db/pool.ts:181](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L181)
114125
+ Defined in: [lib/db/pool.ts:186](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L186)
114104
114126
 
114105
114127
  执行一条 SQL 并获得影响行数对象 packet,连接失败抛出错误
114106
114128
 
@@ -114128,7 +114150,7 @@ Defined in: [lib/db/pool.ts:181](https://github.com/maiyunnet/kebab/blob/master/
114128
114150
 
114129
114151
  > **getName**(): `string`
114130
114152
 
114131
- Defined in: [lib/db/pool.ts:147](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L147)
114153
+ Defined in: [lib/db/pool.ts:152](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L152)
114132
114154
 
114133
114155
  获取当前连接的数据库名称
114134
114156
 
@@ -114142,7 +114164,7 @@ Defined in: [lib/db/pool.ts:147](https://github.com/maiyunnet/kebab/blob/master/
114142
114164
 
114143
114165
  > **getQueries**(): `number`
114144
114166
 
114145
- Defined in: [lib/db/pool.ts:339](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L339)
114167
+ Defined in: [lib/db/pool.ts:346](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L346)
114146
114168
 
114147
114169
  获取 SQL 执行次数
114148
114170
 
@@ -114156,7 +114178,7 @@ Defined in: [lib/db/pool.ts:339](https://github.com/maiyunnet/kebab/blob/master/
114156
114178
 
114157
114179
  > **getService**(): [`ESERVICE`](../../enumerations/ESERVICE.md)
114158
114180
 
114159
- Defined in: [lib/db/pool.ts:142](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L142)
114181
+ Defined in: [lib/db/pool.ts:147](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L147)
114160
114182
 
114161
114183
  获取当前连接的服务商
114162
114184
 
@@ -114170,7 +114192,7 @@ Defined in: [lib/db/pool.ts:142](https://github.com/maiyunnet/kebab/blob/master/
114170
114192
 
114171
114193
  > **query**(`sql`, `values?`): `Promise`\<[`IData`](../../interfaces/IData.md)\>
114172
114194
 
114173
- Defined in: [lib/db/pool.ts:157](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L157)
114195
+ Defined in: [lib/db/pool.ts:162](https://github.com/maiyunnet/kebab/blob/master/lib/db/pool.ts#L162)
114174
114196
 
114175
114197
  执行一条 SQL,无视顺序和相同连接,随用随取
114176
114198
 
package/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export declare const VER = "9.17.0";
8
+ export declare const VER = "9.17.1";
9
9
  /** --- 框架根目录,以 / 结尾 --- */
10
10
  export declare const ROOT_PATH: string;
11
11
  /** --- 框架的 LIB,以 / 结尾 --- */
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * --- 本文件用来定义每个目录实体地址的常量 ---
7
7
  */
8
8
  /** --- 当前系统版本号 --- */
9
- export const VER = '9.17.0';
9
+ export const VER = '9.17.1';
10
10
  // --- 服务端用的路径 ---
11
11
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
12
12
  /** --- /xxx/xxx --- */
package/lib/db/conn.d.ts CHANGED
@@ -53,9 +53,12 @@ export declare class Connection {
53
53
  */
54
54
  isUsing(): boolean;
55
55
  /**
56
- * --- 判断是否可用(丢失的也算不可用),返回 true 代表获取成功并自动刷新最后时间 ---
56
+ * --- 判断是否可用(丢失的也算不可用),返回 true 代表获取成功 ---
57
+ * @param opt 获取选项,连接巡检独占时不刷新最后使用时间
57
58
  */
58
- using(): boolean;
59
+ using(opt?: {
60
+ 'refreshLast'?: boolean;
61
+ }): boolean;
59
62
  /**
60
63
  * --- 取消占用 ---
61
64
  */
@@ -85,7 +88,12 @@ export declare class Connection {
85
88
  * --- 关闭连接,一般情况下不使用 ---
86
89
  */
87
90
  end(): Promise<boolean>;
88
- beginTransaction(): Promise<boolean>;
91
+ /**
92
+ * --- 开启事务,只能在独占连接中使用 ---
93
+ * @param logError 失败时是否记录错误,连接池在非最后一次重试时传 false
94
+ * @returns 是否开启成功
95
+ */
96
+ beginTransaction(logError?: boolean): Promise<boolean>;
89
97
  commit(): Promise<boolean>;
90
98
  rollback(): Promise<boolean>;
91
99
  }
package/lib/db/conn.js CHANGED
@@ -99,14 +99,17 @@ export class Connection {
99
99
  return this._using;
100
100
  }
101
101
  /**
102
- * --- 判断是否可用(丢失的也算不可用),返回 true 代表获取成功并自动刷新最后时间 ---
102
+ * --- 判断是否可用(丢失的也算不可用),返回 true 代表获取成功 ---
103
+ * @param opt 获取选项,连接巡检独占时不刷新最后使用时间
103
104
  */
104
- using() {
105
+ using(opt = {}) {
105
106
  if (this._lost || this._using) {
106
107
  return false;
107
108
  }
108
109
  else {
109
- this.refreshLast();
110
+ if (opt.refreshLast !== false) {
111
+ this.refreshLast();
112
+ }
110
113
  this._using = true;
111
114
  return true;
112
115
  }
@@ -281,6 +284,8 @@ export class Connection {
281
284
  * --- 关闭连接,一般情况下不使用 ---
282
285
  */
283
286
  async end() {
287
+ // --- 驱动的 end 事件可能在异步关闭完成后才触发,提前标记避免连接池在关闭期间重新取到本连接 ---
288
+ this._lost = true;
284
289
  try {
285
290
  await this._link.end();
286
291
  return true;
@@ -289,8 +294,12 @@ export class Connection {
289
294
  return false;
290
295
  }
291
296
  }
292
- // --- 事务,只能在独占连接中使用,pool 创建事务返回独占连接,commit 或 rollback 释放连接回连接池 ---
293
- async beginTransaction() {
297
+ /**
298
+ * --- 开启事务,只能在独占连接中使用 ---
299
+ * @param logError 失败时是否记录错误,连接池在非最后一次重试时传 false
300
+ * @returns 是否开启成功
301
+ */
302
+ async beginTransaction(logError = true) {
294
303
  if (this._using) {
295
304
  try {
296
305
  this._transaction = true;
@@ -306,12 +315,16 @@ export class Connection {
306
315
  this._transaction = false;
307
316
  this._using = false;
308
317
  this._lost = true;
309
- lCore.log({}, '[DB][Connection][beginTransaction] ' + lText.stringifyError(e), '-error');
318
+ if (logError) {
319
+ lCore.log({}, '[DB][Connection][beginTransaction] ' + lText.stringifyError(e), '-error');
320
+ }
310
321
  return false;
311
322
  }
312
323
  }
313
324
  else {
314
- lCore.log({}, '[DB][Connection][beginTransaction] connection is not in use', '-error');
325
+ if (logError) {
326
+ lCore.log({}, '[DB][Connection][beginTransaction] connection is not in use', '-error');
327
+ }
315
328
  return false;
316
329
  }
317
330
  }
package/lib/db/pool.js CHANGED
@@ -67,6 +67,10 @@ async function checkConnection() {
67
67
  }
68
68
  continue;
69
69
  }
70
+ // --- 巡检探活或关闭前先独占连接,避免业务在异步操作期间取到同一连接 ---
71
+ if (!connection.using({ 'refreshLast': false })) {
72
+ continue;
73
+ }
70
74
  if (connection.getLast() <= now - 30) {
71
75
  // --- 超 30 秒未被使用,则关闭 ---
72
76
  await connection.end();
@@ -75,8 +79,9 @@ async function checkConnection() {
75
79
  continue;
76
80
  }
77
81
  // --- 30 秒内使用过,看看连接是否正常 ---
78
- if (await connection.isAvailable(false)) {
82
+ if (await connection.isAvailable(false) && !connection.isLost()) {
79
83
  // --- 正常 ---
84
+ connection.used();
80
85
  continue;
81
86
  }
82
87
  // --- 连接有问题,直接关闭 ---
@@ -120,7 +125,7 @@ export class Pool {
120
125
  */
121
126
  async query(sql, values) {
122
127
  ++this._queries;
123
- // --- 获取并自动 using ---
128
+ // --- 获取并自动 using ---
124
129
  const conn = await this._getConnection();
125
130
  if (!conn) {
126
131
  return {
@@ -167,7 +172,9 @@ export class Pool {
167
172
  lCore.log(ctr ?? {}, `[DB][Pool][beginTransaction] failed to get connection, service: ${lDb.ESERVICE[this._service]}, database: ${this._etc.name ?? ''}`, '-error');
168
173
  return null;
169
174
  }
170
- if (!await conn.beginTransaction()) {
175
+ /** --- 只有最后一次尝试仍失败时才记录原始错误,避免已恢复的重试被误判为业务失败 --- */
176
+ const logError = i === BEGIN_TRANSACTION_MAX_ATTEMPTS - 1;
177
+ if (!await conn.beginTransaction(logError)) {
171
178
  continue;
172
179
  }
173
180
  return new Transaction(ctr, conn);
@@ -31,7 +31,7 @@ export declare class Response {
31
31
  /**
32
32
  * --- 获取响应读取流对象 ---
33
33
  */
34
- getStream(): (import("undici/types/readable").default & undici.Dispatcher.BodyMixin) | zlib.Gunzip | zlib.Inflate | zlib.BrotliDecompress | null;
34
+ getStream(): zlib.BrotliDecompress | zlib.Gunzip | zlib.Inflate | (import("undici/types/readable").default & undici.Dispatcher.BodyMixin) | null;
35
35
  /**
36
36
  * --- 获取原生响应读取流对象 ---
37
37
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "9.17.0",
3
+ "version": "9.17.1",
4
4
  "description": "Simple, easy-to-use, and fully-featured Node.js framework that is ready-to-use out of the box.",
5
5
  "type": "module",
6
6
  "keywords": [