wok-server 0.4.7 → 0.4.8

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.
@@ -16,14 +16,18 @@ function doRequest(opts) {
16
16
  if (opts.query) {
17
17
  Object.entries(opts.query).forEach(entry => {
18
18
  const [key, val] = entry;
19
- if (typeof val === 'string') {
19
+ // 忽略空值
20
+ if (val === undefined || val === null) {
21
+ return;
22
+ }
23
+ else if (typeof val === 'string') {
20
24
  url.searchParams.append(key, val);
21
25
  }
22
26
  else if (Array.isArray(val)) {
23
27
  val.forEach(v => url.searchParams.append(key, v));
24
28
  }
25
29
  else {
26
- throw new Error(`The value is neither of string nor string[] type:${val}`);
30
+ throw new Error(`The request parameter value is neither of string nor string[] type:${val} ,name: ${key}`);
27
31
  }
28
32
  });
29
33
  }
@@ -23,7 +23,8 @@ exports.defaultConfig = {
23
23
  slowSqlWarn: true,
24
24
  slowSqlMs: 200,
25
25
  transactionTimeout: 5000,
26
- transactionStrict: true
26
+ transactionStrict: true,
27
+ maxOpsInStrictTx: 10
27
28
  };
28
29
  /**
29
30
  * 配置校验规则.
@@ -45,5 +46,6 @@ exports.configValidation = {
45
46
  slowSqlWarn: [(0, validation_1.notNull)()],
46
47
  slowSqlMs: [(0, validation_1.notNull)(), (0, validation_1.min)(1), (0, validation_1.max)(3600000)],
47
48
  transactionTimeout: [(0, validation_1.notNull)(), (0, validation_1.min)(0), (0, validation_1.max)(60000)],
48
- transactionStrict: [(0, validation_1.notNull)()]
49
+ transactionStrict: [(0, validation_1.notNull)()],
50
+ maxOpsInStrictTx: [(0, validation_1.notNull)(), (0, validation_1.min)(1)]
49
51
  };
@@ -7,15 +7,17 @@ const exception_1 = require("../exception");
7
7
  * 严格 mysql 事务会话,会禁用一些操作.
8
8
  */
9
9
  class MysqlStrictTxSession extends tx_1.MysqlTxSession {
10
+ config;
10
11
  #opsCount = 0;
11
12
  constructor(config, conn) {
12
13
  super(config, conn);
14
+ this.config = config;
13
15
  }
14
16
  /**
15
17
  * 为操作计数,检查是否操作次数过多.
16
18
  */
17
19
  #checkAndAddOpsCount() {
18
- if (this.#opsCount >= 10) {
20
+ if (this.#opsCount >= this.config.maxOpsInStrictTx) {
19
21
  throw new exception_1.MysqlException('Too many operations in a strict transaction.');
20
22
  }
21
23
  this.#opsCount++;
@@ -629,3 +629,39 @@ await startWebServer({
629
629
  }
630
630
  })
631
631
  ```
632
+
633
+ ### Server-Sent Events
634
+
635
+ 组件本身没有直接提供对 SSE(Server-Sent Events) 的支持,可以在路由的处理函数中适当的进行处理来实现。
636
+
637
+ ```ts
638
+ // 启动服务
639
+ await startWebServer({
640
+ // 路由配置
641
+ routers: {
642
+ '/sse': async exchange => {
643
+ const { response } = exchange
644
+ // 设置消息头
645
+ response.setHeader('Content-Type', 'text/event-stream')
646
+ response.setHeader('Cache-Control', 'no-cache')
647
+ response.setHeader('Connection', 'keep-alive')
648
+ // 模拟异步操作更新多条消息
649
+ let counter = 0
650
+ for (let i = 0; i < 10; i++) {
651
+ // 沉睡一秒,模拟异步业务处理
652
+ await new Promise<void>((resolve, reject) => {
653
+ setTimeout(resolve, 1000)
654
+ })
655
+ counter++
656
+ // 写数据到响应信息中,前端在 EventSource 的 message 事件中处理
657
+ response.write(`data: ${JSON.stringify({ message: '实时更新', count: counter })}\n\n`)
658
+ if (counter >= 10) {
659
+ break
660
+ }
661
+ }
662
+ // 最后结束响应
663
+ response.end()
664
+ }
665
+ }
666
+ })
667
+ ```
@@ -29,6 +29,7 @@ mysql 组件基于 [mysql2](https://www.npmjs.com/package/mysql2) 封装,提
29
29
  | MYSQL_SLOW_SQL_MS | 慢 sql 毫秒数,默认 200 |
30
30
  | MYSQL_TRANSACTION_TIMEOUT | 事务超时时间,单位毫秒,默认 5000 |
31
31
  | MYSQL_TRANSACTION_STRICT | 事务严格模式,默认 true,设置为 false 可关闭严格模式 |
32
+ | MYSQL_MAX_OPS_IN_STRICT_TX | 严格事务中可以执行的操作次数,默认 10 |
32
33
 
33
34
  ## 初始化
34
35
 
@@ -548,7 +549,7 @@ mysqlManager.tx(
548
549
 
549
550
  ### 严格模式
550
551
 
551
- 因此事务默认是打开严格模式的,在严格模式下,事务中的很多操作都被禁止,
552
+ 事务默认是打开严格模式的,在严格模式下,事务中的很多操作都被禁止,
552
553
  通过将环境变量 MYSQL_TRANSACTION_STRICT (默认变量名称,多实例的情况下使用对应名称)
553
554
  设置为 false 可以关闭。
554
555
 
@@ -560,6 +561,6 @@ mysqlManager.tx(
560
561
  4. 批量查询和计数 find、count、paginate
561
562
  5. findByIdIn 参数超过 100 个
562
563
  6. 使用 query 和 modify 执行自定义 sql
563
- 7. 调用 session 进行的任何操作累计超过 10
564
+ 7. 调用 session 进行的任何操作累计超过 10 次,通过 MYSQL_MAX_OPS_IN_STRICT_TX 变量可以修改允许的次数
564
565
 
565
566
  长事务有很大的风险,在生产环境下推荐使用严格事务,并且将事务的超时时间设置的尽可能短一些。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wok-server",
3
- "version": "0.4.7",
3
+ "version": "0.4.8",
4
4
  "packageManager": "pnpm@8.9.0",
5
5
  "description": "一个基于 NodeJs 和 TypeScript 的后端框架,轻量级、克制、简洁。A lightweight, restrained, and concise backend framework based on Node.js and TypeScript.",
6
6
  "scripts": {
@@ -75,6 +75,10 @@ export interface MysqlConfig {
75
75
  * 事务严格模式
76
76
  */
77
77
  transactionStrict: boolean;
78
+ /**
79
+ * 在严格事务中可进行的最大操作次数
80
+ */
81
+ maxOpsInStrictTx: number;
78
82
  }
79
83
  /**
80
84
  * 默认配置.
@@ -8,6 +8,7 @@ import { DeleteManyOpts, FindOpts, MixCriteria, MysqlPage, MysqlPaginateOpts, Up
8
8
  */
9
9
  export declare class MysqlStrictTxSession extends MysqlTxSession {
10
10
  #private;
11
+ private config;
11
12
  constructor(config: MysqlConfig, conn: PoolConnection);
12
13
  findById<T>(table: Table<T>, id: string | number): Promise<T | null>;
13
14
  findByIdIn<T>(table: Table<T>, ids: (string | number)[]): Promise<T[]>;