wok-server 0.1.0
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/LICENSE +21 -0
- package/README.md +47 -0
- package/dist/cache/cache.js +94 -0
- package/dist/cache/config.js +19 -0
- package/dist/cache/index.js +27 -0
- package/dist/cache/purge-task.js +56 -0
- package/dist/cache/stat.js +47 -0
- package/dist/config/convert.js +36 -0
- package/dist/config/exception.js +14 -0
- package/dist/config/index.js +67 -0
- package/dist/http-client/index.js +132 -0
- package/dist/i18n/ar.js +17 -0
- package/dist/i18n/de.js +17 -0
- package/dist/i18n/en-us.js +17 -0
- package/dist/i18n/es.js +17 -0
- package/dist/i18n/fr.js +17 -0
- package/dist/i18n/i18n.js +231 -0
- package/dist/i18n/index.js +52 -0
- package/dist/i18n/ja.js +17 -0
- package/dist/i18n/ko.js +17 -0
- package/dist/i18n/msg.js +2 -0
- package/dist/i18n/pt.js +17 -0
- package/dist/i18n/ru.js +17 -0
- package/dist/i18n/tag.js +18 -0
- package/dist/i18n/zh-HK.js +17 -0
- package/dist/i18n/zh-TW.js +17 -0
- package/dist/i18n/zh-cn.js +17 -0
- package/dist/index.js +13 -0
- package/dist/log/config.js +28 -0
- package/dist/log/date.js +21 -0
- package/dist/log/file.js +79 -0
- package/dist/log/index.js +109 -0
- package/dist/log/level.js +39 -0
- package/dist/log/store.js +16 -0
- package/dist/mongodb/collection.js +2 -0
- package/dist/mongodb/config.js +34 -0
- package/dist/mongodb/doc.js +2 -0
- package/dist/mongodb/exception.js +14 -0
- package/dist/mongodb/index.js +58 -0
- package/dist/mongodb/manager/base.js +563 -0
- package/dist/mongodb/manager/index.js +63 -0
- package/dist/mongodb/manager/tx-strict.js +84 -0
- package/dist/mongodb/manager/tx.js +30 -0
- package/dist/mongodb/migration.js +52 -0
- package/dist/mvc/access-log.js +31 -0
- package/dist/mvc/config.js +20 -0
- package/dist/mvc/exchange.js +113 -0
- package/dist/mvc/handler/index.js +6 -0
- package/dist/mvc/handler/json.js +33 -0
- package/dist/mvc/handler/restful.js +35 -0
- package/dist/mvc/handler/upload.js +33 -0
- package/dist/mvc/index.js +316 -0
- package/dist/mvc/interceptor.js +2 -0
- package/dist/mvc/query.js +43 -0
- package/dist/mvc/render/file.js +177 -0
- package/dist/mvc/render/html/html.js +90 -0
- package/dist/mvc/render/html/index.js +18 -0
- package/dist/mvc/render/html/style.js +2 -0
- package/dist/mvc/render/index.js +7 -0
- package/dist/mvc/render/json.js +26 -0
- package/dist/mvc/render/text.js +16 -0
- package/dist/mvc/router.js +2 -0
- package/dist/mysql/config.js +49 -0
- package/dist/mysql/exception.js +14 -0
- package/dist/mysql/index.js +85 -0
- package/dist/mysql/manager/base.js +233 -0
- package/dist/mysql/manager/index.js +107 -0
- package/dist/mysql/manager/ops/count.js +20 -0
- package/dist/mysql/manager/ops/criteria.js +326 -0
- package/dist/mysql/manager/ops/delete.js +65 -0
- package/dist/mysql/manager/ops/exist.js +26 -0
- package/dist/mysql/manager/ops/find.js +111 -0
- package/dist/mysql/manager/ops/index.js +14 -0
- package/dist/mysql/manager/ops/insert.js +101 -0
- package/dist/mysql/manager/ops/modify.js +10 -0
- package/dist/mysql/manager/ops/paginate.js +23 -0
- package/dist/mysql/manager/ops/query.js +9 -0
- package/dist/mysql/manager/ops/update.js +201 -0
- package/dist/mysql/manager/tx-strict.js +98 -0
- package/dist/mysql/manager/tx.js +30 -0
- package/dist/mysql/manager/utils.js +56 -0
- package/dist/mysql/migration.js +136 -0
- package/dist/mysql/table-info.js +8 -0
- package/dist/task/daily.js +58 -0
- package/dist/task/fixed-delay.js +33 -0
- package/dist/task/fixed-rate.js +37 -0
- package/dist/task/index.js +9 -0
- package/dist/task/task.js +39 -0
- package/dist/validation/exception.js +44 -0
- package/dist/validation/index.js +29 -0
- package/dist/validation/validator/array.js +38 -0
- package/dist/validation/validator/enum.js +28 -0
- package/dist/validation/validator/index.js +14 -0
- package/dist/validation/validator/length.js +40 -0
- package/dist/validation/validator/max-length.js +35 -0
- package/dist/validation/validator/max.js +29 -0
- package/dist/validation/validator/min-length.js +33 -0
- package/dist/validation/validator/min.js +29 -0
- package/dist/validation/validator/not-blank.js +33 -0
- package/dist/validation/validator/not-null.js +21 -0
- package/dist/validation/validator/plain-obj.js +32 -0
- package/dist/validation/validator/regexp.js +30 -0
- package/documentation/en/index.md +1 -0
- package/documentation/zh-cn/cache.md +59 -0
- package/documentation/zh-cn/config.md +68 -0
- package/documentation/zh-cn/http-client.md +33 -0
- package/documentation/zh-cn/i18n.md +154 -0
- package/documentation/zh-cn/index.md +25 -0
- package/documentation/zh-cn/log.md +40 -0
- package/documentation/zh-cn/mongodb.md +262 -0
- package/documentation/zh-cn/mvc.md +430 -0
- package/documentation/zh-cn/mysql.md +389 -0
- package/documentation/zh-cn/task.md +50 -0
- package/documentation/zh-cn/test.md +57 -0
- package/documentation/zh-cn/validate.md +125 -0
- package/package.json +46 -0
- package/types/cache/cache.d.ts +52 -0
- package/types/cache/config.d.ts +32 -0
- package/types/cache/index.d.ts +2 -0
- package/types/cache/purge-task.d.ts +11 -0
- package/types/cache/stat.d.ts +26 -0
- package/types/config/convert.d.ts +6 -0
- package/types/config/exception.d.ts +7 -0
- package/types/config/index.d.ts +15 -0
- package/types/http-client/index.d.ts +71 -0
- package/types/i18n/ar.d.ts +2 -0
- package/types/i18n/de.d.ts +2 -0
- package/types/i18n/en-us.d.ts +2 -0
- package/types/i18n/es.d.ts +2 -0
- package/types/i18n/fr.d.ts +2 -0
- package/types/i18n/i18n.d.ts +102 -0
- package/types/i18n/index.d.ts +9 -0
- package/types/i18n/ja.d.ts +2 -0
- package/types/i18n/ko.d.ts +2 -0
- package/types/i18n/msg.d.ts +50 -0
- package/types/i18n/pt.d.ts +2 -0
- package/types/i18n/ru.d.ts +2 -0
- package/types/i18n/tag.d.ts +11 -0
- package/types/i18n/zh-HK.d.ts +2 -0
- package/types/i18n/zh-TW.d.ts +2 -0
- package/types/i18n/zh-cn.d.ts +2 -0
- package/types/index.d.ts +10 -0
- package/types/log/config.d.ts +27 -0
- package/types/log/date.d.ts +2 -0
- package/types/log/file.d.ts +5 -0
- package/types/log/index.d.ts +34 -0
- package/types/log/level.d.ts +15 -0
- package/types/log/store.d.ts +12 -0
- package/types/mongodb/collection.d.ts +25 -0
- package/types/mongodb/config.d.ts +45 -0
- package/types/mongodb/doc.d.ts +11 -0
- package/types/mongodb/exception.d.ts +7 -0
- package/types/mongodb/index.d.ts +29 -0
- package/types/mongodb/manager/base.d.ts +188 -0
- package/types/mongodb/manager/index.d.ts +38 -0
- package/types/mongodb/manager/tx-strict.d.ts +41 -0
- package/types/mongodb/manager/tx.d.ts +21 -0
- package/types/mongodb/migration.d.ts +12 -0
- package/types/mvc/access-log.d.ts +7 -0
- package/types/mvc/config.d.ts +30 -0
- package/types/mvc/exchange.d.ts +72 -0
- package/types/mvc/handler/index.d.ts +3 -0
- package/types/mvc/handler/json.d.ts +23 -0
- package/types/mvc/handler/restful.d.ts +11 -0
- package/types/mvc/handler/upload.d.ts +40 -0
- package/types/mvc/index.d.ts +49 -0
- package/types/mvc/interceptor.d.ts +11 -0
- package/types/mvc/query.d.ts +13 -0
- package/types/mvc/render/file.d.ts +10 -0
- package/types/mvc/render/html/html.d.ts +98 -0
- package/types/mvc/render/html/index.d.ts +11 -0
- package/types/mvc/render/html/style.d.ts +1201 -0
- package/types/mvc/render/index.d.ts +4 -0
- package/types/mvc/render/json.d.ts +17 -0
- package/types/mvc/render/text.d.ts +10 -0
- package/types/mvc/router.d.ts +11 -0
- package/types/mysql/config.d.ts +86 -0
- package/types/mysql/exception.d.ts +7 -0
- package/types/mysql/index.d.ts +16 -0
- package/types/mysql/manager/base.d.ts +158 -0
- package/types/mysql/manager/index.d.ts +36 -0
- package/types/mysql/manager/ops/count.d.ts +13 -0
- package/types/mysql/manager/ops/criteria.d.ts +120 -0
- package/types/mysql/manager/ops/delete.d.ts +46 -0
- package/types/mysql/manager/ops/exist.d.ts +6 -0
- package/types/mysql/manager/ops/find.d.ts +66 -0
- package/types/mysql/manager/ops/index.d.ts +10 -0
- package/types/mysql/manager/ops/insert.d.ts +18 -0
- package/types/mysql/manager/ops/modify.d.ts +3 -0
- package/types/mysql/manager/ops/paginate.d.ts +36 -0
- package/types/mysql/manager/ops/query.d.ts +3 -0
- package/types/mysql/manager/ops/update.d.ts +70 -0
- package/types/mysql/manager/tx-strict.d.ts +34 -0
- package/types/mysql/manager/tx.d.ts +15 -0
- package/types/mysql/manager/utils.d.ts +17 -0
- package/types/mysql/migration.d.ts +8 -0
- package/types/mysql/table-info.d.ts +36 -0
- package/types/task/daily.d.ts +15 -0
- package/types/task/fixed-delay.d.ts +8 -0
- package/types/task/fixed-rate.d.ts +8 -0
- package/types/task/index.d.ts +4 -0
- package/types/task/task.d.ts +33 -0
- package/types/validation/exception.d.ts +43 -0
- package/types/validation/index.d.ts +32 -0
- package/types/validation/validator/array.d.ts +5 -0
- package/types/validation/validator/enum.d.ts +8 -0
- package/types/validation/validator/index.d.ts +11 -0
- package/types/validation/validator/length.d.ts +10 -0
- package/types/validation/validator/max-length.d.ts +8 -0
- package/types/validation/validator/max.d.ts +7 -0
- package/types/validation/validator/min-length.d.ts +6 -0
- package/types/validation/validator/min.d.ts +7 -0
- package/types/validation/validator/not-blank.d.ts +7 -0
- package/types/validation/validator/not-null.d.ts +6 -0
- package/types/validation/validator/plain-obj.d.ts +7 -0
- package/types/validation/validator/regexp.d.ts +8 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { ServerResponse } from 'http';
|
|
3
|
+
/**
|
|
4
|
+
* 渲染 json 格式数据.
|
|
5
|
+
* @param response 响应信息
|
|
6
|
+
* @param json json对象
|
|
7
|
+
* @param status 状态码,默认 200
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare function renderJson(response: ServerResponse, json: any, status?: number): void;
|
|
11
|
+
/**
|
|
12
|
+
* 渲染错误信息,json 格式
|
|
13
|
+
* @param response
|
|
14
|
+
* @param message
|
|
15
|
+
* @param status
|
|
16
|
+
*/
|
|
17
|
+
export declare function renderError(response: ServerResponse, message: string, status?: number): void;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { ServerResponse } from 'http';
|
|
3
|
+
/**
|
|
4
|
+
* 渲染 json 格式数据.
|
|
5
|
+
* @param response 响应信息
|
|
6
|
+
* @param text 文本
|
|
7
|
+
* @param status 状态码,默认 200
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare function renderText(response: ServerResponse, text: string, status?: number): void;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { ValidationOpts } from '../validation';
|
|
2
|
+
/**
|
|
3
|
+
* mysql 配置.
|
|
4
|
+
*/
|
|
5
|
+
export interface MysqlConfig {
|
|
6
|
+
/**
|
|
7
|
+
* 主机名.
|
|
8
|
+
*/
|
|
9
|
+
host: string;
|
|
10
|
+
/**
|
|
11
|
+
* 端口号
|
|
12
|
+
*/
|
|
13
|
+
port: number;
|
|
14
|
+
/**
|
|
15
|
+
* 用户名
|
|
16
|
+
*/
|
|
17
|
+
user: string;
|
|
18
|
+
/**
|
|
19
|
+
* 密码
|
|
20
|
+
*/
|
|
21
|
+
password: string;
|
|
22
|
+
/**
|
|
23
|
+
* 字符集,默认 utf8mb4
|
|
24
|
+
*/
|
|
25
|
+
charset: string;
|
|
26
|
+
/**
|
|
27
|
+
* 库
|
|
28
|
+
*/
|
|
29
|
+
database: string;
|
|
30
|
+
/**
|
|
31
|
+
* 版本控制开启.
|
|
32
|
+
*/
|
|
33
|
+
versionControlEnabled: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* 版本控制的文件目录,默认 db_migration.
|
|
36
|
+
*/
|
|
37
|
+
versionControlDir: string;
|
|
38
|
+
/**
|
|
39
|
+
* 时区,默认 +08:00
|
|
40
|
+
*/
|
|
41
|
+
timezone: string;
|
|
42
|
+
/**
|
|
43
|
+
* 超时时间,单位毫秒,默认 10秒
|
|
44
|
+
*/
|
|
45
|
+
connectTimeout: number;
|
|
46
|
+
/**
|
|
47
|
+
* 调试模式
|
|
48
|
+
*/
|
|
49
|
+
debug: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* 最大连接数
|
|
52
|
+
*/
|
|
53
|
+
connectionLimit: number;
|
|
54
|
+
/**
|
|
55
|
+
* 最大闲置数
|
|
56
|
+
*/
|
|
57
|
+
maxIdle: number;
|
|
58
|
+
/**
|
|
59
|
+
* 闲置的超时时间,也即多久不用算闲置,单位毫秒
|
|
60
|
+
*/
|
|
61
|
+
idleTimeout: number;
|
|
62
|
+
/**
|
|
63
|
+
* 慢 sql 警告,启用后如果一个查询过慢,就会输出警告日志。
|
|
64
|
+
*/
|
|
65
|
+
slowSqlWarn: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* 慢 sql 毫秒数,执行时间超过设定值的 sql 会被认为是慢 sql ,默认 200
|
|
68
|
+
*/
|
|
69
|
+
slowSqlMs: number;
|
|
70
|
+
/**
|
|
71
|
+
* 事务超时时间,单位毫秒,默认 5000,设置为小于等于 0 的值表示不限制
|
|
72
|
+
*/
|
|
73
|
+
transactionTimeout: number;
|
|
74
|
+
/**
|
|
75
|
+
* 事务严格模式
|
|
76
|
+
*/
|
|
77
|
+
transactionStrict: boolean;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 默认配置.
|
|
81
|
+
*/
|
|
82
|
+
export declare const defaultConfig: MysqlConfig;
|
|
83
|
+
/**
|
|
84
|
+
* 配置校验规则.
|
|
85
|
+
*/
|
|
86
|
+
export declare const configValidation: ValidationOpts<MysqlConfig>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { MysqlException } from './exception';
|
|
2
|
+
import { MysqlManager } from './manager';
|
|
3
|
+
import { MysqlCriteria } from './manager/ops/criteria';
|
|
4
|
+
import { Table } from './table-info';
|
|
5
|
+
/**
|
|
6
|
+
* 启用mysql
|
|
7
|
+
* @param name 配置名称,同时也是环境变量配置的前缀,英文开头,由英文数字和下划线成.
|
|
8
|
+
*/
|
|
9
|
+
export declare function enableMysql(name?: string): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* 获取管理器.
|
|
12
|
+
* @param name 配置名称,如果调用 enableMysql 时填写了,那么这里也要填写并保持一致
|
|
13
|
+
*/
|
|
14
|
+
export declare function getMysqlManager(name?: string): MysqlManager;
|
|
15
|
+
export * from './manager';
|
|
16
|
+
export { MysqlCriteria, MysqlException, Table };
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { Pool, PoolConnection } from 'mysql2';
|
|
2
|
+
import { MysqlConfig } from '../config';
|
|
3
|
+
import { Table } from '../table-info';
|
|
4
|
+
import { DeleteManyOpts, FindOpts, MixCriteria, MysqlPage, MysqlPaginateOpts, UpdateOpts, Updater } from './ops';
|
|
5
|
+
/**
|
|
6
|
+
* mysql 管理器基类,提供基础的操作方法.
|
|
7
|
+
*/
|
|
8
|
+
export declare abstract class BaseMysqlManager {
|
|
9
|
+
/**
|
|
10
|
+
* 选项,连接池和连接必须有一个,连接池优先.
|
|
11
|
+
*/
|
|
12
|
+
private readonly opts;
|
|
13
|
+
/**
|
|
14
|
+
* @param mapping 映射
|
|
15
|
+
*/
|
|
16
|
+
constructor(
|
|
17
|
+
/**
|
|
18
|
+
* 选项,连接池和连接必须有一个,连接池优先.
|
|
19
|
+
*/
|
|
20
|
+
opts: {
|
|
21
|
+
config: MysqlConfig;
|
|
22
|
+
pool?: Pool;
|
|
23
|
+
connection?: PoolConnection;
|
|
24
|
+
});
|
|
25
|
+
protected queryWithConnection<T>(queryFn: (conn: PoolConnection) => Promise<T>): Promise<T>;
|
|
26
|
+
/**
|
|
27
|
+
* 依据id查找.
|
|
28
|
+
* @param table 表信息
|
|
29
|
+
* @param id id
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
findById<T>(table: Table<T>, id: string | number): Promise<T | null>;
|
|
33
|
+
/**
|
|
34
|
+
* 根据id列表查询
|
|
35
|
+
* @param table
|
|
36
|
+
* @param ids
|
|
37
|
+
* @returns
|
|
38
|
+
*/
|
|
39
|
+
findByIdIn<T>(table: Table<T>, ids: Array<string | number>): Promise<T[]>;
|
|
40
|
+
/**
|
|
41
|
+
* 根据条件判定是否存在记录.
|
|
42
|
+
* @param table
|
|
43
|
+
* @param criteria 条件信息,可以为空,为空的情况下判定整个表是否有记录
|
|
44
|
+
* @returns
|
|
45
|
+
*/
|
|
46
|
+
existsBy<T>(table: Table<T>, criteria?: MixCriteria<T>): Promise<boolean>;
|
|
47
|
+
/**
|
|
48
|
+
* 根据id判定记录是否存在
|
|
49
|
+
* @param table
|
|
50
|
+
* @param id
|
|
51
|
+
* @returns
|
|
52
|
+
*/
|
|
53
|
+
existsById<T>(table: Table<T>, id: string | number): Promise<boolean>;
|
|
54
|
+
/**
|
|
55
|
+
* 按 id 查询
|
|
56
|
+
* @param table 表信息
|
|
57
|
+
* @param id
|
|
58
|
+
*/
|
|
59
|
+
deleteById<T>(table: Table<T>, id: string | number): Promise<boolean>;
|
|
60
|
+
/**
|
|
61
|
+
* 按条件删除数据。批量删除是危险操作,建议在使用时尽可能设置 limit 参数来限制数量。
|
|
62
|
+
* @param opts
|
|
63
|
+
* @returns
|
|
64
|
+
*/
|
|
65
|
+
deleteMany<T>(opts: DeleteManyOpts<T>): Promise<number>;
|
|
66
|
+
/**
|
|
67
|
+
* 仅删除符合条件的一条记录,必须是相等条件
|
|
68
|
+
* @param table
|
|
69
|
+
* @param criteria
|
|
70
|
+
* @returns
|
|
71
|
+
*/
|
|
72
|
+
deleteOne<T>(table: Table<T>, criteria: Partial<T>): Promise<boolean>;
|
|
73
|
+
/**
|
|
74
|
+
* 查询表中所有数据.
|
|
75
|
+
* @param table 表信息
|
|
76
|
+
* @returns
|
|
77
|
+
*/
|
|
78
|
+
findAll<T>(table: Table<T>): Promise<T[]>;
|
|
79
|
+
/**
|
|
80
|
+
* 按条件查询第一条记录.
|
|
81
|
+
* @param table
|
|
82
|
+
* @param criteria
|
|
83
|
+
* @returns
|
|
84
|
+
*/
|
|
85
|
+
findFirst<T>(table: Table<T>, criteria?: MixCriteria<T>): Promise<T | null>;
|
|
86
|
+
/**
|
|
87
|
+
* 插入数据. 不支持自增加长id,id必须提前生成,请使用 uuid.
|
|
88
|
+
* @param table 表信息
|
|
89
|
+
* @param data 数据,数据必须是 T 的实例, T 必须是已配置的实体类类型,否则无法完成操作
|
|
90
|
+
* @returns 插入后的数据
|
|
91
|
+
*/
|
|
92
|
+
insert<T>(table: Table<T>, data: T): Promise<T>;
|
|
93
|
+
/**
|
|
94
|
+
* 批量插入
|
|
95
|
+
* @param table 表
|
|
96
|
+
* @param list 要插入的数据列表
|
|
97
|
+
*/
|
|
98
|
+
insertMany<T>(table: Table<T>, list: T[]): Promise<void>;
|
|
99
|
+
/**
|
|
100
|
+
* 更新
|
|
101
|
+
* @param table 表信息
|
|
102
|
+
* @param data
|
|
103
|
+
*/
|
|
104
|
+
update<T>(table: Table<T>, data: T): Promise<T>;
|
|
105
|
+
/**
|
|
106
|
+
* 更新所以匹配条件的记录
|
|
107
|
+
* @param opts
|
|
108
|
+
* @returns
|
|
109
|
+
*/
|
|
110
|
+
updateMany<T>(opts: UpdateOpts<T>): Promise<number>;
|
|
111
|
+
/**
|
|
112
|
+
* 只更新一条记录。仅支持相等条件,不支持范围条件,可以严格事务中使用。
|
|
113
|
+
* @param table
|
|
114
|
+
* @param criteria
|
|
115
|
+
* @param updater
|
|
116
|
+
* @returns 更新是否成功
|
|
117
|
+
*/
|
|
118
|
+
updateOne<T>(table: Table<T>, criteria: Partial<T>, updater: Updater<T>): Promise<boolean>;
|
|
119
|
+
/**
|
|
120
|
+
* 部分更新
|
|
121
|
+
* @param table 表信息
|
|
122
|
+
* @param data 字段信息,必须要包含id字段,如果包含了更新和创建时间的字段,则忽略掉
|
|
123
|
+
*/
|
|
124
|
+
partialUpdate<T>(table: Table<T>, data: Updater<T>): Promise<boolean>;
|
|
125
|
+
/**
|
|
126
|
+
* 查找实体类 自定义条件
|
|
127
|
+
* @param opts
|
|
128
|
+
* @returns
|
|
129
|
+
*/
|
|
130
|
+
find<T>(opts: FindOpts<T>): Promise<T[]>;
|
|
131
|
+
/**
|
|
132
|
+
* 指定条件查询数量
|
|
133
|
+
* @param table 表信息
|
|
134
|
+
* @param criteria 查询条件
|
|
135
|
+
* @returns
|
|
136
|
+
*/
|
|
137
|
+
count<T>(table: Table<T>, criteria?: MixCriteria<T>): Promise<number>;
|
|
138
|
+
/**
|
|
139
|
+
* 分页查询
|
|
140
|
+
* @param opts
|
|
141
|
+
* @returns
|
|
142
|
+
*/
|
|
143
|
+
paginate<T>(opts: MysqlPaginateOpts<T>): Promise<MysqlPage<T>>;
|
|
144
|
+
/**
|
|
145
|
+
* 自定义查询,指定 sql 、参数和返回值类型
|
|
146
|
+
* @param sql 预编译 sql ,参数使用 ”?“(英文问号) 占位,注意查询的字段名称会与返回值类型的字段映射,如果 sql 中的字段名称很特殊(比如纯数字等),需要设置别名,避免产生映射错误
|
|
147
|
+
* @param values 要传递的值,对应 sql 中的占位符 ”?“(英文问号)
|
|
148
|
+
* @param T 返回值类型,注意类型映射问题,详细可以参考文档中关于类型映射的说明
|
|
149
|
+
*/
|
|
150
|
+
query<T>(sql: string, values?: any[]): Promise<T[]>;
|
|
151
|
+
/**
|
|
152
|
+
* 自定义 sql 执行,必须是修改类的 sql ,否则会发生错误
|
|
153
|
+
* @param sql
|
|
154
|
+
* @param values
|
|
155
|
+
* @returns 返回影响的行数
|
|
156
|
+
*/
|
|
157
|
+
modify(sql: string, values?: any[]): Promise<number>;
|
|
158
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Pool } from 'mysql2';
|
|
2
|
+
import { MysqlConfig } from '../config';
|
|
3
|
+
import { BaseMysqlManager } from './base';
|
|
4
|
+
import { MysqlTxSession } from './tx';
|
|
5
|
+
/**
|
|
6
|
+
* mysql 管理器,封装数据库操作,提供方便使用的实体类操作方法.
|
|
7
|
+
*/
|
|
8
|
+
export declare class MysqlManager extends BaseMysqlManager {
|
|
9
|
+
private readonly config;
|
|
10
|
+
/**
|
|
11
|
+
* 连接池.
|
|
12
|
+
*/
|
|
13
|
+
private readonly pool;
|
|
14
|
+
/**
|
|
15
|
+
* @param mapping 映射
|
|
16
|
+
*/
|
|
17
|
+
constructor(config: MysqlConfig,
|
|
18
|
+
/**
|
|
19
|
+
* 连接池.
|
|
20
|
+
*/
|
|
21
|
+
pool: Pool);
|
|
22
|
+
/**
|
|
23
|
+
* 事务. 所有事务中的操作都必须使用 exec 函数中提供的 session 对象,直接使用 MysqlManager 上的方法
|
|
24
|
+
* 进行的操作不会在事务中生效.
|
|
25
|
+
* @param ops 逻辑执行函数,内部的所有逻辑都必须在函数内执行完,
|
|
26
|
+
* 不得有操作在函数外异步执行(异步操作不 await,或使用定时器单独执行等),否则不会在事务中生效
|
|
27
|
+
* @param opts 选项,可设置超时时间等
|
|
28
|
+
*/
|
|
29
|
+
tx<T>(ops: (session: MysqlTxSession) => Promise<T>, opts?: {
|
|
30
|
+
timeout?: number;
|
|
31
|
+
isolationLevel?: 'REPEATABLE READ' | 'READ COMMITTED' | 'READ UNCOMMITTED' | 'SERIALIZABLE';
|
|
32
|
+
accessMode?: 'READ WRITE' | 'READ ONLY';
|
|
33
|
+
}): Promise<T>;
|
|
34
|
+
}
|
|
35
|
+
export { FindOpts, MixCriteria, MysqlCriteria } from './ops';
|
|
36
|
+
export { BaseMysqlManager, MysqlTxSession };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PoolConnection } from 'mysql2';
|
|
2
|
+
import { Table } from '../../table-info';
|
|
3
|
+
import { MixCriteria } from './criteria';
|
|
4
|
+
import { MysqlConfig } from '../../config';
|
|
5
|
+
/**
|
|
6
|
+
* 按条件统计数量.
|
|
7
|
+
* @param config
|
|
8
|
+
* @param conn
|
|
9
|
+
* @param table
|
|
10
|
+
* @param criteria
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
export declare function count<T>(config: MysqlConfig, conn: PoolConnection, table: Table<T>, criteria?: MixCriteria<T>): Promise<number>;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
export interface MysqlQuery {
|
|
2
|
+
sql: string;
|
|
3
|
+
values: any[];
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* mysql 查询条件( query criterion ),默认查询条件都是并且关系(and), 部分方法会有例外,在使用的时候请注意方法说明。
|
|
7
|
+
*
|
|
8
|
+
* @param <T> 表类型
|
|
9
|
+
*/
|
|
10
|
+
export declare class MysqlCriteria<T> {
|
|
11
|
+
/**
|
|
12
|
+
* 条件列表.
|
|
13
|
+
*/
|
|
14
|
+
private criteria;
|
|
15
|
+
/**
|
|
16
|
+
* 相等.
|
|
17
|
+
* @param column
|
|
18
|
+
* @param value
|
|
19
|
+
*/
|
|
20
|
+
eq(column: keyof T, value: any): this;
|
|
21
|
+
/**
|
|
22
|
+
* 不等于,注意不相等不能走索引,谨慎使用
|
|
23
|
+
* @param column
|
|
24
|
+
* @param value
|
|
25
|
+
*/
|
|
26
|
+
neq(column: keyof T, value: any): this;
|
|
27
|
+
/**
|
|
28
|
+
* like
|
|
29
|
+
* @param column
|
|
30
|
+
* @param value
|
|
31
|
+
* @returns
|
|
32
|
+
*/
|
|
33
|
+
like(column: keyof T, value: string): this;
|
|
34
|
+
/**
|
|
35
|
+
* not like
|
|
36
|
+
* @param column
|
|
37
|
+
* @param value
|
|
38
|
+
* @returns
|
|
39
|
+
*/
|
|
40
|
+
notLike(column: keyof T, value: string): this;
|
|
41
|
+
/**
|
|
42
|
+
* BETWEEN x and y
|
|
43
|
+
*/
|
|
44
|
+
between(column: keyof T, min: number, max: number): this;
|
|
45
|
+
/**
|
|
46
|
+
* 大于
|
|
47
|
+
* @param column
|
|
48
|
+
* @param value
|
|
49
|
+
*/
|
|
50
|
+
gt(column: keyof T, value: number | Date | string): this;
|
|
51
|
+
/**
|
|
52
|
+
* 大于等于
|
|
53
|
+
* @param column
|
|
54
|
+
* @param value
|
|
55
|
+
*/
|
|
56
|
+
gte(column: keyof T, value: number | Date | string): this;
|
|
57
|
+
/**
|
|
58
|
+
* 小于
|
|
59
|
+
* @param column
|
|
60
|
+
* @param value
|
|
61
|
+
*/
|
|
62
|
+
lt(column: keyof T, value: number | Date | string): this;
|
|
63
|
+
/**
|
|
64
|
+
* 小于等于
|
|
65
|
+
* @param column
|
|
66
|
+
* @param value
|
|
67
|
+
*/
|
|
68
|
+
lte(column: keyof T, value: number | Date | string): this;
|
|
69
|
+
/**
|
|
70
|
+
* in 条件
|
|
71
|
+
* @param column
|
|
72
|
+
* @param values
|
|
73
|
+
*/
|
|
74
|
+
in(column: keyof T, values: Array<string | number>): this;
|
|
75
|
+
/**
|
|
76
|
+
* 嵌入其它的查询条件,与现有的查询条件是或者关系.
|
|
77
|
+
* @param criteria
|
|
78
|
+
*/
|
|
79
|
+
or(orCriteria: (criteria: MysqlCriteria<T>) => void): this;
|
|
80
|
+
/**
|
|
81
|
+
* 嵌入其它的查询条件,与现有的查询条件是并且关系.
|
|
82
|
+
* @param criteria
|
|
83
|
+
*/
|
|
84
|
+
and(andCriteria: (criteria: MysqlCriteria<T>) => void): this;
|
|
85
|
+
/**
|
|
86
|
+
* 字段为空
|
|
87
|
+
* @param field
|
|
88
|
+
* @returns
|
|
89
|
+
*/
|
|
90
|
+
isNull(field: keyof T): this;
|
|
91
|
+
/**
|
|
92
|
+
* 字段非空
|
|
93
|
+
* @param field
|
|
94
|
+
* @returns
|
|
95
|
+
*/
|
|
96
|
+
isNotNull(field: keyof T): this;
|
|
97
|
+
/**
|
|
98
|
+
* 判定是否空,未设置条件.
|
|
99
|
+
* @returns
|
|
100
|
+
*/
|
|
101
|
+
isEmpty(): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* 检查条件信息是否有效,在出错时能给予较详细的提示,以方便排查.
|
|
104
|
+
*/
|
|
105
|
+
check(): void;
|
|
106
|
+
/**
|
|
107
|
+
* 生成查询数据
|
|
108
|
+
*/
|
|
109
|
+
generateQuery(): MysqlQuery;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* 混合的查询对象.
|
|
113
|
+
*/
|
|
114
|
+
export type MixCriteria<T> = Partial<T> | ((criteria: MysqlCriteria<T>) => void) | MysqlCriteria<T>;
|
|
115
|
+
/**
|
|
116
|
+
* 将混合的查询条件转换成查询语句,如果最终构建的条件是空的,返回 undefined
|
|
117
|
+
* @param criteria
|
|
118
|
+
* @returns
|
|
119
|
+
*/
|
|
120
|
+
export declare function buildQuery<T>(criteria: MixCriteria<T>): MysqlQuery | undefined;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { PoolConnection } from 'mysql2';
|
|
2
|
+
import { Table } from '../../table-info';
|
|
3
|
+
import { MixCriteria } from './criteria';
|
|
4
|
+
import { MysqlConfig } from '../../config';
|
|
5
|
+
/**
|
|
6
|
+
* 按 id 删除.
|
|
7
|
+
*
|
|
8
|
+
* @param config
|
|
9
|
+
* @param connection
|
|
10
|
+
* @param mapping
|
|
11
|
+
* @param type
|
|
12
|
+
* @param id
|
|
13
|
+
*/
|
|
14
|
+
export declare function deleteById<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, id: string | number): Promise<boolean>;
|
|
15
|
+
/**
|
|
16
|
+
* 批量删除的选项
|
|
17
|
+
*/
|
|
18
|
+
export interface DeleteManyOpts<T> {
|
|
19
|
+
/**
|
|
20
|
+
* 表.
|
|
21
|
+
*/
|
|
22
|
+
table: Table<T>;
|
|
23
|
+
/**
|
|
24
|
+
* 查询条件
|
|
25
|
+
* @param criteria
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
criteria: MixCriteria<T>;
|
|
29
|
+
/**
|
|
30
|
+
* 限制数量
|
|
31
|
+
*/
|
|
32
|
+
limit?: number;
|
|
33
|
+
/**
|
|
34
|
+
* 排序规则,按先后顺序放入,每个规则是一个元组,第一个元素是字段名称,第二个元素是顺序
|
|
35
|
+
*/
|
|
36
|
+
orderBy?: Array<[keyof T, 'asc' | 'desc']>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 按条件删除,返回被删除的记录数.
|
|
40
|
+
* 务必谨慎使用,大批量删除容易带来性能问题,造成线上事故.
|
|
41
|
+
* @param config
|
|
42
|
+
* @param connection
|
|
43
|
+
* @param table
|
|
44
|
+
* @param criteria
|
|
45
|
+
*/
|
|
46
|
+
export declare function deleteMany<T>(config: MysqlConfig, connection: PoolConnection, opts: DeleteManyOpts<T>): Promise<number>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { PoolConnection } from 'mysql2';
|
|
2
|
+
import { Table } from '../../table-info';
|
|
3
|
+
import { MixCriteria } from './criteria';
|
|
4
|
+
import { MysqlConfig } from '../../config';
|
|
5
|
+
export declare function existsById<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, id: string | number): Promise<boolean>;
|
|
6
|
+
export declare function existsBy<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, criteria?: MixCriteria<T>): Promise<boolean>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { PoolConnection } from 'mysql2';
|
|
2
|
+
import { Table } from '../../table-info';
|
|
3
|
+
import { MixCriteria } from './criteria';
|
|
4
|
+
import { MysqlConfig } from '../../config';
|
|
5
|
+
/**
|
|
6
|
+
* 按 id 查询
|
|
7
|
+
* @param connection
|
|
8
|
+
* @param mapping
|
|
9
|
+
* @param type
|
|
10
|
+
* @param id
|
|
11
|
+
*/
|
|
12
|
+
export declare function findById<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, id: string | number): Promise<T | null>;
|
|
13
|
+
/**
|
|
14
|
+
* 查询表中所有数据。需谨慎操作,全表查询有可能会产生大列表问题。
|
|
15
|
+
* @param connection
|
|
16
|
+
* @param mapping
|
|
17
|
+
* @param type
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
export declare function findAll<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>): Promise<T[]>;
|
|
21
|
+
export interface FindOpts<T> {
|
|
22
|
+
/**
|
|
23
|
+
* 表
|
|
24
|
+
*/
|
|
25
|
+
table: Table<T>;
|
|
26
|
+
/**
|
|
27
|
+
* 查询条件
|
|
28
|
+
* @param criteria
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
criteria?: MixCriteria<T>;
|
|
32
|
+
/**
|
|
33
|
+
* 偏移,需要 limit 有值才可以
|
|
34
|
+
*/
|
|
35
|
+
offset?: number;
|
|
36
|
+
/**
|
|
37
|
+
* 限制返回的数量
|
|
38
|
+
*/
|
|
39
|
+
limit?: number;
|
|
40
|
+
/**
|
|
41
|
+
* 排序规则,按先后顺序放入,每个规则是一个元组,第一个元素是字段名称,第二个元素是顺序
|
|
42
|
+
*/
|
|
43
|
+
orderBy?: Array<[keyof T, 'asc' | 'desc']>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 条件查询
|
|
47
|
+
* @param opts
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
export declare function find<T>(config: MysqlConfig, conn: PoolConnection, opts: FindOpts<T>): Promise<T[]>;
|
|
51
|
+
/**
|
|
52
|
+
* 根据 id 列表查询记录
|
|
53
|
+
* @param connection
|
|
54
|
+
* @param table
|
|
55
|
+
* @param ids
|
|
56
|
+
* @returns
|
|
57
|
+
*/
|
|
58
|
+
export declare function findByIdIn<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, ids: Array<string | number>): Promise<T[]>;
|
|
59
|
+
/**
|
|
60
|
+
* 查找符合条件的第一条记录
|
|
61
|
+
* @param conn
|
|
62
|
+
* @param table
|
|
63
|
+
* @param criteria
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
export declare function findFirst<T>(config: MysqlConfig, conn: PoolConnection, table: Table<T>, criteria?: MixCriteria<T>): Promise<T | null>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './count';
|
|
2
|
+
export * from './criteria';
|
|
3
|
+
export * from './delete';
|
|
4
|
+
export * from './exist';
|
|
5
|
+
export * from './find';
|
|
6
|
+
export * from './insert';
|
|
7
|
+
export * from './modify';
|
|
8
|
+
export * from './paginate';
|
|
9
|
+
export * from './query';
|
|
10
|
+
export * from './update';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { PoolConnection } from 'mysql2';
|
|
2
|
+
import { Table } from '../../table-info';
|
|
3
|
+
import { MysqlConfig } from '../../config';
|
|
4
|
+
/**
|
|
5
|
+
* 为表插入数据
|
|
6
|
+
* @param connection
|
|
7
|
+
* @param table
|
|
8
|
+
* @param data
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
export declare function insert<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, data: T): Promise<T>;
|
|
12
|
+
/**
|
|
13
|
+
* 一次插入多条记录
|
|
14
|
+
* @param connection 连接
|
|
15
|
+
* @param table 表
|
|
16
|
+
* @param list 要插入的记录列表
|
|
17
|
+
*/
|
|
18
|
+
export declare function insertMany<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, list: T[]): Promise<void>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { PoolConnection } from 'mysql2';
|
|
2
|
+
import { Table } from '../../table-info';
|
|
3
|
+
import { MixCriteria } from './criteria';
|
|
4
|
+
import { MysqlConfig } from '../../config';
|
|
5
|
+
export interface MysqlPaginateOpts<T> {
|
|
6
|
+
/**
|
|
7
|
+
* 表
|
|
8
|
+
*/
|
|
9
|
+
table: Table<T>;
|
|
10
|
+
/**
|
|
11
|
+
* 查询条件
|
|
12
|
+
* @param criteria
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
criteria?: MixCriteria<T>;
|
|
16
|
+
/**
|
|
17
|
+
* 而码,默认1
|
|
18
|
+
*/
|
|
19
|
+
pn?: number;
|
|
20
|
+
/**
|
|
21
|
+
* 每页的数据量大小,默认 20, 必须在 1-1000 之间
|
|
22
|
+
*/
|
|
23
|
+
pz?: number;
|
|
24
|
+
/**
|
|
25
|
+
* 排序规则,按先后顺序放入,每个规则是一个元组,第一个元素是字段名称,第二个元素是顺序
|
|
26
|
+
*/
|
|
27
|
+
orderBy?: Array<[keyof T, 'asc' | 'desc']>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* mysql 分页查询结果
|
|
31
|
+
*/
|
|
32
|
+
export interface MysqlPage<T> {
|
|
33
|
+
total: number;
|
|
34
|
+
list: T[];
|
|
35
|
+
}
|
|
36
|
+
export declare function paginate<T>(config: MysqlConfig, conn: PoolConnection, opts: MysqlPaginateOpts<T>): Promise<MysqlPage<T>>;
|