wok-server 0.6.0 → 0.7.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/README.en.md +1 -1
- package/dist/http-client/index.js +2 -1
- package/dist/mysql/config.js +1 -1
- package/dist/mysql/manager/base.js +39 -0
- package/dist/mysql/manager/ops/criteria.js +25 -0
- package/dist/mysql/manager/ops/delete.js +4 -10
- package/dist/mysql/manager/ops/find.js +10 -30
- package/dist/mysql/manager/ops/index.js +2 -0
- package/dist/mysql/manager/ops/insert.js +39 -13
- package/dist/mysql/manager/ops/order-by.js +28 -0
- package/dist/mysql/manager/ops/paginate.js +26 -1
- package/dist/mysql/manager/ops/update.js +43 -37
- package/dist/mysql/manager/ops/upsert.js +178 -0
- package/dist/mysql/manager/ops/utils.js +4 -0
- package/documentation/en/mysql.md +135 -5
- package/documentation/zh-cn/mysql.md +146 -17
- package/package.json +2 -1
- package/skills/wok-server-code-navigation/SKILL.md +153 -0
- package/skills/wok-server-mysql/SKILL.md +76 -3
- package/src/http-client/index.ts +8 -3
- package/src/mysql/config.ts +2 -2
- package/src/mysql/manager/base.ts +51 -4
- package/src/mysql/manager/ops/criteria.ts +34 -0
- package/src/mysql/manager/ops/delete.ts +5 -10
- package/src/mysql/manager/ops/find.ts +12 -29
- package/src/mysql/manager/ops/index.ts +2 -0
- package/src/mysql/manager/ops/insert.ts +53 -15
- package/src/mysql/manager/ops/order-by.ts +58 -0
- package/src/mysql/manager/ops/paginate.ts +42 -2
- package/src/mysql/manager/ops/update.ts +66 -42
- package/src/mysql/manager/ops/upsert.ts +224 -0
- package/src/mysql/manager/ops/utils.ts +4 -0
- package/types/http-client/index.d.ts +7 -1
- package/types/mysql/config.d.ts +1 -1
- package/types/mysql/manager/base.d.ts +35 -4
- package/types/mysql/manager/ops/criteria.d.ts +10 -0
- package/types/mysql/manager/ops/delete.d.ts +2 -1
- package/types/mysql/manager/ops/find.d.ts +3 -2
- package/types/mysql/manager/ops/index.d.ts +2 -0
- package/types/mysql/manager/ops/insert.d.ts +16 -2
- package/types/mysql/manager/ops/order-by.d.ts +38 -0
- package/types/mysql/manager/ops/paginate.d.ts +18 -1
- package/types/mysql/manager/ops/update.d.ts +26 -3
- package/types/mysql/manager/ops/upsert.d.ts +36 -0
package/types/mysql/config.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Pool, PoolConnection } from 'mysql2';
|
|
2
2
|
import { MysqlConfig } from '../config';
|
|
3
3
|
import { Table } from '../table-info';
|
|
4
|
-
import { DeleteManyOpts, FindOpts, MixCriteria, MysqlPage, MysqlPaginateOpts, UpdateOpts, Updater, FindSelectOpts } from './ops';
|
|
4
|
+
import { DeleteManyOpts, FindOpts, MixCriteria, MysqlPage, MysqlPaginateOpts, MysqlPaginateSelectOpts, OrderBy, UpdateOpts, Updater, InsertValue, FindSelectOpts } from './ops';
|
|
5
5
|
/**
|
|
6
6
|
* mysql 管理器基类,提供基础的操作方法.
|
|
7
7
|
*/
|
|
@@ -83,20 +83,45 @@ export declare abstract class BaseMysqlManager {
|
|
|
83
83
|
* @param orderBy 排序规则,按先后顺序放入,每个规则是一个元组,第一个元素是字段名称,第二个元素是顺序
|
|
84
84
|
* @returns
|
|
85
85
|
*/
|
|
86
|
-
findFirst<T>(table: Table<T>, criteria?: MixCriteria<T>, orderBy?:
|
|
86
|
+
findFirst<T>(table: Table<T>, criteria?: MixCriteria<T>, orderBy?: OrderBy<T>): Promise<T | null>;
|
|
87
87
|
/**
|
|
88
88
|
* 插入数据. 不支持自增加长id,id必须提前生成,请使用 uuid.
|
|
89
89
|
* @param table 表信息
|
|
90
90
|
* @param data 数据,数据必须是 T 的实例, T 必须是已配置的实体类类型,否则无法完成操作
|
|
91
91
|
* @returns 插入后的数据
|
|
92
92
|
*/
|
|
93
|
-
insert<T>(table: Table<T>, data: T): Promise<T>;
|
|
93
|
+
insert<T>(table: Table<T>, data: InsertValue<T>): Promise<T>;
|
|
94
94
|
/**
|
|
95
95
|
* 批量插入
|
|
96
96
|
* @param table 表
|
|
97
97
|
* @param list 要插入的数据列表
|
|
98
98
|
*/
|
|
99
|
-
insertMany<T>(table: Table<T>, list: T[]): Promise<void>;
|
|
99
|
+
insertMany<T>(table: Table<T>, list: InsertValue<T>[]): Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Upsert 单条数据
|
|
102
|
+
* 如果主键冲突则更新,否则插入
|
|
103
|
+
* @param table 表信息
|
|
104
|
+
* @param data 数据
|
|
105
|
+
* @returns
|
|
106
|
+
*/
|
|
107
|
+
upsert<T>(table: Table<T>, data: InsertValue<T>): Promise<T>;
|
|
108
|
+
/**
|
|
109
|
+
* Upsert 多条数据
|
|
110
|
+
* 如果主键冲突则更新,否则插入
|
|
111
|
+
* @param table 表
|
|
112
|
+
* @param list 要插入的数据列表
|
|
113
|
+
* @returns 影响的行数
|
|
114
|
+
*/
|
|
115
|
+
upsertMany<T>(table: Table<T>, list: InsertValue<T>[]): Promise<number>;
|
|
116
|
+
/**
|
|
117
|
+
* Upsert 单条数据(支持自定义更新器)
|
|
118
|
+
* 如果主键冲突则按自定义逻辑更新,否则插入
|
|
119
|
+
* @param table 表信息
|
|
120
|
+
* @param data 插入的数据
|
|
121
|
+
* @param updater 冲突时的更新器
|
|
122
|
+
* @returns
|
|
123
|
+
*/
|
|
124
|
+
upsertWithUpdater<T>(table: Table<T>, data: InsertValue<T>, updater: Updater<T>): Promise<T>;
|
|
100
125
|
/**
|
|
101
126
|
* 更新
|
|
102
127
|
* @param table 表信息
|
|
@@ -148,6 +173,12 @@ export declare abstract class BaseMysqlManager {
|
|
|
148
173
|
* @returns
|
|
149
174
|
*/
|
|
150
175
|
paginate<T>(opts: MysqlPaginateOpts<T>): Promise<MysqlPage<T>>;
|
|
176
|
+
/**
|
|
177
|
+
* 指定字段分页查询
|
|
178
|
+
* @param opts
|
|
179
|
+
* @returns
|
|
180
|
+
*/
|
|
181
|
+
paginateSelect<T, K extends keyof T>(opts: MysqlPaginateSelectOpts<T, K>): Promise<MysqlPage<Pick<T, K>>>;
|
|
151
182
|
/**
|
|
152
183
|
* 自定义查询,指定 sql 、参数和返回值类型
|
|
153
184
|
* @param sql 预编译 sql ,参数使用 ”?“(英文问号) 占位,注意查询的字段名称会与返回值类型的字段映射,如果 sql 中的字段名称很特殊(比如纯数字等),需要设置别名,避免产生映射错误
|
|
@@ -108,6 +108,16 @@ export declare class MysqlCriteria<T> {
|
|
|
108
108
|
* @returns
|
|
109
109
|
*/
|
|
110
110
|
isNotNull(field: MysqlCriteriaKey<T>): this;
|
|
111
|
+
/**
|
|
112
|
+
* 自定义表达式查询
|
|
113
|
+
* 如 .expr('?? * ? > ?', ['balance', 2, 50])
|
|
114
|
+
* 如 .expr('MATCH(??, ??) AGAINST(? IN BOOLEAN MODE)', ['title', 'content', keyword])
|
|
115
|
+
* 如 .expr('VECTOR_DISTANCE(??, STRING_TO_VECTOR(?)) < ?', ['content_vec', embedding, threshold])
|
|
116
|
+
* @param sql SQL 片段,使用 ?? 引用列名,? 引用参数值
|
|
117
|
+
* @param values 参数值数组,按 SQL 中占位符顺序传入
|
|
118
|
+
* @returns
|
|
119
|
+
*/
|
|
120
|
+
expr(sql: string, values?: any[]): this;
|
|
111
121
|
/**
|
|
112
122
|
* 判定是否空,未设置条件.
|
|
113
123
|
* @returns
|
|
@@ -2,6 +2,7 @@ import { PoolConnection } from 'mysql2';
|
|
|
2
2
|
import { Table } from '../../table-info';
|
|
3
3
|
import { MixCriteria } from './criteria';
|
|
4
4
|
import { MysqlConfig } from '../../config';
|
|
5
|
+
import { OrderBy } from './order-by';
|
|
5
6
|
/**
|
|
6
7
|
* 按 id 删除.
|
|
7
8
|
*
|
|
@@ -33,7 +34,7 @@ export interface DeleteManyOpts<T> {
|
|
|
33
34
|
/**
|
|
34
35
|
* 排序规则,按先后顺序放入,每个规则是一个元组,第一个元素是字段名称,第二个元素是顺序
|
|
35
36
|
*/
|
|
36
|
-
orderBy?:
|
|
37
|
+
orderBy?: OrderBy<T>;
|
|
37
38
|
}
|
|
38
39
|
/**
|
|
39
40
|
* 按条件删除,返回被删除的记录数.
|
|
@@ -2,6 +2,7 @@ import { PoolConnection } from 'mysql2';
|
|
|
2
2
|
import { Table } from '../../table-info';
|
|
3
3
|
import { MixCriteria } from './criteria';
|
|
4
4
|
import { MysqlConfig } from '../../config';
|
|
5
|
+
import { OrderBy } from './order-by';
|
|
5
6
|
/**
|
|
6
7
|
* 按 id 查询
|
|
7
8
|
* @param connection
|
|
@@ -40,7 +41,7 @@ export interface FindOpts<T> {
|
|
|
40
41
|
/**
|
|
41
42
|
* 排序规则,按先后顺序放入,每个规则是一个元组,第一个元素是字段名称,第二个元素是顺序
|
|
42
43
|
*/
|
|
43
|
-
orderBy?:
|
|
44
|
+
orderBy?: OrderBy<T>;
|
|
44
45
|
}
|
|
45
46
|
/**
|
|
46
47
|
* 条件查询
|
|
@@ -83,4 +84,4 @@ export declare function findFirst<T>(config: MysqlConfig, conn: PoolConnection,
|
|
|
83
84
|
/**
|
|
84
85
|
* 排序规则,按先后顺序放入,每个规则是一个元组,第一个元素是字段名称,第二个元素是顺序
|
|
85
86
|
*/
|
|
86
|
-
orderBy?:
|
|
87
|
+
orderBy?: OrderBy<T>): Promise<T | null>;
|
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import { PoolConnection } from 'mysql2';
|
|
2
2
|
import { Table } from '../../table-info';
|
|
3
3
|
import { MysqlConfig } from '../../config';
|
|
4
|
+
/**
|
|
5
|
+
* 插入值类型,支持在 INSERT VALUES 中使用表达式
|
|
6
|
+
*/
|
|
7
|
+
export type InsertValue<T> = {
|
|
8
|
+
[K in keyof T]?: T[K] | ['now'] | ['set', T[K]] | ['expr', string] | ['expr', string, any[]];
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* 处理 insert value,支持表达式
|
|
12
|
+
* @returns { frag: SQL 片段, values: 参数值数组 }
|
|
13
|
+
*/
|
|
14
|
+
export declare function processInsertValue(value: any): {
|
|
15
|
+
frag: string;
|
|
16
|
+
values: any[];
|
|
17
|
+
};
|
|
4
18
|
/**
|
|
5
19
|
* 为表插入数据
|
|
6
20
|
* @param connection
|
|
@@ -8,11 +22,11 @@ import { MysqlConfig } from '../../config';
|
|
|
8
22
|
* @param data
|
|
9
23
|
* @returns
|
|
10
24
|
*/
|
|
11
|
-
export declare function insert<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, data: T): Promise<T>;
|
|
25
|
+
export declare function insert<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, data: InsertValue<T>): Promise<T>;
|
|
12
26
|
/**
|
|
13
27
|
* 一次插入多条记录
|
|
14
28
|
* @param connection 连接
|
|
15
29
|
* @param table 表
|
|
16
30
|
* @param list 要插入的记录列表
|
|
17
31
|
*/
|
|
18
|
-
export declare function insertMany<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, list: T[]): Promise<void>;
|
|
32
|
+
export declare function insertMany<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, list: InsertValue<T>[]): Promise<void>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 排序规则类型.
|
|
3
|
+
*
|
|
4
|
+
* T 为表类型.
|
|
5
|
+
*
|
|
6
|
+
* 普通列排序:
|
|
7
|
+
* [keyof T, 'asc' | 'desc']
|
|
8
|
+
* 例: ['balance', 'asc'] → ORDER BY `balance` asc
|
|
9
|
+
*
|
|
10
|
+
* 自定义表达式排序:
|
|
11
|
+
* ['expr', SQL片段, 参数值数组, 'asc' | 'desc']
|
|
12
|
+
* 例: ['expr', '?? * ?', ['balance', 2], 'desc']
|
|
13
|
+
* → ORDER BY `balance` * 2 desc
|
|
14
|
+
*
|
|
15
|
+
* 例: ['expr', 'CHAR_LENGTH(??)', ['name'], 'desc']
|
|
16
|
+
* → ORDER BY CHAR_LENGTH(`name`) desc
|
|
17
|
+
*
|
|
18
|
+
* 例: ['expr', 'VECTOR_DISTANCE(??, STRING_TO_VECTOR(?))', ['content_vec', embedding], 'asc']
|
|
19
|
+
* → ORDER BY VECTOR_DISTANCE(`content_vec`, STRING_TO_VECTOR(?)) asc
|
|
20
|
+
*
|
|
21
|
+
* 混合使用:
|
|
22
|
+
* [
|
|
23
|
+
* ['active', 'asc'],
|
|
24
|
+
* ['expr', '?? * ?', ['balance', 2], 'desc']
|
|
25
|
+
* ]
|
|
26
|
+
* → ORDER BY `active` asc , `balance` * 2 desc
|
|
27
|
+
*/
|
|
28
|
+
export type OrderBy<T> = Array<[keyof T, 'asc' | 'desc'] | ['expr', string, any[], 'asc' | 'desc']>;
|
|
29
|
+
/**
|
|
30
|
+
* 构建 ORDER BY 子句.
|
|
31
|
+
*
|
|
32
|
+
* @param orderBy 排序规则
|
|
33
|
+
* @returns { sql: SQL 片段, values: 参数值数组 }
|
|
34
|
+
*/
|
|
35
|
+
export declare function buildOrderBy<T>(orderBy: OrderBy<T>): {
|
|
36
|
+
sql: string;
|
|
37
|
+
values: any[];
|
|
38
|
+
};
|
|
@@ -2,6 +2,7 @@ import { PoolConnection } from 'mysql2';
|
|
|
2
2
|
import { Table } from '../../table-info';
|
|
3
3
|
import { MixCriteria } from './criteria';
|
|
4
4
|
import { MysqlConfig } from '../../config';
|
|
5
|
+
import { OrderBy } from './order-by';
|
|
5
6
|
export interface MysqlPaginateOpts<T> {
|
|
6
7
|
/**
|
|
7
8
|
* 表
|
|
@@ -24,7 +25,7 @@ export interface MysqlPaginateOpts<T> {
|
|
|
24
25
|
/**
|
|
25
26
|
* 排序规则,按先后顺序放入,每个规则是一个元组,第一个元素是字段名称,第二个元素是顺序
|
|
26
27
|
*/
|
|
27
|
-
orderBy?:
|
|
28
|
+
orderBy?: OrderBy<T>;
|
|
28
29
|
}
|
|
29
30
|
/**
|
|
30
31
|
* mysql 分页查询结果
|
|
@@ -34,3 +35,19 @@ export interface MysqlPage<T> {
|
|
|
34
35
|
list: T[];
|
|
35
36
|
}
|
|
36
37
|
export declare function paginate<T>(config: MysqlConfig, conn: PoolConnection, opts: MysqlPaginateOpts<T>): Promise<MysqlPage<T>>;
|
|
38
|
+
/**
|
|
39
|
+
* 指定字段分页查询选项
|
|
40
|
+
*/
|
|
41
|
+
export interface MysqlPaginateSelectOpts<T, K extends keyof T> extends MysqlPaginateOpts<T> {
|
|
42
|
+
/**
|
|
43
|
+
* 要查询的字段
|
|
44
|
+
*/
|
|
45
|
+
select: K[];
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 指定字段分页查询
|
|
49
|
+
* @param config
|
|
50
|
+
* @param conn
|
|
51
|
+
* @param opts
|
|
52
|
+
*/
|
|
53
|
+
export declare function paginateSelect<T, K extends keyof T>(config: MysqlConfig, conn: PoolConnection, opts: MysqlPaginateSelectOpts<T, K>): Promise<MysqlPage<Pick<T, K>>>;
|
|
@@ -2,6 +2,7 @@ import { PoolConnection } from 'mysql2';
|
|
|
2
2
|
import { Table } from '../../table-info';
|
|
3
3
|
import { MixCriteria } from './criteria';
|
|
4
4
|
import { MysqlConfig } from '../../config';
|
|
5
|
+
import { OrderBy } from './order-by';
|
|
5
6
|
/**
|
|
6
7
|
* 更新
|
|
7
8
|
* @param config
|
|
@@ -15,14 +16,36 @@ export declare function update<T>(config: MysqlConfig, connection: PoolConnectio
|
|
|
15
16
|
* 更新器
|
|
16
17
|
*/
|
|
17
18
|
export type Updater<T> = Partial<{
|
|
18
|
-
[key in keyof T]: T[key] | undefined | ['setNull'] | ['inc', number]
|
|
19
|
+
[key in keyof T]: T[key] | undefined | ['setNull'] | ['inc'] | ['inc', number] | ['now']
|
|
19
20
|
/**
|
|
20
21
|
* 设置一个字段的值,和直接赋值是一样的,作用是解决一些特殊的情况的冲突
|
|
21
22
|
* 比如将 json 字段的值设置为 ['setNull'] ,这就会被认为是要置空,
|
|
22
23
|
* 使用 ['set',['setNull']] 就可以解决这个问题
|
|
23
24
|
*/
|
|
24
|
-
| ['set', T[key]]
|
|
25
|
+
| ['set', T[key]]
|
|
26
|
+
/**
|
|
27
|
+
* 字符串追加,如 ['concat', '/suffix'] 生成 col = CONCAT(IFNULL(col, ''), '/suffix')
|
|
28
|
+
* NULL 安全:字段为 NULL 时视作空字符串
|
|
29
|
+
*/
|
|
30
|
+
| ['concat', string]
|
|
31
|
+
/**
|
|
32
|
+
* 使用自定义表达式
|
|
33
|
+
* 如 ['expr', '?? * ?', ['score', 2]] 生成 score = score * 2
|
|
34
|
+
* 无参数时可省略第三个参数,如 ['expr', 'NOW()'] 等同 ['expr', 'NOW()', []]
|
|
35
|
+
*/
|
|
36
|
+
| ['expr', string, any[]] | ['expr', string];
|
|
25
37
|
}>;
|
|
38
|
+
/**
|
|
39
|
+
* 转换更新器
|
|
40
|
+
* @param table
|
|
41
|
+
* @param updater
|
|
42
|
+
* @param autoUpdateTime 是否自动添加更新时间
|
|
43
|
+
* @returns
|
|
44
|
+
*/
|
|
45
|
+
export declare function updatorToSql<T>(table: Table<T>, updater: Updater<T>): {
|
|
46
|
+
sql: string;
|
|
47
|
+
values: any[];
|
|
48
|
+
};
|
|
26
49
|
/**
|
|
27
50
|
* 部分更新
|
|
28
51
|
* @param connection
|
|
@@ -59,7 +82,7 @@ export interface UpdateOpts<T> {
|
|
|
59
82
|
/**
|
|
60
83
|
* 排序规则,按先后顺序放入,每个规则是一个元组,第一个元素是字段名称,第二个元素是顺序
|
|
61
84
|
*/
|
|
62
|
-
orderBy?:
|
|
85
|
+
orderBy?: OrderBy<T>;
|
|
63
86
|
/**
|
|
64
87
|
* 更新设置
|
|
65
88
|
*/
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { PoolConnection } from 'mysql2';
|
|
2
|
+
import { MysqlConfig } from '../../config';
|
|
3
|
+
import { Table } from '../../table-info';
|
|
4
|
+
import { InsertValue } from './insert';
|
|
5
|
+
import { Updater } from './update';
|
|
6
|
+
/**
|
|
7
|
+
* Upsert 单条数据
|
|
8
|
+
* 如果主键冲突则更新,否则插入
|
|
9
|
+
* @param config
|
|
10
|
+
* @param connection
|
|
11
|
+
* @param table
|
|
12
|
+
* @param data
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
export declare function upsert<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, data: InsertValue<T>): Promise<T>;
|
|
16
|
+
/**
|
|
17
|
+
* Upsert 多条数据
|
|
18
|
+
* 如果主键冲突则更新,否则插入
|
|
19
|
+
* @param config
|
|
20
|
+
* @param connection
|
|
21
|
+
* @param table
|
|
22
|
+
* @param list
|
|
23
|
+
* @returns 影响的行数
|
|
24
|
+
*/
|
|
25
|
+
export declare function upsertMany<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, list: InsertValue<T>[]): Promise<number>;
|
|
26
|
+
/**
|
|
27
|
+
* Upsert 单条数据(支持自定义更新器)
|
|
28
|
+
* 如果主键冲突则按自定义逻辑更新,否则插入
|
|
29
|
+
* @param config
|
|
30
|
+
* @param connection
|
|
31
|
+
* @param table
|
|
32
|
+
* @param data 插入的数据
|
|
33
|
+
* @param updater 冲突时的更新器
|
|
34
|
+
* @returns
|
|
35
|
+
*/
|
|
36
|
+
export declare function upsertWithUpdater<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, data: InsertValue<T>, updater: Updater<T>): Promise<T>;
|