@eggjs/tegg-dal-plugin 3.64.5 → 3.65.3

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 (2) hide show
  1. package/README.md +48 -1
  2. package/package.json +8 -8
package/README.md CHANGED
@@ -77,6 +77,29 @@ dataSource:
77
77
  port: 3306
78
78
  ```
79
79
 
80
+ #### executeType 配置
81
+
82
+ 可以通过在 `dataSource` 下配置 `executeType` 来指定 SQL 执行模式:
83
+
84
+ ```yaml
85
+ dataSource:
86
+ foo:
87
+ connectionLimit: 100
88
+ database: 'test'
89
+ host: '127.0.0.1'
90
+ user: root
91
+ port: 3306
92
+ executeType: execute # 可选值: execute | query,默认为 query
93
+ ```
94
+
95
+ **执行模式说明:**
96
+ - `execute`: 使用 SQL 参数化查询,采用服务端序列化参数模式,通过预编译语句执行,某些情况下能提升查询性能
97
+ - `query`: 使用文本 SQL 模式,在本地将参数序列化到 SQL 语句中(nodejs 生态中 mysql1 只有这种模式)
98
+
99
+ **注意事项:**
100
+ - 由于 MySQL execute 模式反序列化 float 采用二进制模式,可能会导致精度丢失,建议使用 decimal 类型
101
+ - 当使用 `executeType: execute` 时,请确保数据库中的浮点数字段使用 `DECIMAL` 类型而不是 `FLOAT` 或 `DOUBLE` 类型,以避免精度问题
102
+
80
103
  ### Table
81
104
 
82
105
  `TableModel` 定义一个表结构,包括表配置、列、索引。
@@ -572,7 +595,7 @@ import { SqlMap, SqlType } from '@eggjs/tegg/dal';
572
595
  export default {
573
596
  findByName: {
574
597
  type: SqlType.SELECT,
575
- sql: 'SELECT {{ allColumns }} FROM egg_foo WHERE name = {{ name }}',
598
+ sql: 'SELECT {{ allColumns }} FROM egg_foo WHERE name = {{ name | param }}',
576
599
  },
577
600
  } as Record<string, SqlMap>;
578
601
  ```
@@ -599,6 +622,7 @@ export default class FooDAO extends BaseFooDAO {
599
622
  支持的自定义 filter
600
623
 
601
624
  ```
625
+ - param: 参数化查询过滤器,用于防止 SQL 注入
602
626
  - toPoint
603
627
  - toLine
604
628
  - toPolygon
@@ -609,6 +633,29 @@ export default class FooDAO extends BaseFooDAO {
609
633
  - toGeometryCollection
610
634
  ```
611
635
 
636
+ **param 过滤器**
637
+
638
+ `param` 过滤器用于将值作为参数化查询参数,而不是直接拼接到 SQL 字符串中。可以有效利用到 sql parameters 的能力,小幅提升 db 性能与观测能力。
639
+
640
+ 使用示例:
641
+
642
+ ```ts
643
+ export default {
644
+ findByNameAndAge: {
645
+ type: SqlType.SELECT,
646
+ sql: `
647
+ SELECT {{ allColumns }}
648
+ FROM egg_foo
649
+ WHERE name = {{ name | param }}
650
+ AND age > {{ age | param }}
651
+ `,
652
+ },
653
+ } as Record<string, SqlMap>;
654
+ ```
655
+
656
+ 生成的 SQL:`SELECT ... FROM egg_foo WHERE name = ? AND age > ?`
657
+ 参数数组:`['John', 18]`
658
+
612
659
  支持自定义 block 来简化 sql, 如内置的 allColumns
613
660
 
614
661
  ```ts
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "eggModule": {
11
11
  "name": "teggDal"
12
12
  },
13
- "version": "3.64.5",
13
+ "version": "3.65.3",
14
14
  "description": "dal plugin for egg",
15
15
  "main": "index.js",
16
16
  "keywords": [
@@ -35,8 +35,8 @@
35
35
  "scripts": {
36
36
  "test": "cross-env NODE_ENV=test NODE_OPTIONS='--no-deprecation' mocha",
37
37
  "clean": "tsc -b --clean",
38
- "tsc": "ut clean && tsc -p ./tsconfig.json",
39
- "tsc:pub": "ut clean && tsc -p ./tsconfig.pub.json",
38
+ "tsc": "ut run clean && tsc -p ./tsconfig.json",
39
+ "tsc:pub": "ut run clean && tsc -p ./tsconfig.pub.json",
40
40
  "prepublishOnly": "ut tsc:pub"
41
41
  },
42
42
  "homepage": "https://github.com/eggjs/tegg",
@@ -52,12 +52,12 @@
52
52
  "node": ">=14.0.0"
53
53
  },
54
54
  "dependencies": {
55
- "@eggjs/dal-runtime": "^3.64.5"
55
+ "@eggjs/dal-runtime": "^3.65.3"
56
56
  },
57
57
  "devDependencies": {
58
- "@eggjs/tegg-aop-plugin": "^3.64.5",
59
- "@eggjs/tegg-config": "^3.64.5",
60
- "@eggjs/tegg-plugin": "^3.64.5",
58
+ "@eggjs/tegg-aop-plugin": "^3.65.3",
59
+ "@eggjs/tegg-config": "^3.65.3",
60
+ "@eggjs/tegg-plugin": "^3.65.3",
61
61
  "@types/mocha": "^10.0.1",
62
62
  "@types/node": "^20.2.4",
63
63
  "cross-env": "^7.0.3",
@@ -72,5 +72,5 @@
72
72
  "publishConfig": {
73
73
  "access": "public"
74
74
  },
75
- "gitHead": "bb2ff7c64016202925cd660f510840286aca041d"
75
+ "gitHead": "e3334a4c8b04b22a2197693f0037fbf272b97032"
76
76
  }