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.
Files changed (216) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +47 -0
  3. package/dist/cache/cache.js +94 -0
  4. package/dist/cache/config.js +19 -0
  5. package/dist/cache/index.js +27 -0
  6. package/dist/cache/purge-task.js +56 -0
  7. package/dist/cache/stat.js +47 -0
  8. package/dist/config/convert.js +36 -0
  9. package/dist/config/exception.js +14 -0
  10. package/dist/config/index.js +67 -0
  11. package/dist/http-client/index.js +132 -0
  12. package/dist/i18n/ar.js +17 -0
  13. package/dist/i18n/de.js +17 -0
  14. package/dist/i18n/en-us.js +17 -0
  15. package/dist/i18n/es.js +17 -0
  16. package/dist/i18n/fr.js +17 -0
  17. package/dist/i18n/i18n.js +231 -0
  18. package/dist/i18n/index.js +52 -0
  19. package/dist/i18n/ja.js +17 -0
  20. package/dist/i18n/ko.js +17 -0
  21. package/dist/i18n/msg.js +2 -0
  22. package/dist/i18n/pt.js +17 -0
  23. package/dist/i18n/ru.js +17 -0
  24. package/dist/i18n/tag.js +18 -0
  25. package/dist/i18n/zh-HK.js +17 -0
  26. package/dist/i18n/zh-TW.js +17 -0
  27. package/dist/i18n/zh-cn.js +17 -0
  28. package/dist/index.js +13 -0
  29. package/dist/log/config.js +28 -0
  30. package/dist/log/date.js +21 -0
  31. package/dist/log/file.js +79 -0
  32. package/dist/log/index.js +109 -0
  33. package/dist/log/level.js +39 -0
  34. package/dist/log/store.js +16 -0
  35. package/dist/mongodb/collection.js +2 -0
  36. package/dist/mongodb/config.js +34 -0
  37. package/dist/mongodb/doc.js +2 -0
  38. package/dist/mongodb/exception.js +14 -0
  39. package/dist/mongodb/index.js +58 -0
  40. package/dist/mongodb/manager/base.js +563 -0
  41. package/dist/mongodb/manager/index.js +63 -0
  42. package/dist/mongodb/manager/tx-strict.js +84 -0
  43. package/dist/mongodb/manager/tx.js +30 -0
  44. package/dist/mongodb/migration.js +52 -0
  45. package/dist/mvc/access-log.js +31 -0
  46. package/dist/mvc/config.js +20 -0
  47. package/dist/mvc/exchange.js +113 -0
  48. package/dist/mvc/handler/index.js +6 -0
  49. package/dist/mvc/handler/json.js +33 -0
  50. package/dist/mvc/handler/restful.js +35 -0
  51. package/dist/mvc/handler/upload.js +33 -0
  52. package/dist/mvc/index.js +316 -0
  53. package/dist/mvc/interceptor.js +2 -0
  54. package/dist/mvc/query.js +43 -0
  55. package/dist/mvc/render/file.js +177 -0
  56. package/dist/mvc/render/html/html.js +90 -0
  57. package/dist/mvc/render/html/index.js +18 -0
  58. package/dist/mvc/render/html/style.js +2 -0
  59. package/dist/mvc/render/index.js +7 -0
  60. package/dist/mvc/render/json.js +26 -0
  61. package/dist/mvc/render/text.js +16 -0
  62. package/dist/mvc/router.js +2 -0
  63. package/dist/mysql/config.js +49 -0
  64. package/dist/mysql/exception.js +14 -0
  65. package/dist/mysql/index.js +85 -0
  66. package/dist/mysql/manager/base.js +233 -0
  67. package/dist/mysql/manager/index.js +107 -0
  68. package/dist/mysql/manager/ops/count.js +20 -0
  69. package/dist/mysql/manager/ops/criteria.js +326 -0
  70. package/dist/mysql/manager/ops/delete.js +65 -0
  71. package/dist/mysql/manager/ops/exist.js +26 -0
  72. package/dist/mysql/manager/ops/find.js +111 -0
  73. package/dist/mysql/manager/ops/index.js +14 -0
  74. package/dist/mysql/manager/ops/insert.js +101 -0
  75. package/dist/mysql/manager/ops/modify.js +10 -0
  76. package/dist/mysql/manager/ops/paginate.js +23 -0
  77. package/dist/mysql/manager/ops/query.js +9 -0
  78. package/dist/mysql/manager/ops/update.js +201 -0
  79. package/dist/mysql/manager/tx-strict.js +98 -0
  80. package/dist/mysql/manager/tx.js +30 -0
  81. package/dist/mysql/manager/utils.js +56 -0
  82. package/dist/mysql/migration.js +136 -0
  83. package/dist/mysql/table-info.js +8 -0
  84. package/dist/task/daily.js +58 -0
  85. package/dist/task/fixed-delay.js +33 -0
  86. package/dist/task/fixed-rate.js +37 -0
  87. package/dist/task/index.js +9 -0
  88. package/dist/task/task.js +39 -0
  89. package/dist/validation/exception.js +44 -0
  90. package/dist/validation/index.js +29 -0
  91. package/dist/validation/validator/array.js +38 -0
  92. package/dist/validation/validator/enum.js +28 -0
  93. package/dist/validation/validator/index.js +14 -0
  94. package/dist/validation/validator/length.js +40 -0
  95. package/dist/validation/validator/max-length.js +35 -0
  96. package/dist/validation/validator/max.js +29 -0
  97. package/dist/validation/validator/min-length.js +33 -0
  98. package/dist/validation/validator/min.js +29 -0
  99. package/dist/validation/validator/not-blank.js +33 -0
  100. package/dist/validation/validator/not-null.js +21 -0
  101. package/dist/validation/validator/plain-obj.js +32 -0
  102. package/dist/validation/validator/regexp.js +30 -0
  103. package/documentation/en/index.md +1 -0
  104. package/documentation/zh-cn/cache.md +59 -0
  105. package/documentation/zh-cn/config.md +68 -0
  106. package/documentation/zh-cn/http-client.md +33 -0
  107. package/documentation/zh-cn/i18n.md +154 -0
  108. package/documentation/zh-cn/index.md +25 -0
  109. package/documentation/zh-cn/log.md +40 -0
  110. package/documentation/zh-cn/mongodb.md +262 -0
  111. package/documentation/zh-cn/mvc.md +430 -0
  112. package/documentation/zh-cn/mysql.md +389 -0
  113. package/documentation/zh-cn/task.md +50 -0
  114. package/documentation/zh-cn/test.md +57 -0
  115. package/documentation/zh-cn/validate.md +125 -0
  116. package/package.json +46 -0
  117. package/types/cache/cache.d.ts +52 -0
  118. package/types/cache/config.d.ts +32 -0
  119. package/types/cache/index.d.ts +2 -0
  120. package/types/cache/purge-task.d.ts +11 -0
  121. package/types/cache/stat.d.ts +26 -0
  122. package/types/config/convert.d.ts +6 -0
  123. package/types/config/exception.d.ts +7 -0
  124. package/types/config/index.d.ts +15 -0
  125. package/types/http-client/index.d.ts +71 -0
  126. package/types/i18n/ar.d.ts +2 -0
  127. package/types/i18n/de.d.ts +2 -0
  128. package/types/i18n/en-us.d.ts +2 -0
  129. package/types/i18n/es.d.ts +2 -0
  130. package/types/i18n/fr.d.ts +2 -0
  131. package/types/i18n/i18n.d.ts +102 -0
  132. package/types/i18n/index.d.ts +9 -0
  133. package/types/i18n/ja.d.ts +2 -0
  134. package/types/i18n/ko.d.ts +2 -0
  135. package/types/i18n/msg.d.ts +50 -0
  136. package/types/i18n/pt.d.ts +2 -0
  137. package/types/i18n/ru.d.ts +2 -0
  138. package/types/i18n/tag.d.ts +11 -0
  139. package/types/i18n/zh-HK.d.ts +2 -0
  140. package/types/i18n/zh-TW.d.ts +2 -0
  141. package/types/i18n/zh-cn.d.ts +2 -0
  142. package/types/index.d.ts +10 -0
  143. package/types/log/config.d.ts +27 -0
  144. package/types/log/date.d.ts +2 -0
  145. package/types/log/file.d.ts +5 -0
  146. package/types/log/index.d.ts +34 -0
  147. package/types/log/level.d.ts +15 -0
  148. package/types/log/store.d.ts +12 -0
  149. package/types/mongodb/collection.d.ts +25 -0
  150. package/types/mongodb/config.d.ts +45 -0
  151. package/types/mongodb/doc.d.ts +11 -0
  152. package/types/mongodb/exception.d.ts +7 -0
  153. package/types/mongodb/index.d.ts +29 -0
  154. package/types/mongodb/manager/base.d.ts +188 -0
  155. package/types/mongodb/manager/index.d.ts +38 -0
  156. package/types/mongodb/manager/tx-strict.d.ts +41 -0
  157. package/types/mongodb/manager/tx.d.ts +21 -0
  158. package/types/mongodb/migration.d.ts +12 -0
  159. package/types/mvc/access-log.d.ts +7 -0
  160. package/types/mvc/config.d.ts +30 -0
  161. package/types/mvc/exchange.d.ts +72 -0
  162. package/types/mvc/handler/index.d.ts +3 -0
  163. package/types/mvc/handler/json.d.ts +23 -0
  164. package/types/mvc/handler/restful.d.ts +11 -0
  165. package/types/mvc/handler/upload.d.ts +40 -0
  166. package/types/mvc/index.d.ts +49 -0
  167. package/types/mvc/interceptor.d.ts +11 -0
  168. package/types/mvc/query.d.ts +13 -0
  169. package/types/mvc/render/file.d.ts +10 -0
  170. package/types/mvc/render/html/html.d.ts +98 -0
  171. package/types/mvc/render/html/index.d.ts +11 -0
  172. package/types/mvc/render/html/style.d.ts +1201 -0
  173. package/types/mvc/render/index.d.ts +4 -0
  174. package/types/mvc/render/json.d.ts +17 -0
  175. package/types/mvc/render/text.d.ts +10 -0
  176. package/types/mvc/router.d.ts +11 -0
  177. package/types/mysql/config.d.ts +86 -0
  178. package/types/mysql/exception.d.ts +7 -0
  179. package/types/mysql/index.d.ts +16 -0
  180. package/types/mysql/manager/base.d.ts +158 -0
  181. package/types/mysql/manager/index.d.ts +36 -0
  182. package/types/mysql/manager/ops/count.d.ts +13 -0
  183. package/types/mysql/manager/ops/criteria.d.ts +120 -0
  184. package/types/mysql/manager/ops/delete.d.ts +46 -0
  185. package/types/mysql/manager/ops/exist.d.ts +6 -0
  186. package/types/mysql/manager/ops/find.d.ts +66 -0
  187. package/types/mysql/manager/ops/index.d.ts +10 -0
  188. package/types/mysql/manager/ops/insert.d.ts +18 -0
  189. package/types/mysql/manager/ops/modify.d.ts +3 -0
  190. package/types/mysql/manager/ops/paginate.d.ts +36 -0
  191. package/types/mysql/manager/ops/query.d.ts +3 -0
  192. package/types/mysql/manager/ops/update.d.ts +70 -0
  193. package/types/mysql/manager/tx-strict.d.ts +34 -0
  194. package/types/mysql/manager/tx.d.ts +15 -0
  195. package/types/mysql/manager/utils.d.ts +17 -0
  196. package/types/mysql/migration.d.ts +8 -0
  197. package/types/mysql/table-info.d.ts +36 -0
  198. package/types/task/daily.d.ts +15 -0
  199. package/types/task/fixed-delay.d.ts +8 -0
  200. package/types/task/fixed-rate.d.ts +8 -0
  201. package/types/task/index.d.ts +4 -0
  202. package/types/task/task.d.ts +33 -0
  203. package/types/validation/exception.d.ts +43 -0
  204. package/types/validation/index.d.ts +32 -0
  205. package/types/validation/validator/array.d.ts +5 -0
  206. package/types/validation/validator/enum.d.ts +8 -0
  207. package/types/validation/validator/index.d.ts +11 -0
  208. package/types/validation/validator/length.d.ts +10 -0
  209. package/types/validation/validator/max-length.d.ts +8 -0
  210. package/types/validation/validator/max.d.ts +7 -0
  211. package/types/validation/validator/min-length.d.ts +6 -0
  212. package/types/validation/validator/min.d.ts +7 -0
  213. package/types/validation/validator/not-blank.d.ts +7 -0
  214. package/types/validation/validator/not-null.d.ts +6 -0
  215. package/types/validation/validator/plain-obj.d.ts +7 -0
  216. package/types/validation/validator/regexp.d.ts +8 -0
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deleteMany = exports.deleteById = void 0;
4
+ const exception_1 = require("../../exception");
5
+ const utils_1 = require("../utils");
6
+ const criteria_1 = require("./criteria");
7
+ /**
8
+ * 按 id 删除.
9
+ *
10
+ * @param config
11
+ * @param connection
12
+ * @param mapping
13
+ * @param type
14
+ * @param id
15
+ */
16
+ async function deleteById(config, connection, table, id) {
17
+ const res = await (0, utils_1.promiseQuery)(config, connection, `delete from ?? where ?? = ?`, [
18
+ table.tableName,
19
+ table.id,
20
+ id
21
+ ]);
22
+ const ok = res;
23
+ return ok.affectedRows === 1;
24
+ }
25
+ exports.deleteById = deleteById;
26
+ /**
27
+ * 按条件删除,返回被删除的记录数.
28
+ * 务必谨慎使用,大批量删除容易带来性能问题,造成线上事故.
29
+ * @param config
30
+ * @param connection
31
+ * @param table
32
+ * @param criteria
33
+ */
34
+ async function deleteMany(config, connection, opts) {
35
+ let sql = 'delete from ?? ';
36
+ const values = [opts.table.tableName];
37
+ let query = (0, criteria_1.buildQuery)(opts.criteria);
38
+ if (!query) {
39
+ throw new exception_1.MysqlException('No valid criteria specified.');
40
+ }
41
+ if (query) {
42
+ sql += ` where ${query.sql} `;
43
+ values.push(...query.values);
44
+ }
45
+ // 排序
46
+ if (opts.orderBy && opts.orderBy.length) {
47
+ opts.orderBy.forEach((orderBy, idx) => {
48
+ const [field, sort] = orderBy;
49
+ if (idx == 0) {
50
+ sql += ` order by ?? ${sort} `;
51
+ }
52
+ else {
53
+ sql += ` , ?? ${sort} `;
54
+ }
55
+ values.push(field);
56
+ });
57
+ }
58
+ // 数量限制
59
+ if (opts.limit) {
60
+ sql += ` limit ${opts.limit} `;
61
+ }
62
+ const res = await (0, utils_1.promiseQuery)(config, connection, sql, values);
63
+ return res.affectedRows;
64
+ }
65
+ exports.deleteMany = deleteMany;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.existsBy = exports.existsById = void 0;
4
+ const criteria_1 = require("./criteria");
5
+ const utils_1 = require("../utils");
6
+ async function existsById(config, connection, table, id) {
7
+ const res = await (0, utils_1.promiseQuery)(config, connection, `select 1 from ?? where ?? = ?`, [
8
+ table.tableName,
9
+ table.id,
10
+ id
11
+ ]);
12
+ const rows = res;
13
+ return !!rows.length;
14
+ }
15
+ exports.existsById = existsById;
16
+ async function existsBy(config, connection, table, criteria) {
17
+ let query = criteria ? (0, criteria_1.buildQuery)(criteria) : undefined;
18
+ let sql = `select 1 from ?? `;
19
+ if (query) {
20
+ sql += ` where ${query.sql} `;
21
+ }
22
+ const res = await (0, utils_1.promiseQuery)(config, connection, sql, [table.tableName].concat(query ? query.values : []));
23
+ const rows = res;
24
+ return !!rows.length;
25
+ }
26
+ exports.existsBy = existsBy;
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.findFirst = exports.findByIdIn = exports.find = exports.findAll = exports.findById = void 0;
4
+ const criteria_1 = require("./criteria");
5
+ const utils_1 = require("../utils");
6
+ /**
7
+ * 按 id 查询
8
+ * @param connection
9
+ * @param mapping
10
+ * @param type
11
+ * @param id
12
+ */
13
+ async function findById(config, connection, table, id) {
14
+ const res = await (0, utils_1.promiseQuery)(config, connection, `select * from ?? where ?? = ?`, [
15
+ table.tableName,
16
+ table.id,
17
+ id
18
+ ]);
19
+ const rows = res;
20
+ if (!rows.length) {
21
+ return null;
22
+ }
23
+ return rows[0];
24
+ }
25
+ exports.findById = findById;
26
+ /**
27
+ * 查询表中所有数据。需谨慎操作,全表查询有可能会产生大列表问题。
28
+ * @param connection
29
+ * @param mapping
30
+ * @param type
31
+ * @returns
32
+ */
33
+ async function findAll(config, connection, table) {
34
+ const res = await (0, utils_1.promiseQuery)(config, connection, `select * from ??`, [table.tableName]);
35
+ return res;
36
+ }
37
+ exports.findAll = findAll;
38
+ /**
39
+ * 条件查询
40
+ * @param opts
41
+ * @returns
42
+ */
43
+ async function find(config, conn, opts) {
44
+ let query = opts.criteria ? (0, criteria_1.buildQuery)(opts.criteria) : undefined;
45
+ const values = [];
46
+ let sql = `select * from ?? `;
47
+ values.push(opts.table.tableName);
48
+ if (query) {
49
+ sql += ` where ${query.sql} `;
50
+ values.push(...query.values);
51
+ }
52
+ // 排序
53
+ if (opts.orderBy && opts.orderBy.length) {
54
+ opts.orderBy.forEach((orderBy, idx) => {
55
+ const [field, sort] = orderBy;
56
+ if (idx == 0) {
57
+ sql += ` order by ?? ${sort} `;
58
+ }
59
+ else {
60
+ sql += ` , ?? ${sort} `;
61
+ }
62
+ values.push(field);
63
+ });
64
+ }
65
+ // 数量限制
66
+ if (opts.limit) {
67
+ sql += ` limit ${opts.limit} `;
68
+ if (opts.offset) {
69
+ sql += ` offset ${opts.offset}`;
70
+ }
71
+ }
72
+ const res = await (0, utils_1.promiseQuery)(config, conn, sql, values);
73
+ return res;
74
+ }
75
+ exports.find = find;
76
+ /**
77
+ * 根据 id 列表查询记录
78
+ * @param connection
79
+ * @param table
80
+ * @param ids
81
+ * @returns
82
+ */
83
+ async function findByIdIn(config, connection, table, ids) {
84
+ if (!ids.length) {
85
+ return [];
86
+ }
87
+ return find(config, connection, {
88
+ table,
89
+ criteria: c => c.in(table.id, ids)
90
+ });
91
+ }
92
+ exports.findByIdIn = findByIdIn;
93
+ /**
94
+ * 查找符合条件的第一条记录
95
+ * @param conn
96
+ * @param table
97
+ * @param criteria
98
+ * @returns
99
+ */
100
+ async function findFirst(config, conn, table, criteria) {
101
+ let query = criteria ? (0, criteria_1.buildQuery)(criteria) : undefined;
102
+ let sql = `select * from ?? `;
103
+ if (query) {
104
+ sql += ` where ${query.sql} `;
105
+ }
106
+ sql += ' limit 1';
107
+ const res = await (0, utils_1.promiseQuery)(config, conn, sql, [table.tableName].concat(query ? query.values : []));
108
+ const list = res;
109
+ return list.length ? list[0] : null;
110
+ }
111
+ exports.findFirst = findFirst;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ // 各类操作封装
5
+ tslib_1.__exportStar(require("./count"), exports);
6
+ tslib_1.__exportStar(require("./criteria"), exports);
7
+ tslib_1.__exportStar(require("./delete"), exports);
8
+ tslib_1.__exportStar(require("./exist"), exports);
9
+ tslib_1.__exportStar(require("./find"), exports);
10
+ tslib_1.__exportStar(require("./insert"), exports);
11
+ tslib_1.__exportStar(require("./modify"), exports);
12
+ tslib_1.__exportStar(require("./paginate"), exports);
13
+ tslib_1.__exportStar(require("./query"), exports);
14
+ tslib_1.__exportStar(require("./update"), exports);
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.insertMany = exports.insert = void 0;
4
+ const exception_1 = require("../../exception");
5
+ const utils_1 = require("../utils");
6
+ /**
7
+ * 为表插入数据
8
+ * @param connection
9
+ * @param table
10
+ * @param data
11
+ * @returns
12
+ */
13
+ async function insert(config, connection, table, data) {
14
+ // 插入后的新数据
15
+ // 列信息,使用 set 防止 columns 中重复配置 id 和更新创建时间列
16
+ let columnsSet = new Set();
17
+ // 判定下 id ,如果有值,才在 insert 语句中出现 id 列,否则不出现
18
+ if (data[table.id]) {
19
+ columnsSet.add(table.id);
20
+ }
21
+ table.columns.forEach(col => columnsSet.add(col));
22
+ if (table.createdDate) {
23
+ const createdData = table.createdDate.type === 'date' ? new Date() : new Date().getTime();
24
+ data[table.createdDate.column] = createdData;
25
+ columnsSet.add(table.createdDate.column);
26
+ }
27
+ if (table.updatedDate) {
28
+ const updatedDate = table.updatedDate.type === 'date' ? new Date() : new Date().getTime();
29
+ data[table.updatedDate.column] = updatedDate;
30
+ columnsSet.add(table.updatedDate.column);
31
+ }
32
+ const columns = Array.from(columnsSet);
33
+ // 构建 sql
34
+ const sql = `insert into ??(${columns.map(() => '??').join(',')}) values(${columns
35
+ .map(() => '?')
36
+ .join(',')})`;
37
+ const values = [table.tableName, ...columns, ...columns.map(col => data[col])];
38
+ const res = await (0, utils_1.promiseQuery)(config, connection, sql, values);
39
+ const packet = res;
40
+ if (packet.affectedRows !== 1) {
41
+ throw new exception_1.MysqlException(`Insert failed,table:${table.tableName},primary key: ${data[table.id]}`);
42
+ }
43
+ // 自动生成的id处理
44
+ if (packet.insertId && (data[table.id] === undefined || data[table.id] === null)) {
45
+ data[table.id] = packet.insertId;
46
+ }
47
+ return data;
48
+ }
49
+ exports.insert = insert;
50
+ /**
51
+ * 一次插入多条记录
52
+ * @param connection 连接
53
+ * @param table 表
54
+ * @param list 要插入的记录列表
55
+ */
56
+ async function insertMany(config, connection, table, list) {
57
+ if (!list.length) {
58
+ return;
59
+ }
60
+ // 列信息,使用 set 防止 columns 中重复配置 id 和更新创建时间列
61
+ let columnsSet = new Set();
62
+ // 批量插入必须统一使用 id 或不使用 id,以第一条记录为准
63
+ if (list[0][table.id]) {
64
+ columnsSet.add(table.id);
65
+ }
66
+ table.columns.forEach(col => columnsSet.add(col));
67
+ let createdData = undefined;
68
+ if (table.createdDate) {
69
+ createdData = table.createdDate.type === 'date' ? new Date() : new Date().getTime();
70
+ columnsSet.add(table.createdDate.column);
71
+ }
72
+ let updatedDate = undefined;
73
+ if (table.updatedDate) {
74
+ updatedDate = table.updatedDate.type === 'date' ? new Date() : new Date().getTime();
75
+ columnsSet.add(table.updatedDate.column);
76
+ }
77
+ const columns = Array.from(columnsSet);
78
+ // 构建 sql
79
+ let sql = `insert into ??(${columns.map(() => '??').join(',')}) values`;
80
+ const values = [table.tableName, ...columns];
81
+ list.forEach((data, idx) => {
82
+ if (idx > 0) {
83
+ sql += ',';
84
+ }
85
+ sql += `(${columns.map(() => '?').join(',')})`;
86
+ if (table.createdDate) {
87
+ data[table.createdDate.column] = createdData;
88
+ }
89
+ if (table.updatedDate) {
90
+ data[table.updatedDate.column] = updatedDate;
91
+ }
92
+ values.push(...columns.map(col => data[col]));
93
+ });
94
+ const res = await (0, utils_1.promiseQuery)(config, connection, sql, values);
95
+ const rsh = res;
96
+ if (rsh.affectedRows !== list.length) {
97
+ throw new exception_1.MysqlException(`Insert many for table ${table.tableName} failed,expected to insert ${list.length} records, actually ${rsh.affectedRows}.`);
98
+ }
99
+ // insert 插入多条记录是不会返回生成的 id 的,所以就没有办法自动处理
100
+ }
101
+ exports.insertMany = insertMany;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.modify = void 0;
4
+ const utils_1 = require("../utils");
5
+ async function modify(config, connection, sql, values) {
6
+ const res = await (0, utils_1.promiseQuery)(config, connection, sql, values || []);
7
+ const pck = res;
8
+ return pck.affectedRows;
9
+ }
10
+ exports.modify = modify;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.paginate = void 0;
4
+ const count_1 = require("./count");
5
+ const find_1 = require("./find");
6
+ async function paginate(config, conn, opts) {
7
+ const pn = opts.pn && opts.pn >= 1 ? opts.pn : 1;
8
+ const limit = opts.pz && opts.pz >= 1 && opts.pz <= 1000 ? opts.pz : 20;
9
+ const offset = (pn - 1) * limit;
10
+ const list = await (0, find_1.find)(config, conn, {
11
+ table: opts.table,
12
+ criteria: opts.criteria,
13
+ offset,
14
+ limit,
15
+ orderBy: opts.orderBy
16
+ });
17
+ const total = await (0, count_1.count)(config, conn, opts.table, opts.criteria);
18
+ return {
19
+ total,
20
+ list
21
+ };
22
+ }
23
+ exports.paginate = paginate;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.query = void 0;
4
+ const utils_1 = require("../utils");
5
+ async function query(config, connection, sql, values) {
6
+ const res = await (0, utils_1.promiseQuery)(config, connection, sql, values || []);
7
+ return res;
8
+ }
9
+ exports.query = query;
@@ -0,0 +1,201 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.updateMany = exports.updateOne = exports.partialUpdate = exports.update = void 0;
4
+ const exception_1 = require("../../exception");
5
+ const criteria_1 = require("./criteria");
6
+ const utils_1 = require("../utils");
7
+ /**
8
+ * 更新
9
+ * @param config
10
+ * @param connection
11
+ * @param mapping
12
+ * @param data
13
+ * @returns
14
+ */
15
+ async function update(config, connection, table, data) {
16
+ // 列信息
17
+ let columns = [...table.columns];
18
+ // 创建时间不更新
19
+ if (table.createdDate) {
20
+ const { column } = table.createdDate;
21
+ columns = columns.filter(col => col !== column);
22
+ }
23
+ if (table.updatedDate) {
24
+ const updatedDate = table.updatedDate.type === 'date' ? new Date() : new Date().getTime();
25
+ data[table.updatedDate.column] = updatedDate;
26
+ columns.push(table.updatedDate.column);
27
+ }
28
+ // 构建 sql
29
+ const sql = `update ?? set ${columns.map(() => ' ?? = ? ').join(',')} where ?? = ?`;
30
+ // 值
31
+ const values = [
32
+ table.tableName,
33
+ ...columns.flatMap(col => [col, data[col] || null]),
34
+ table.id,
35
+ data[table.id]
36
+ ];
37
+ const res = await (0, utils_1.promiseQuery)(config, connection, sql, values);
38
+ const packet = res;
39
+ if (packet.affectedRows !== 1) {
40
+ throw new exception_1.MysqlException(`Failed to update record, possibly due to non-existent record,table:${table.tableName},primary key: ${data[table.id]}`);
41
+ }
42
+ return data;
43
+ }
44
+ exports.update = update;
45
+ /**
46
+ * 转换更新器
47
+ * @param table
48
+ * @param updater
49
+ * @returns
50
+ */
51
+ function updatorToSql(table, updater) {
52
+ const values = [];
53
+ // 更新操作
54
+ const updateFragList = [];
55
+ // 更新时间
56
+ if (table.updatedDate) {
57
+ const updatedDate = table.updatedDate.type === 'date' ? new Date() : new Date().getTime();
58
+ updateFragList.push(' ?? = ?');
59
+ values.push(table.updatedDate.column, updatedDate);
60
+ }
61
+ for (const column in updater) {
62
+ // 过滤掉id
63
+ if (column === table.id) {
64
+ continue;
65
+ }
66
+ const val = updater[column];
67
+ if (Array.isArray(val)) {
68
+ // set null
69
+ if (val[0] === 'setNull') {
70
+ updateFragList.push(' ?? = NULL ');
71
+ values.push(column);
72
+ continue;
73
+ }
74
+ if (val[0] === 'inc') {
75
+ updateFragList.push(' ?? = ?? + ? ');
76
+ values.push(column, column, val[1]);
77
+ continue;
78
+ }
79
+ continue;
80
+ }
81
+ updateFragList.push(' ?? = ? ');
82
+ values.push(column, val);
83
+ }
84
+ return { sql: updateFragList.join(','), values };
85
+ }
86
+ /**
87
+ * 部分更新
88
+ * @param connection
89
+ * @param mapping
90
+ * @param type
91
+ * @param data
92
+ */
93
+ async function partialUpdate(config, connection, table, data) {
94
+ if (!data[table.id]) {
95
+ throw new exception_1.MysqlException(`Can't do a partial update, the data to be updated does not contain a primary key,table: ${table.tableName},column:${JSON.stringify(data)}`);
96
+ }
97
+ const id = data[table.id];
98
+ if (typeof id !== 'string' && typeof id !== 'number') {
99
+ throw new exception_1.MysqlException('Primary key can only be of string or number type');
100
+ }
101
+ if (Object.keys(data).length < 2) {
102
+ throw new exception_1.MysqlException(`Can't do a partial update, data must contain at least one column outside of the primary key,table: ${table.tableName},column:${JSON.stringify(data)}`);
103
+ }
104
+ const fieldNames = Object.keys(data);
105
+ for (const name of fieldNames) {
106
+ if (name !== table.id && !table.columns.some(col => col === name)) {
107
+ throw new exception_1.MysqlException(`Can't do a partial update,there are unconfigured columns in the data,table: ${table.tableName},unconfigured column:${name}`);
108
+ }
109
+ }
110
+ let sql = ` update ?? `;
111
+ const values = [table.tableName];
112
+ // 更新操作
113
+ const convertRes = updatorToSql(table, data);
114
+ if (!convertRes.sql) {
115
+ throw new exception_1.MysqlException('No fields were specified to be updated!');
116
+ }
117
+ values.push(...convertRes.values);
118
+ sql += ` set ${convertRes.sql} where ?? = ?`;
119
+ values.push(table.id, id);
120
+ const res = await (0, utils_1.promiseQuery)(config, connection, sql, values);
121
+ const packet = res;
122
+ return packet.affectedRows === 1;
123
+ }
124
+ exports.partialUpdate = partialUpdate;
125
+ /**
126
+ * 更新指定的一条记录
127
+ * @param config
128
+ * @param connection
129
+ * @param table
130
+ * @param query
131
+ * @param data
132
+ */
133
+ async function updateOne(config, connection, table, query, updater) {
134
+ const values = [];
135
+ const mysqlQuery = (0, criteria_1.buildQuery)(query);
136
+ if (!mysqlQuery) {
137
+ throw new exception_1.MysqlException('No valid criteria specified.');
138
+ }
139
+ let sql = ` update ?? `;
140
+ values.push(table.tableName);
141
+ // 更新操作
142
+ const convertRes = updatorToSql(table, updater);
143
+ if (!convertRes.sql) {
144
+ throw new exception_1.MysqlException('No fields were specified to be updated!');
145
+ }
146
+ sql += ` set ${convertRes.sql} `;
147
+ values.push(...convertRes.values);
148
+ sql += ` where ${mysqlQuery.sql} limit 1`;
149
+ values.push(...mysqlQuery.values);
150
+ const res = await (0, utils_1.promiseQuery)(config, connection, sql, values);
151
+ const packet = res;
152
+ return packet.affectedRows === 1;
153
+ }
154
+ exports.updateOne = updateOne;
155
+ /**
156
+ * 更新所有匹配条件的记录
157
+ * @param config
158
+ * @param connection 连接
159
+ * @param table 表
160
+ * @param query 查询条件
161
+ * @param updater 更新操作,支持置空和递增(需要使用元组)
162
+ */
163
+ async function updateMany(config, connection, opts) {
164
+ const values = [];
165
+ const mysqlQuery = (0, criteria_1.buildQuery)(opts.query);
166
+ if (!mysqlQuery) {
167
+ throw new exception_1.MysqlException('No valid criteria specified.');
168
+ }
169
+ let sql = ` update ?? `;
170
+ values.push(opts.table.tableName);
171
+ // 更新操作
172
+ const convertRes = updatorToSql(opts.table, opts.updater);
173
+ if (!convertRes.sql) {
174
+ throw new exception_1.MysqlException('No fields were specified to be updated!');
175
+ }
176
+ sql += ` set ${convertRes.sql} `;
177
+ values.push(...convertRes.values);
178
+ sql += ` where ${mysqlQuery.sql} `;
179
+ values.push(...mysqlQuery.values);
180
+ // 排序
181
+ if (opts.orderBy && opts.orderBy.length) {
182
+ opts.orderBy.forEach((orderBy, idx) => {
183
+ const [field, sort] = orderBy;
184
+ if (idx == 0) {
185
+ sql += ` order by ?? ${sort} `;
186
+ }
187
+ else {
188
+ sql += ` , ?? ${sort} `;
189
+ }
190
+ values.push(field);
191
+ });
192
+ }
193
+ // 数量限制
194
+ if (opts.limit) {
195
+ sql += ` limit ${opts.limit} `;
196
+ }
197
+ const res = await (0, utils_1.promiseQuery)(config, connection, sql, values);
198
+ const packet = res;
199
+ return packet.affectedRows;
200
+ }
201
+ exports.updateMany = updateMany;
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MysqlStrictTxSession = void 0;
4
+ const tx_1 = require("./tx");
5
+ const exception_1 = require("../exception");
6
+ /**
7
+ * 严格 mysql 事务会话,会禁用一些操作.
8
+ */
9
+ class MysqlStrictTxSession extends tx_1.MysqlTxSession {
10
+ #opsCount = 0;
11
+ constructor(config, conn) {
12
+ super(config, conn);
13
+ }
14
+ /**
15
+ * 为操作计数,检查是否操作次数过多.
16
+ */
17
+ #checkAndAddOpsCount() {
18
+ if (this.#opsCount >= 10) {
19
+ throw new exception_1.MysqlException('Too many operations in a strict transaction.');
20
+ }
21
+ this.#opsCount++;
22
+ }
23
+ findById(table, id) {
24
+ this.#checkAndAddOpsCount();
25
+ return super.findById(table, id);
26
+ }
27
+ findByIdIn(table, ids) {
28
+ if (ids.length > 100) {
29
+ throw new exception_1.MysqlException(`The augument ids length(${ids.length}) passed to findByIdIn in a strict transaction is too large .`);
30
+ }
31
+ this.#checkAndAddOpsCount();
32
+ return super.findByIdIn(table, ids);
33
+ }
34
+ existsBy(table, criteria) {
35
+ this.#checkAndAddOpsCount();
36
+ return super.existsBy(table, criteria);
37
+ }
38
+ existsById(table, id) {
39
+ this.#checkAndAddOpsCount();
40
+ return super.existsById(table, id);
41
+ }
42
+ deleteById(table, id) {
43
+ this.#checkAndAddOpsCount();
44
+ return super.deleteById(table, id);
45
+ }
46
+ deleteOne(table, criteria) {
47
+ this.#checkAndAddOpsCount();
48
+ return super.deleteOne(table, criteria);
49
+ }
50
+ deleteMany(opts) {
51
+ throw new exception_1.MysqlException('Prohibited to use deleteBy in a strict transaction.');
52
+ }
53
+ findAll(table) {
54
+ throw new exception_1.MysqlException('Prohibited to use findAll in a strict transaction.');
55
+ }
56
+ findFirst(table, criteria) {
57
+ this.#checkAndAddOpsCount();
58
+ return super.findFirst(table, criteria);
59
+ }
60
+ insert(table, data) {
61
+ this.#checkAndAddOpsCount();
62
+ return super.insert(table, data);
63
+ }
64
+ insertMany(table, list) {
65
+ throw new exception_1.MysqlException('Prohibited to use insertMany in a strict transaction.');
66
+ }
67
+ update(table, data) {
68
+ this.#checkAndAddOpsCount();
69
+ return super.update(table, data);
70
+ }
71
+ updateMany(opts) {
72
+ throw new exception_1.MysqlException('Prohibited to use updateMany in a strict transaction.');
73
+ }
74
+ updateOne(table, query, updater) {
75
+ this.#checkAndAddOpsCount();
76
+ return super.updateOne(table, query, updater);
77
+ }
78
+ partialUpdate(table, data) {
79
+ this.#checkAndAddOpsCount();
80
+ return super.partialUpdate(table, data);
81
+ }
82
+ find(opts) {
83
+ throw new exception_1.MysqlException('Prohibited to use find in a strict transaction.');
84
+ }
85
+ count(table, criteria) {
86
+ throw new exception_1.MysqlException('Prohibited to use count in a strict transaction.');
87
+ }
88
+ paginate(opts) {
89
+ throw new exception_1.MysqlException('Prohibited to use paginate in a strict transaction.');
90
+ }
91
+ query(sql, values) {
92
+ throw new exception_1.MysqlException('Prohibited to use query in a strict transaction.');
93
+ }
94
+ modify(sql, values) {
95
+ throw new exception_1.MysqlException('Prohibited to use modify in a strict transaction.');
96
+ }
97
+ }
98
+ exports.MysqlStrictTxSession = MysqlStrictTxSession;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MysqlTxSession = void 0;
4
+ const base_1 = require("./base");
5
+ const exception_1 = require("../exception");
6
+ /**
7
+ * mysql 事务会话.
8
+ */
9
+ class MysqlTxSession extends base_1.BaseMysqlManager {
10
+ /**
11
+ * 中止标识
12
+ */
13
+ #aborted = false;
14
+ constructor(config, conn) {
15
+ super({ config, connection: conn });
16
+ }
17
+ queryWithConnection(queryFn) {
18
+ if (this.#aborted) {
19
+ throw new exception_1.MysqlException('Session has been aborted!');
20
+ }
21
+ return super.queryWithConnection(queryFn);
22
+ }
23
+ /**
24
+ * 中止,被中止后的会话不能再进行任何操作
25
+ */
26
+ abort() {
27
+ this.#aborted = true;
28
+ }
29
+ }
30
+ exports.MysqlTxSession = MysqlTxSession;