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,233 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseMysqlManager = void 0;
|
|
4
|
+
const exception_1 = require("../exception");
|
|
5
|
+
const ops_1 = require("./ops");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
/**
|
|
8
|
+
* mysql 管理器基类,提供基础的操作方法.
|
|
9
|
+
*/
|
|
10
|
+
class BaseMysqlManager {
|
|
11
|
+
opts;
|
|
12
|
+
/**
|
|
13
|
+
* @param mapping 映射
|
|
14
|
+
*/
|
|
15
|
+
constructor(
|
|
16
|
+
/**
|
|
17
|
+
* 选项,连接池和连接必须有一个,连接池优先.
|
|
18
|
+
*/
|
|
19
|
+
opts) {
|
|
20
|
+
this.opts = opts;
|
|
21
|
+
if (!opts.pool && !opts.connection) {
|
|
22
|
+
throw new exception_1.MysqlException('Pool and Connection cannot be both null.');
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async queryWithConnection(queryFn) {
|
|
26
|
+
let conn;
|
|
27
|
+
if (this.opts.pool) {
|
|
28
|
+
conn = await (0, utils_1.promiseGetConnection)(this.opts.pool);
|
|
29
|
+
}
|
|
30
|
+
else if (this.opts.connection) {
|
|
31
|
+
conn = this.opts.connection;
|
|
32
|
+
}
|
|
33
|
+
if (!conn) {
|
|
34
|
+
throw new exception_1.MysqlException('Unable to obtain connection');
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
return queryFn(conn);
|
|
38
|
+
}
|
|
39
|
+
finally {
|
|
40
|
+
// 如果是连接池来的,每次连接都是新建,必须释放掉
|
|
41
|
+
if (this.opts.pool) {
|
|
42
|
+
this.opts.pool.releaseConnection(conn);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 依据id查找.
|
|
48
|
+
* @param table 表信息
|
|
49
|
+
* @param id id
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
findById(table, id) {
|
|
53
|
+
return this.queryWithConnection(conn => (0, ops_1.findById)(this.opts.config, conn, table, id));
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 根据id列表查询
|
|
57
|
+
* @param table
|
|
58
|
+
* @param ids
|
|
59
|
+
* @returns
|
|
60
|
+
*/
|
|
61
|
+
findByIdIn(table, ids) {
|
|
62
|
+
return this.queryWithConnection(conn => (0, ops_1.findByIdIn)(this.opts.config, conn, table, ids));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* 根据条件判定是否存在记录.
|
|
66
|
+
* @param table
|
|
67
|
+
* @param criteria 条件信息,可以为空,为空的情况下判定整个表是否有记录
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
70
|
+
existsBy(table, criteria) {
|
|
71
|
+
return this.queryWithConnection(conn => (0, ops_1.existsBy)(this.opts.config, conn, table, criteria));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* 根据id判定记录是否存在
|
|
75
|
+
* @param table
|
|
76
|
+
* @param id
|
|
77
|
+
* @returns
|
|
78
|
+
*/
|
|
79
|
+
existsById(table, id) {
|
|
80
|
+
return this.queryWithConnection(conn => (0, ops_1.existsById)(this.opts.config, conn, table, id));
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 按 id 查询
|
|
84
|
+
* @param table 表信息
|
|
85
|
+
* @param id
|
|
86
|
+
*/
|
|
87
|
+
deleteById(table, id) {
|
|
88
|
+
return this.queryWithConnection(conn => (0, ops_1.deleteById)(this.opts.config, conn, table, id));
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 按条件删除数据。批量删除是危险操作,建议在使用时尽可能设置 limit 参数来限制数量。
|
|
92
|
+
* @param opts
|
|
93
|
+
* @returns
|
|
94
|
+
*/
|
|
95
|
+
deleteMany(opts) {
|
|
96
|
+
return this.queryWithConnection(conn => (0, ops_1.deleteMany)(this.opts.config, conn, opts));
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* 仅删除符合条件的一条记录,必须是相等条件
|
|
100
|
+
* @param table
|
|
101
|
+
* @param criteria
|
|
102
|
+
* @returns
|
|
103
|
+
*/
|
|
104
|
+
async deleteOne(table, criteria) {
|
|
105
|
+
if (!Object.keys(criteria).length) {
|
|
106
|
+
throw new exception_1.MysqlException('criteria cannot be empty !');
|
|
107
|
+
}
|
|
108
|
+
const res = await this.queryWithConnection(conn => (0, ops_1.deleteMany)(this.opts.config, conn, {
|
|
109
|
+
table,
|
|
110
|
+
criteria,
|
|
111
|
+
limit: 1
|
|
112
|
+
}));
|
|
113
|
+
return res === 1;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 查询表中所有数据.
|
|
117
|
+
* @param table 表信息
|
|
118
|
+
* @returns
|
|
119
|
+
*/
|
|
120
|
+
findAll(table) {
|
|
121
|
+
return this.queryWithConnection(conn => (0, ops_1.findAll)(this.opts.config, conn, table));
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* 按条件查询第一条记录.
|
|
125
|
+
* @param table
|
|
126
|
+
* @param criteria
|
|
127
|
+
* @returns
|
|
128
|
+
*/
|
|
129
|
+
findFirst(table, criteria) {
|
|
130
|
+
return this.queryWithConnection(conn => (0, ops_1.findFirst)(this.opts.config, conn, table, criteria));
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* 插入数据. 不支持自增加长id,id必须提前生成,请使用 uuid.
|
|
134
|
+
* @param table 表信息
|
|
135
|
+
* @param data 数据,数据必须是 T 的实例, T 必须是已配置的实体类类型,否则无法完成操作
|
|
136
|
+
* @returns 插入后的数据
|
|
137
|
+
*/
|
|
138
|
+
insert(table, data) {
|
|
139
|
+
return this.queryWithConnection(conn => (0, ops_1.insert)(this.opts.config, conn, table, data));
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* 批量插入
|
|
143
|
+
* @param table 表
|
|
144
|
+
* @param list 要插入的数据列表
|
|
145
|
+
*/
|
|
146
|
+
insertMany(table, list) {
|
|
147
|
+
return this.queryWithConnection(conn => (0, ops_1.insertMany)(this.opts.config, conn, table, list));
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* 更新
|
|
151
|
+
* @param table 表信息
|
|
152
|
+
* @param data
|
|
153
|
+
*/
|
|
154
|
+
update(table, data) {
|
|
155
|
+
return this.queryWithConnection(conn => (0, ops_1.update)(this.opts.config, conn, table, data));
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* 更新所以匹配条件的记录
|
|
159
|
+
* @param opts
|
|
160
|
+
* @returns
|
|
161
|
+
*/
|
|
162
|
+
updateMany(opts) {
|
|
163
|
+
return this.queryWithConnection(conn => (0, ops_1.updateMany)(this.opts.config, conn, opts));
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* 只更新一条记录。仅支持相等条件,不支持范围条件,可以严格事务中使用。
|
|
167
|
+
* @param table
|
|
168
|
+
* @param criteria
|
|
169
|
+
* @param updater
|
|
170
|
+
* @returns 更新是否成功
|
|
171
|
+
*/
|
|
172
|
+
updateOne(table, criteria, updater) {
|
|
173
|
+
if (!Object.keys(criteria).length) {
|
|
174
|
+
throw new exception_1.MysqlException('criteria cannot be empty !');
|
|
175
|
+
}
|
|
176
|
+
if (!Object.keys(updater).length) {
|
|
177
|
+
throw new exception_1.MysqlException('updater cannot be empty !');
|
|
178
|
+
}
|
|
179
|
+
return this.queryWithConnection(conn => (0, ops_1.updateOne)(this.opts.config, conn, table, criteria, updater));
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* 部分更新
|
|
183
|
+
* @param table 表信息
|
|
184
|
+
* @param data 字段信息,必须要包含id字段,如果包含了更新和创建时间的字段,则忽略掉
|
|
185
|
+
*/
|
|
186
|
+
partialUpdate(table, data) {
|
|
187
|
+
return this.queryWithConnection(conn => (0, ops_1.partialUpdate)(this.opts.config, conn, table, data));
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* 查找实体类 自定义条件
|
|
191
|
+
* @param opts
|
|
192
|
+
* @returns
|
|
193
|
+
*/
|
|
194
|
+
find(opts) {
|
|
195
|
+
return this.queryWithConnection(conn => (0, ops_1.find)(this.opts.config, conn, opts));
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* 指定条件查询数量
|
|
199
|
+
* @param table 表信息
|
|
200
|
+
* @param criteria 查询条件
|
|
201
|
+
* @returns
|
|
202
|
+
*/
|
|
203
|
+
count(table, criteria) {
|
|
204
|
+
return this.queryWithConnection(conn => (0, ops_1.count)(this.opts.config, conn, table, criteria));
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* 分页查询
|
|
208
|
+
* @param opts
|
|
209
|
+
* @returns
|
|
210
|
+
*/
|
|
211
|
+
paginate(opts) {
|
|
212
|
+
return this.queryWithConnection(conn => (0, ops_1.paginate)(this.opts.config, conn, opts));
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* 自定义查询,指定 sql 、参数和返回值类型
|
|
216
|
+
* @param sql 预编译 sql ,参数使用 ”?“(英文问号) 占位,注意查询的字段名称会与返回值类型的字段映射,如果 sql 中的字段名称很特殊(比如纯数字等),需要设置别名,避免产生映射错误
|
|
217
|
+
* @param values 要传递的值,对应 sql 中的占位符 ”?“(英文问号)
|
|
218
|
+
* @param T 返回值类型,注意类型映射问题,详细可以参考文档中关于类型映射的说明
|
|
219
|
+
*/
|
|
220
|
+
async query(sql, values) {
|
|
221
|
+
return this.queryWithConnection(conn => (0, ops_1.query)(this.opts.config, conn, sql, values));
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* 自定义 sql 执行,必须是修改类的 sql ,否则会发生错误
|
|
225
|
+
* @param sql
|
|
226
|
+
* @param values
|
|
227
|
+
* @returns 返回影响的行数
|
|
228
|
+
*/
|
|
229
|
+
async modify(sql, values) {
|
|
230
|
+
return this.queryWithConnection(conn => (0, ops_1.modify)(this.opts.config, conn, sql, values));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
exports.BaseMysqlManager = BaseMysqlManager;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MysqlTxSession = exports.BaseMysqlManager = exports.MysqlCriteria = exports.MysqlManager = void 0;
|
|
4
|
+
const base_1 = require("./base");
|
|
5
|
+
Object.defineProperty(exports, "BaseMysqlManager", { enumerable: true, get: function () { return base_1.BaseMysqlManager; } });
|
|
6
|
+
const tx_1 = require("./tx");
|
|
7
|
+
Object.defineProperty(exports, "MysqlTxSession", { enumerable: true, get: function () { return tx_1.MysqlTxSession; } });
|
|
8
|
+
const utils_1 = require("./utils");
|
|
9
|
+
const tx_strict_1 = require("./tx-strict");
|
|
10
|
+
const exception_1 = require("../exception");
|
|
11
|
+
/**
|
|
12
|
+
* mysql 管理器,封装数据库操作,提供方便使用的实体类操作方法.
|
|
13
|
+
*/
|
|
14
|
+
class MysqlManager extends base_1.BaseMysqlManager {
|
|
15
|
+
config;
|
|
16
|
+
pool;
|
|
17
|
+
/**
|
|
18
|
+
* @param mapping 映射
|
|
19
|
+
*/
|
|
20
|
+
constructor(config,
|
|
21
|
+
/**
|
|
22
|
+
* 连接池.
|
|
23
|
+
*/
|
|
24
|
+
pool) {
|
|
25
|
+
super({ config, pool });
|
|
26
|
+
this.config = config;
|
|
27
|
+
this.pool = pool;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 事务. 所有事务中的操作都必须使用 exec 函数中提供的 session 对象,直接使用 MysqlManager 上的方法
|
|
31
|
+
* 进行的操作不会在事务中生效.
|
|
32
|
+
* @param ops 逻辑执行函数,内部的所有逻辑都必须在函数内执行完,
|
|
33
|
+
* 不得有操作在函数外异步执行(异步操作不 await,或使用定时器单独执行等),否则不会在事务中生效
|
|
34
|
+
* @param opts 选项,可设置超时时间等
|
|
35
|
+
*/
|
|
36
|
+
async tx(ops, opts) {
|
|
37
|
+
const conn = await (0, utils_1.promiseGetConnection)(this.pool);
|
|
38
|
+
if (opts && opts.isolationLevel) {
|
|
39
|
+
await (0, utils_1.promiseQuery)(this.config, conn, `SET TRANSACTION ISOLATION LEVEL ${opts.isolationLevel}`);
|
|
40
|
+
}
|
|
41
|
+
if (opts && opts.accessMode) {
|
|
42
|
+
await (0, utils_1.promiseQuery)(this.config, conn, `SET TRANSACTION ${opts.accessMode}`);
|
|
43
|
+
}
|
|
44
|
+
await new Promise((resolve, reject) => {
|
|
45
|
+
conn.beginTransaction(err => {
|
|
46
|
+
if (err) {
|
|
47
|
+
reject(err);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
resolve();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
// 构建 session ,然后执行查询
|
|
55
|
+
const session = this.config.transactionStrict
|
|
56
|
+
? new tx_strict_1.MysqlStrictTxSession(this.config, conn)
|
|
57
|
+
: new tx_1.MysqlTxSession(this.config, conn);
|
|
58
|
+
try {
|
|
59
|
+
// 超时抛出异常
|
|
60
|
+
const timeout = opts && typeof opts.timeout === 'number' ? opts.timeout : this.config.transactionTimeout;
|
|
61
|
+
const result = timeout > 0
|
|
62
|
+
? await Promise.race([
|
|
63
|
+
ops(session),
|
|
64
|
+
new Promise((resolve, reject) => {
|
|
65
|
+
setTimeout(() => {
|
|
66
|
+
// 立即中止会话,防止再有后续操作
|
|
67
|
+
session.abort();
|
|
68
|
+
reject(new exception_1.MysqlException('Transaction timeout !'));
|
|
69
|
+
}, timeout);
|
|
70
|
+
})
|
|
71
|
+
])
|
|
72
|
+
: await ops(session);
|
|
73
|
+
await new Promise((resolve, reject) => {
|
|
74
|
+
conn.commit(err => {
|
|
75
|
+
if (err) {
|
|
76
|
+
reject(err);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
resolve();
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
await new Promise((resolve, reject) => {
|
|
87
|
+
conn.rollback(err => {
|
|
88
|
+
if (err) {
|
|
89
|
+
reject(err);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
resolve();
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
throw e;
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
// 无论如何中止会话,离开事务,会话就不能再被使用
|
|
100
|
+
session.abort();
|
|
101
|
+
this.pool.releaseConnection(conn);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
exports.MysqlManager = MysqlManager;
|
|
106
|
+
var ops_1 = require("./ops");
|
|
107
|
+
Object.defineProperty(exports, "MysqlCriteria", { enumerable: true, get: function () { return ops_1.MysqlCriteria; } });
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.count = void 0;
|
|
4
|
+
const criteria_1 = require("./criteria");
|
|
5
|
+
const utils_1 = require("../utils");
|
|
6
|
+
/**
|
|
7
|
+
* 按条件统计数量.
|
|
8
|
+
* @param config
|
|
9
|
+
* @param conn
|
|
10
|
+
* @param table
|
|
11
|
+
* @param criteria
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
async function count(config, conn, table, criteria) {
|
|
15
|
+
let query = criteria ? (0, criteria_1.buildQuery)(criteria) : undefined;
|
|
16
|
+
const res = await (0, utils_1.promiseQuery)(config, conn, `select count(*) as ct from ?? ${query ? `where ${query.sql}` : ''} `, [table.tableName].concat(query ? query.values : []));
|
|
17
|
+
const row = res;
|
|
18
|
+
return row[0].ct;
|
|
19
|
+
}
|
|
20
|
+
exports.count = count;
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildQuery = exports.MysqlCriteria = void 0;
|
|
4
|
+
const exception_1 = require("../../exception");
|
|
5
|
+
/**
|
|
6
|
+
* mysql 查询条件( query criterion ),默认查询条件都是并且关系(and), 部分方法会有例外,在使用的时候请注意方法说明。
|
|
7
|
+
*
|
|
8
|
+
* @param <T> 表类型
|
|
9
|
+
*/
|
|
10
|
+
class MysqlCriteria {
|
|
11
|
+
/**
|
|
12
|
+
* 条件列表.
|
|
13
|
+
*/
|
|
14
|
+
criteria = [];
|
|
15
|
+
/**
|
|
16
|
+
* 相等.
|
|
17
|
+
* @param column
|
|
18
|
+
* @param value
|
|
19
|
+
*/
|
|
20
|
+
eq(column, value) {
|
|
21
|
+
this.criteria.push({ type: 'eq', columnName: column, value });
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 不等于,注意不相等不能走索引,谨慎使用
|
|
26
|
+
* @param column
|
|
27
|
+
* @param value
|
|
28
|
+
*/
|
|
29
|
+
neq(column, value) {
|
|
30
|
+
this.criteria.push({ type: 'neq', columnName: column, value });
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* like
|
|
35
|
+
* @param column
|
|
36
|
+
* @param value
|
|
37
|
+
* @returns
|
|
38
|
+
*/
|
|
39
|
+
like(column, value) {
|
|
40
|
+
this.criteria.push({ type: 'like', columnName: column, value });
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* not like
|
|
45
|
+
* @param column
|
|
46
|
+
* @param value
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
notLike(column, value) {
|
|
50
|
+
this.criteria.push({ type: 'notLike', columnName: column, value });
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* BETWEEN x and y
|
|
55
|
+
*/
|
|
56
|
+
between(column, min, max) {
|
|
57
|
+
this.criteria.push({ type: 'between', columnName: column, value: [min, max] });
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 大于
|
|
62
|
+
* @param column
|
|
63
|
+
* @param value
|
|
64
|
+
*/
|
|
65
|
+
gt(column, value) {
|
|
66
|
+
this.criteria.push({ type: 'gt', columnName: column, value });
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 大于等于
|
|
71
|
+
* @param column
|
|
72
|
+
* @param value
|
|
73
|
+
*/
|
|
74
|
+
gte(column, value) {
|
|
75
|
+
this.criteria.push({ type: 'gte', columnName: column, value });
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 小于
|
|
80
|
+
* @param column
|
|
81
|
+
* @param value
|
|
82
|
+
*/
|
|
83
|
+
lt(column, value) {
|
|
84
|
+
this.criteria.push({ type: 'lt', columnName: column, value });
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* 小于等于
|
|
89
|
+
* @param column
|
|
90
|
+
* @param value
|
|
91
|
+
*/
|
|
92
|
+
lte(column, value) {
|
|
93
|
+
this.criteria.push({ type: 'lte', columnName: column, value });
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* in 条件
|
|
98
|
+
* @param column
|
|
99
|
+
* @param values
|
|
100
|
+
*/
|
|
101
|
+
in(column, values) {
|
|
102
|
+
this.criteria.push({ type: 'in', columnName: column, value: values });
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 嵌入其它的查询条件,与现有的查询条件是或者关系.
|
|
107
|
+
* @param criteria
|
|
108
|
+
*/
|
|
109
|
+
or(orCriteria) {
|
|
110
|
+
const criteria = new MysqlCriteria();
|
|
111
|
+
orCriteria(criteria);
|
|
112
|
+
this.criteria.push({ type: 'or', criteria });
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 嵌入其它的查询条件,与现有的查询条件是并且关系.
|
|
117
|
+
* @param criteria
|
|
118
|
+
*/
|
|
119
|
+
and(andCriteria) {
|
|
120
|
+
const criteria = new MysqlCriteria();
|
|
121
|
+
andCriteria(criteria);
|
|
122
|
+
this.criteria.push({ type: 'and', criteria });
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 字段为空
|
|
127
|
+
* @param field
|
|
128
|
+
* @returns
|
|
129
|
+
*/
|
|
130
|
+
isNull(field) {
|
|
131
|
+
this.criteria.push({ type: 'isNull', columnName: field });
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 字段非空
|
|
136
|
+
* @param field
|
|
137
|
+
* @returns
|
|
138
|
+
*/
|
|
139
|
+
isNotNull(field) {
|
|
140
|
+
this.criteria.push({ type: 'isNotNull', columnName: field });
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 判定是否空,未设置条件.
|
|
145
|
+
* @returns
|
|
146
|
+
*/
|
|
147
|
+
isEmpty() {
|
|
148
|
+
return !this.criteria.length;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* 检查条件信息是否有效,在出错时能给予较详细的提示,以方便排查.
|
|
152
|
+
*/
|
|
153
|
+
check() {
|
|
154
|
+
for (const criterion of this.criteria) {
|
|
155
|
+
if (criterion.type === 'or' || criterion.type === 'and') {
|
|
156
|
+
if (!criterion.criteria) {
|
|
157
|
+
throw new exception_1.MysqlException(`${criterion.type} clause cannot be empty`);
|
|
158
|
+
}
|
|
159
|
+
criterion.criteria.check();
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (!criterion.columnName) {
|
|
163
|
+
throw new exception_1.MysqlException('The column name of the query criteria cannot be blank.');
|
|
164
|
+
}
|
|
165
|
+
if (criterion.type === 'isNull' || criterion.type === 'isNotNull') {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (criterion.type === 'in') {
|
|
169
|
+
if (!Array.isArray(criterion.value)) {
|
|
170
|
+
throw new exception_1.MysqlException(`Invalid in condition,the condition value is not a array type,column name:${criterion.columnName.toString()}`);
|
|
171
|
+
}
|
|
172
|
+
if (!criterion.value.length) {
|
|
173
|
+
throw new exception_1.MysqlException(`Invalid in condition,the condition value cannot be an empty array,column name:${criterion.columnName.toString()}`);
|
|
174
|
+
}
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (criterion.type === 'between') {
|
|
178
|
+
if (!Array.isArray(criterion.value)) {
|
|
179
|
+
throw new exception_1.MysqlException(`Invalid between condition,the condition value is not an array type,column name:${criterion.columnName.toString()}, value:${criterion.value}`);
|
|
180
|
+
}
|
|
181
|
+
if (criterion.value.length !== 2) {
|
|
182
|
+
throw new exception_1.MysqlException(`Invalid between condition,the condition value must be an array of length 2,column: ${criterion.columnName.toString()},value:${criterion.value.length}`);
|
|
183
|
+
}
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (typeof criterion.value !== 'number' &&
|
|
187
|
+
typeof criterion.value !== 'string' &&
|
|
188
|
+
!(criterion.value instanceof Date)) {
|
|
189
|
+
throw new exception_1.MysqlException('The value of the query criteria is invalid,only number,string and Date are supported,' +
|
|
190
|
+
`column name : ${criterion.columnName.toString()},value : ${criterion.value} .`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* 生成查询数据
|
|
196
|
+
*/
|
|
197
|
+
generateQuery() {
|
|
198
|
+
this.check();
|
|
199
|
+
const sqlFragments = [];
|
|
200
|
+
const values = [];
|
|
201
|
+
for (const criterion of this.criteria) {
|
|
202
|
+
// 普通的查询
|
|
203
|
+
if (criterion.columnName && criterion.value !== undefined) {
|
|
204
|
+
// between 特殊处理
|
|
205
|
+
if (criterion.type === 'between') {
|
|
206
|
+
sqlFragments.push('and ?? between ? and ? ');
|
|
207
|
+
values.push(criterion.columnName, criterion.value[0], criterion.value[1]);
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
// 符号
|
|
211
|
+
let sign = '';
|
|
212
|
+
if (criterion.type === 'eq') {
|
|
213
|
+
sign = '=';
|
|
214
|
+
}
|
|
215
|
+
else if (criterion.type === 'neq') {
|
|
216
|
+
sign = '!=';
|
|
217
|
+
}
|
|
218
|
+
else if (criterion.type === 'gt') {
|
|
219
|
+
sign = '>';
|
|
220
|
+
}
|
|
221
|
+
else if (criterion.type === 'gte') {
|
|
222
|
+
sign = '>=';
|
|
223
|
+
}
|
|
224
|
+
else if (criterion.type === 'lt') {
|
|
225
|
+
sign = '<';
|
|
226
|
+
}
|
|
227
|
+
else if (criterion.type === 'lte') {
|
|
228
|
+
sign = '<=';
|
|
229
|
+
}
|
|
230
|
+
else if (criterion.type === 'in') {
|
|
231
|
+
sign = 'in';
|
|
232
|
+
}
|
|
233
|
+
else if (criterion.type === 'like') {
|
|
234
|
+
sign = 'like';
|
|
235
|
+
}
|
|
236
|
+
else if (criterion.type === 'notLike') {
|
|
237
|
+
sign = 'not like';
|
|
238
|
+
}
|
|
239
|
+
if (sign) {
|
|
240
|
+
if (criterion.type === 'in') {
|
|
241
|
+
sqlFragments.push(`and ?? ${sign} (?) `);
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
sqlFragments.push(`and ?? ${sign} ? `);
|
|
245
|
+
}
|
|
246
|
+
values.push(criterion.columnName, criterion.value);
|
|
247
|
+
}
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
else if (criterion.columnName) {
|
|
251
|
+
if (criterion.type === 'isNull') {
|
|
252
|
+
sqlFragments.push(`and ?? is null `);
|
|
253
|
+
values.push(criterion.columnName);
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (criterion.type === 'isNotNull') {
|
|
257
|
+
sqlFragments.push(`and ?? is not null `);
|
|
258
|
+
values.push(criterion.columnName);
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// 特殊查询 or 和 and
|
|
263
|
+
else if (criterion.criteria) {
|
|
264
|
+
const query = criterion.criteria.generateQuery();
|
|
265
|
+
if (criterion.type === 'or') {
|
|
266
|
+
sqlFragments.push(`or (${query.sql}) `);
|
|
267
|
+
values.push(...query.values);
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (criterion.type === 'and') {
|
|
271
|
+
sqlFragments.push(`and (${query.sql}) `);
|
|
272
|
+
values.push(...query.values);
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (!sqlFragments.length) {
|
|
278
|
+
throw new exception_1.MysqlException('No valid query criteria have been set.');
|
|
279
|
+
}
|
|
280
|
+
let sql = sqlFragments.join('');
|
|
281
|
+
// 去除掉第一个条件的连接关键字,可能是 and 或 or
|
|
282
|
+
if (sql.startsWith('and')) {
|
|
283
|
+
sql = sql.substring(3);
|
|
284
|
+
}
|
|
285
|
+
else if (sql.startsWith('or')) {
|
|
286
|
+
sql = sql.substring(2);
|
|
287
|
+
}
|
|
288
|
+
return { sql, values };
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
exports.MysqlCriteria = MysqlCriteria;
|
|
292
|
+
function convertToCriteria(example) {
|
|
293
|
+
const criteria = new MysqlCriteria();
|
|
294
|
+
Object.entries(example).forEach(entry => {
|
|
295
|
+
const [key, value] = entry;
|
|
296
|
+
criteria.eq(key, value);
|
|
297
|
+
});
|
|
298
|
+
return criteria;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* 将混合的查询条件转换成查询语句,如果最终构建的条件是空的,返回 undefined
|
|
302
|
+
* @param criteria
|
|
303
|
+
* @returns
|
|
304
|
+
*/
|
|
305
|
+
function buildQuery(criteria) {
|
|
306
|
+
if (criteria instanceof Function) {
|
|
307
|
+
const c = new MysqlCriteria();
|
|
308
|
+
criteria(c);
|
|
309
|
+
if (!c.isEmpty()) {
|
|
310
|
+
return c.generateQuery();
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
else if (criteria instanceof MysqlCriteria) {
|
|
314
|
+
if (!criteria.isEmpty()) {
|
|
315
|
+
return criteria.generateQuery();
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
const c = convertToCriteria(criteria);
|
|
320
|
+
if (!c.isEmpty()) {
|
|
321
|
+
return c.generateQuery();
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return undefined;
|
|
325
|
+
}
|
|
326
|
+
exports.buildQuery = buildQuery;
|