mm_dao 1.9.0 → 1.9.2

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 (197) hide show
  1. package/README.md +11 -2
  2. package/index.js +3 -13
  3. package/lib/admin.js +24 -0
  4. package/lib/dao.js +38 -1219
  5. package/lib/dao_crud.js +379 -0
  6. package/lib/dao_fields.js +145 -0
  7. package/lib/dao_query.js +238 -0
  8. package/lib/dao_scope.js +203 -0
  9. package/lib/dao_stats.js +129 -0
  10. package/lib/dao_transfer.js +358 -0
  11. package/lib/manager.js +40 -18
  12. package/lib/script.tpl.js +4 -4
  13. package/package.json +70 -63
  14. package/tool/dao/index.js +176 -0
  15. package/tool/dao/tool.json +121 -0
  16. package/tool/dao_admin/index.js +36 -0
  17. package/tool/dao_admin/tool.json +11 -0
  18. package/db/mm/user_account/_meta.json +0 -54
  19. package/db/mm.db +0 -0
  20. package/eslint.config.js +0 -235
  21. package/example/index.js +0 -78
  22. package/test_db/dao_test_1785991977965/account/1.json +0 -6
  23. package/test_db/dao_test_1785991977965/account/2.json +0 -6
  24. package/test_db/dao_test_1785991977965/account/_meta.json +0 -18
  25. package/test_db/dao_test_1785991977965/role/1.json +0 -7
  26. package/test_db/dao_test_1785991977965/role/2.json +0 -7
  27. package/test_db/dao_test_1785991977965/role/3.json +0 -7
  28. package/test_db/dao_test_1785991977965/role/_meta.json +0 -18
  29. package/test_db/dao_test_1785991977965/tool/_meta.json +0 -18
  30. package/test_db/dao_test_1785991977965/tool/bow_001.json +0 -8
  31. package/test_db/dao_test_1785991977965/tool/robe_001.json +0 -8
  32. package/test_db/dao_test_1785991977965/tool/shield_001.json +0 -8
  33. package/test_db/dao_test_1785991977965/tool/staff_001.json +0 -8
  34. package/test_db/dao_test_1785991977965/tool/sword_001.json +0 -8
  35. package/test_db/dao_test_1785993793800/account/1.json +0 -58
  36. package/test_db/dao_test_1785993793800/account/2.json +0 -25
  37. package/test_db/dao_test_1785993793800/account/_meta.json +0 -18
  38. package/test_db/dao_test_1785993793800/role/1.json +0 -7
  39. package/test_db/dao_test_1785993793800/role/2.json +0 -7
  40. package/test_db/dao_test_1785993793800/role/3.json +0 -7
  41. package/test_db/dao_test_1785993793800/role/_meta.json +0 -18
  42. package/test_db/dao_test_1785993793800/tool/_meta.json +0 -18
  43. package/test_db/dao_test_1785993793800/tool/bow_001.json +0 -8
  44. package/test_db/dao_test_1785993793800/tool/robe_001.json +0 -8
  45. package/test_db/dao_test_1785993793800/tool/shield_001.json +0 -8
  46. package/test_db/dao_test_1785993793800/tool/staff_001.json +0 -8
  47. package/test_db/dao_test_1785993793800/tool/sword_001.json +0 -8
  48. package/test_db/dao_test_1785993841615/account/1.json +0 -58
  49. package/test_db/dao_test_1785993841615/account/2.json +0 -25
  50. package/test_db/dao_test_1785993841615/account/_meta.json +0 -18
  51. package/test_db/dao_test_1785993841615/role/1.json +0 -7
  52. package/test_db/dao_test_1785993841615/role/2.json +0 -7
  53. package/test_db/dao_test_1785993841615/role/3.json +0 -7
  54. package/test_db/dao_test_1785993841615/role/_meta.json +0 -18
  55. package/test_db/dao_test_1785993841615/tool/_meta.json +0 -18
  56. package/test_db/dao_test_1785993841615/tool/bow_001.json +0 -8
  57. package/test_db/dao_test_1785993841615/tool/robe_001.json +0 -8
  58. package/test_db/dao_test_1785993841615/tool/shield_001.json +0 -8
  59. package/test_db/dao_test_1785993841615/tool/staff_001.json +0 -8
  60. package/test_db/dao_test_1785993841615/tool/sword_001.json +0 -8
  61. package/test_db/dao_test_1785998204124/account/1.json +0 -58
  62. package/test_db/dao_test_1785998204124/account/2.json +0 -25
  63. package/test_db/dao_test_1785998204124/account/_meta.json +0 -18
  64. package/test_db/dao_test_1785998204124/role/1.json +0 -7
  65. package/test_db/dao_test_1785998204124/role/2.json +0 -7
  66. package/test_db/dao_test_1785998204124/role/3.json +0 -7
  67. package/test_db/dao_test_1785998204124/role/_meta.json +0 -18
  68. package/test_db/dao_test_1785998204124/tool/_meta.json +0 -18
  69. package/test_db/dao_test_1785998204124/tool/bow_001.json +0 -8
  70. package/test_db/dao_test_1785998204124/tool/robe_001.json +0 -8
  71. package/test_db/dao_test_1785998204124/tool/shield_001.json +0 -8
  72. package/test_db/dao_test_1785998204124/tool/staff_001.json +0 -8
  73. package/test_db/dao_test_1785998204124/tool/sword_001.json +0 -8
  74. package/test_db/dao_test_1785998207460/account/1.json +0 -58
  75. package/test_db/dao_test_1785998207460/account/2.json +0 -25
  76. package/test_db/dao_test_1785998207460/account/_meta.json +0 -18
  77. package/test_db/dao_test_1785998207460/role/1.json +0 -7
  78. package/test_db/dao_test_1785998207460/role/2.json +0 -7
  79. package/test_db/dao_test_1785998207460/role/3.json +0 -7
  80. package/test_db/dao_test_1785998207460/role/_meta.json +0 -18
  81. package/test_db/dao_test_1785998207460/tool/_meta.json +0 -18
  82. package/test_db/dao_test_1785998207460/tool/bow_001.json +0 -8
  83. package/test_db/dao_test_1785998207460/tool/robe_001.json +0 -8
  84. package/test_db/dao_test_1785998207460/tool/shield_001.json +0 -8
  85. package/test_db/dao_test_1785998207460/tool/staff_001.json +0 -8
  86. package/test_db/dao_test_1785998207460/tool/sword_001.json +0 -8
  87. package/test_db/dao_test_1785998210638/account/1.json +0 -58
  88. package/test_db/dao_test_1785998210638/account/2.json +0 -25
  89. package/test_db/dao_test_1785998210638/account/_meta.json +0 -18
  90. package/test_db/dao_test_1785998210638/role/1.json +0 -7
  91. package/test_db/dao_test_1785998210638/role/2.json +0 -7
  92. package/test_db/dao_test_1785998210638/role/3.json +0 -7
  93. package/test_db/dao_test_1785998210638/role/_meta.json +0 -18
  94. package/test_db/dao_test_1785998210638/tool/_meta.json +0 -18
  95. package/test_db/dao_test_1785998210638/tool/bow_001.json +0 -8
  96. package/test_db/dao_test_1785998210638/tool/robe_001.json +0 -8
  97. package/test_db/dao_test_1785998210638/tool/shield_001.json +0 -8
  98. package/test_db/dao_test_1785998210638/tool/staff_001.json +0 -8
  99. package/test_db/dao_test_1785998210638/tool/sword_001.json +0 -8
  100. package/test_db/dao_test_1785998213852/account/1.json +0 -58
  101. package/test_db/dao_test_1785998213852/account/2.json +0 -25
  102. package/test_db/dao_test_1785998213852/account/_meta.json +0 -18
  103. package/test_db/dao_test_1785998213852/role/1.json +0 -7
  104. package/test_db/dao_test_1785998213852/role/2.json +0 -7
  105. package/test_db/dao_test_1785998213852/role/3.json +0 -7
  106. package/test_db/dao_test_1785998213852/role/_meta.json +0 -18
  107. package/test_db/dao_test_1785998213852/tool/_meta.json +0 -18
  108. package/test_db/dao_test_1785998213852/tool/bow_001.json +0 -8
  109. package/test_db/dao_test_1785998213852/tool/robe_001.json +0 -8
  110. package/test_db/dao_test_1785998213852/tool/shield_001.json +0 -8
  111. package/test_db/dao_test_1785998213852/tool/staff_001.json +0 -8
  112. package/test_db/dao_test_1785998213852/tool/sword_001.json +0 -8
  113. package/test_db/dao_test_1785998217094/account/1.json +0 -58
  114. package/test_db/dao_test_1785998217094/account/2.json +0 -25
  115. package/test_db/dao_test_1785998217094/account/_meta.json +0 -18
  116. package/test_db/dao_test_1785998217094/role/1.json +0 -7
  117. package/test_db/dao_test_1785998217094/role/2.json +0 -7
  118. package/test_db/dao_test_1785998217094/role/3.json +0 -7
  119. package/test_db/dao_test_1785998217094/role/_meta.json +0 -18
  120. package/test_db/dao_test_1785998217094/tool/_meta.json +0 -18
  121. package/test_db/dao_test_1785998217094/tool/bow_001.json +0 -8
  122. package/test_db/dao_test_1785998217094/tool/robe_001.json +0 -8
  123. package/test_db/dao_test_1785998217094/tool/shield_001.json +0 -8
  124. package/test_db/dao_test_1785998217094/tool/staff_001.json +0 -8
  125. package/test_db/dao_test_1785998217094/tool/sword_001.json +0 -8
  126. package/test_db/dao_test_1786000291805/account/1.json +0 -58
  127. package/test_db/dao_test_1786000291805/account/2.json +0 -25
  128. package/test_db/dao_test_1786000291805/account/_meta.json +0 -18
  129. package/test_db/dao_test_1786000291805/role/1.json +0 -7
  130. package/test_db/dao_test_1786000291805/role/2.json +0 -7
  131. package/test_db/dao_test_1786000291805/role/3.json +0 -7
  132. package/test_db/dao_test_1786000291805/role/_meta.json +0 -18
  133. package/test_db/dao_test_1786000291805/tool/_meta.json +0 -18
  134. package/test_db/dao_test_1786000291805/tool/bow_001.json +0 -8
  135. package/test_db/dao_test_1786000291805/tool/robe_001.json +0 -8
  136. package/test_db/dao_test_1786000291805/tool/shield_001.json +0 -8
  137. package/test_db/dao_test_1786000291805/tool/staff_001.json +0 -8
  138. package/test_db/dao_test_1786000291805/tool/sword_001.json +0 -8
  139. package/test_db/dao_test_1786001183999/account/1.json +0 -58
  140. package/test_db/dao_test_1786001183999/account/2.json +0 -25
  141. package/test_db/dao_test_1786001183999/account/_meta.json +0 -18
  142. package/test_db/dao_test_1786001183999/role/1.json +0 -7
  143. package/test_db/dao_test_1786001183999/role/2.json +0 -7
  144. package/test_db/dao_test_1786001183999/role/3.json +0 -7
  145. package/test_db/dao_test_1786001183999/role/_meta.json +0 -18
  146. package/test_db/dao_test_1786001183999/tool/_meta.json +0 -18
  147. package/test_db/dao_test_1786001183999/tool/bow_001.json +0 -8
  148. package/test_db/dao_test_1786001183999/tool/robe_001.json +0 -8
  149. package/test_db/dao_test_1786001183999/tool/shield_001.json +0 -8
  150. package/test_db/dao_test_1786001183999/tool/staff_001.json +0 -8
  151. package/test_db/dao_test_1786001183999/tool/sword_001.json +0 -8
  152. package/test_db/dao_test_1786001297521/account/1.json +0 -58
  153. package/test_db/dao_test_1786001297521/account/2.json +0 -25
  154. package/test_db/dao_test_1786001297521/account/_meta.json +0 -18
  155. package/test_db/dao_test_1786001297521/role/1.json +0 -7
  156. package/test_db/dao_test_1786001297521/role/2.json +0 -7
  157. package/test_db/dao_test_1786001297521/role/3.json +0 -7
  158. package/test_db/dao_test_1786001297521/role/_meta.json +0 -18
  159. package/test_db/dao_test_1786001297521/tool/_meta.json +0 -18
  160. package/test_db/dao_test_1786001297521/tool/bow_001.json +0 -8
  161. package/test_db/dao_test_1786001297521/tool/robe_001.json +0 -8
  162. package/test_db/dao_test_1786001297521/tool/shield_001.json +0 -8
  163. package/test_db/dao_test_1786001297521/tool/staff_001.json +0 -8
  164. package/test_db/dao_test_1786001297521/tool/sword_001.json +0 -8
  165. package/test_db/debug_dao_1785992422444/account/1.json +0 -24
  166. package/test_db/debug_dao_1785992422444/account/_meta.json +0 -18
  167. package/test_db/debug_dao_1785992422444/role/1.json +0 -7
  168. package/test_db/debug_dao_1785992422444/role/2.json +0 -7
  169. package/test_db/debug_dao_1785992422444/role/_meta.json +0 -18
  170. package/test_db/debug_dao_1785992422444/tool/_meta.json +0 -18
  171. package/test_db/debug_dao_1785992422444/tool/shield_001.json +0 -7
  172. package/test_db/debug_dao_1785992422444/tool/staff_001.json +0 -7
  173. package/test_db/debug_dao_1785992422444/tool/sword_001.json +0 -7
  174. package/test_db/debug_dao_1785993785567/account/1.json +0 -47
  175. package/test_db/debug_dao_1785993785567/account/_meta.json +0 -18
  176. package/test_db/debug_dao_1785993785567/role/1.json +0 -7
  177. package/test_db/debug_dao_1785993785567/role/2.json +0 -7
  178. package/test_db/debug_dao_1785993785567/role/_meta.json +0 -18
  179. package/test_db/debug_dao_1785993785567/tool/_meta.json +0 -18
  180. package/test_db/debug_dao_1785993785567/tool/shield_001.json +0 -7
  181. package/test_db/debug_dao_1785993785567/tool/staff_001.json +0 -7
  182. package/test_db/debug_dao_1785993785567/tool/sword_001.json +0 -7
  183. package/test_db/debug_dao_check/tool/_meta.json +0 -18
  184. package/test_db/debug_dao_check/tool/sword_001.json +0 -6
  185. package/test_db/test_1785973671479/users/1.json +0 -5
  186. package/test_db/test_1785973671479/users/_meta.json +0 -20
  187. package/tests/dao_test.js +0 -213
  188. package/tests/debug_error_test.js +0 -165
  189. package/tests/debug_test.js +0 -35
  190. package/tests/debug_test2.js +0 -45
  191. package/tests/debug_test3.js +0 -54
  192. package/tests/detailed_error_test.js +0 -171
  193. package/tests/main.test.js +0 -261
  194. package/tests/query.test.js +0 -142
  195. package/tests/scope.test.js +0 -72
  196. package/tests/test.js +0 -325
  197. package/tests/test2.js +0 -64
@@ -0,0 +1,358 @@
1
+ const { string } = require('mm_helper');
2
+ const { getRet } = require('mm_ret');
3
+ const { fs: file } = require('mm_files');
4
+
5
+ /**
6
+ * 导入、导出与去重
7
+ * 本文件为 Dao 原型扩展的职责分片:Excel/JSON/XML 数据导入、结果导出与重复数据清理。
8
+ * 函数最终挂载到 Dao.prototype,内部通过 this 访问 Dao 实例。
9
+ */
10
+
11
+ /**
12
+ * 导入数据(主要)
13
+ * @param {object} db 数据库操作类
14
+ * @param {string} filename 文件名
15
+ * @returns {Promise<object>} 返回导入结果
16
+ */
17
+ async function importMain(db, filename) {
18
+ let params = await this.getParam(db);
19
+ let format = await this.getFormat(db);
20
+ let fpath = this._processFilePath(filename);
21
+ if (!file.hasFile(fpath)) {
22
+ return getRet().error(30001, fpath + '文件不存在!');
23
+ }
24
+ let jarr = await this._loadImport(fpath, params, format);
25
+ if (!jarr || !jarr.length) {
26
+ return getRet().error(10000, '要导入的数据不能为空!');
27
+ }
28
+ let result = await this._importToDb(db, jarr);
29
+ return this._buildImport(result);
30
+ }
31
+
32
+ /**
33
+ * 处理文件路径
34
+ * @param {string} filename 文件名
35
+ * @returns {string} 处理后的文件路径
36
+ * @private
37
+ */
38
+ function _processFilePath(filename) {
39
+ // 局部变量不能叫 file,否则会遮蔽 mm_files 的 file 助手(hasFile/fullname 会变 undefined)
40
+ let fpath = filename || '';
41
+ let run_path = this._getInject('run_path') || '';
42
+ // 未配置 run_path 时 indexOf('') 恒为 0,不能据此判断“已在运行目录下”
43
+ if (!run_path || fpath.indexOf(run_path) !== 0) {
44
+ // save_dir 是相对运行目录的,基准也必须是运行目录(原来用 _getPath 会拼出 static/static/file)
45
+ let base_path = run_path || process.cwd();
46
+ fpath = file.fullname(fpath.replace(this.url_path, this.save_dir), base_path);
47
+ }
48
+ return fpath;
49
+ }
50
+
51
+ /**
52
+ * 加载导入数据
53
+ * @param {string} fpath 文件路径
54
+ * @param {Array} params 参数列表
55
+ * @param {Array} format 格式列表
56
+ * @returns {Promise<Array>} 数据数组
57
+ * @throws {Error} Excel 文件解析失败时抛出
58
+ * @private
59
+ */
60
+ async function _loadImport(fpath, params, format) {
61
+ if (fpath.endsWith('.json')) {
62
+ return file.loadJson(fpath);
63
+ }
64
+ if (fpath.endsWith('.xml')) {
65
+ return file.loadXml(fpath);
66
+ }
67
+ const Excel = require('mm_excel');
68
+ let excel = new Excel({ file: fpath, params, format });
69
+ try {
70
+ return await excel.load();
71
+ } finally {
72
+ excel.clear();
73
+ }
74
+ }
75
+
76
+ /**
77
+ * 导入数据到数据库
78
+ * @param {object} db 数据库操作类
79
+ * @param {Array} jarr 数据数组
80
+ * @returns {Promise<object>} 导入结果
81
+ * @private
82
+ */
83
+ async function _importToDb(db, jarr) {
84
+ let list = [];
85
+ let errors = [];
86
+ let list_error = [];
87
+ db.table = db.table || this.config.table;
88
+ let key = this.config.key;
89
+ if (jarr[0][key]) {
90
+ for (let i = 0; i < jarr.length; i++) {
91
+ let o = jarr[i];
92
+ let qy = {};
93
+ qy[key] = o[key];
94
+ let n = await db.addOrSet(qy, o);
95
+ if (n < 1) {
96
+ errors.push(db.error);
97
+ list_error.push(o);
98
+ } else {
99
+ list.push(o);
100
+ }
101
+ }
102
+ } else {
103
+ for (let i = 0; i < jarr.length; i++) {
104
+ let o = jarr[i];
105
+ let n = await db.add(o);
106
+ if (n < 1) {
107
+ errors.push(db.error);
108
+ list_error.push(o);
109
+ } else {
110
+ list.push(o);
111
+ }
112
+ }
113
+ }
114
+ return { list, errors, list_error, total: jarr.length };
115
+ }
116
+
117
+ /**
118
+ * 构建导入结果
119
+ * @param {object} result 导入结果数据
120
+ * @returns {object} 返回结果对象
121
+ * @private
122
+ */
123
+ function _buildImport(result) {
124
+ let bl = result.list.length === result.total;
125
+ let body = getRet().bl(bl, bl ? '导入成功!' : '导入失败!');
126
+ body.result.list = result.list;
127
+ if (result.errors.length) {
128
+ body.result.list_error = result.list_error;
129
+ body.result.errors = result.errors;
130
+ }
131
+ return body;
132
+ }
133
+
134
+ /**
135
+ * 导入数据
136
+ * @param {object} db 数据库操作类
137
+ * @param {object} query 查询条件
138
+ * @param {object} body 导入设置
139
+ * @returns {Promise<object>} 返回执行结果
140
+ */
141
+ async function importData(db, query, body) {
142
+ let params = { ...query, ...body };
143
+ if (!params.file) {
144
+ return getRet().error(60000, '文件名(file)参数不能为空');
145
+ }
146
+ return this.importMain(db, params.file);
147
+ }
148
+
149
+ /**
150
+ * 导出数据(主要)
151
+ * @param {object} db 数据库操作类
152
+ * @param {object} query 查询参数
153
+ * @param {object} body 正文参数(导出设置)
154
+ * @property {string} body.fields 需要导出的字段 例如: `username`,`gm`,`vip`
155
+ * @property {string} body.file 文件名 例如: 用户名.xlsx 、用户信息.csv 、用户账户.xls
156
+ * @property {string} body.path 文件路径 例如: /static/download, 可不填写
157
+ * @returns {Promise<object>} 返回执行结果
158
+ */
159
+ async function exportMain(db, query, body) {
160
+ let by = await this.getMain(db, query);
161
+ if (by.error) {
162
+ return by;
163
+ }
164
+ let err = this._errMsg(db);
165
+ if (err) {
166
+ return getRet().error(10000, err);
167
+ }
168
+ let config = this._prepareExportConfig(query, body);
169
+ let params = await this.getParam(db, config.fields);
170
+ let format = await this.getFormat(db);
171
+ // 统计模式下 getMain 返回的是条数而非列表,此处兜底避免取 list 报错
172
+ let list = (by.result && by.result.list) || [];
173
+ let fpath = await this._saveExportFile(config.file, params, format, list);
174
+ return this._buildExport(fpath, config.name);
175
+ }
176
+
177
+ /**
178
+ * 准备导出配置
179
+ * @param {object} query 查询参数
180
+ * @param {object} body 正文参数
181
+ * @returns {object} 导出配置
182
+ * @private
183
+ */
184
+ function _prepareExportConfig(query, body) {
185
+ let path = body.path || this._getPath();
186
+ let file_name = body.file;
187
+ let fields = body.fields;
188
+ let table = this._getDict().table || this.config.table;
189
+ if (!file_name) {
190
+ file_name = this.save_dir + table + '_' + new Date().stamp() + '.xlsx';
191
+ }
192
+ if (!fields) {
193
+ fields =
194
+ query[this._getFilter(this.config).field] ||
195
+ (this.config.field_default !== '*' ? this.config.field_default : '');
196
+ }
197
+ return { path, file: file_name, fields, name: file_name.split('/').pop() };
198
+ }
199
+
200
+ /**
201
+ * 获取路径
202
+ * @returns {string} 导出路径
203
+ * @private
204
+ */
205
+ function _getPath() {
206
+ return (
207
+ this._getInject('user_path') ||
208
+ this._getInject('static_path') ||
209
+ './static/'
210
+ );
211
+ }
212
+
213
+ /**
214
+ * 保存导出文件
215
+ * @param {string} fpath 文件路径
216
+ * @param {Array} params 参数列表
217
+ * @param {Array} format 格式列表
218
+ * @param {Array} list 数据列表
219
+ * @returns {Promise<string>} 保存后的文件路径
220
+ * @throws {Error} 文件保存失败时抛出
221
+ * @private
222
+ */
223
+ async function _saveExportFile(fpath, params, format, list) {
224
+ let save_file = file.fullname(fpath, this._getPath());
225
+ file.addDir(save_file);
226
+ const Excel = require('mm_excel');
227
+ let excel = new Excel({ file: save_file, params, format });
228
+ try {
229
+ return await excel.save(list);
230
+ } finally {
231
+ excel.clear();
232
+ }
233
+ }
234
+
235
+ /**
236
+ * 构建导出结果
237
+ * @param {string} fpath 文件路径
238
+ * @param {string} name 文件名
239
+ * @returns {object} 导出结果
240
+ * @private
241
+ */
242
+ function _buildExport(fpath, name) {
243
+ let body = getRet().bl(!!fpath, fpath ? '导出成功!' : '导出失败!');
244
+ body.result.file = fpath;
245
+ body.result.url = this.url_path + name;
246
+ return body;
247
+ }
248
+
249
+ /**
250
+ * 导出数据
251
+ * @param {object} db 数据库操作类
252
+ * @param {object} query 查询条件
253
+ * @param {object} body 修改项
254
+ * @returns {Promise<object>} 返回执行结果
255
+ */
256
+ async function exportData(db, query, body) {
257
+ return this.exportMain(db, query, body);
258
+ }
259
+
260
+ /**
261
+ * 删除重复项(主要)
262
+ * @param {object} db 数据库管理器
263
+ * @param {object} params 查询参数
264
+ * @returns {Promise<object>} 返回执行结果
265
+ */
266
+ async function delRepeatMain(db, params) {
267
+ let pm = { ...params };
268
+ let msg = '';
269
+ let cg = this.config.del_repeat;
270
+ let f = this._getFilter(this.config);
271
+ let order_by = pm[f.orderby] || cg.orderby;
272
+ let group_by_val = pm[f.groupby] || cg.groupby;
273
+ let group_by =
274
+ (typeof group_by_val === 'string' && string.validate(group_by_val, 'username') ? group_by_val : '') ||
275
+ cg.groupby ||
276
+ '';
277
+ let field_val = pm[f.field];
278
+ let field =
279
+ (typeof field_val === 'string' && string.validate(field_val, 'username') ? field_val : '') || group_by;
280
+ // 去重过程会改动分页与字段设置(扫全表 -> 逐条取单条),先记下原值,结束后还原
281
+ let old_page = db.page;
282
+ let old_size = db.size;
283
+ let old_field = db.field;
284
+ // 抽走 page、size、table、method、org_id 等控制参数,只留真正的查询条件,
285
+ // 否则会被拼成 `page` = '1' 之类的条件,导致去重查不到数据
286
+ let qy = this.ready(db, pm).qy;
287
+ // 分组与取行都要带 query_default(用户/机构隔离),否则会跨用户分组并删掉别人的记录
288
+ let where = this._buildQueryString(db, qy, this.config);
289
+ let list = [];
290
+ try {
291
+ // 去重要扫全表,不能带分页(LIMIT 拼到 GROUP BY 前面会直接语法错误)
292
+ db.page = 0;
293
+ db.size = 0;
294
+ db.field = field;
295
+ // 用 toQuery 而不是 toGetSql:view 固定为 *,替换分组字段时不受适配器默认字段影响
296
+ let sql = db
297
+ .toQuery(where, '', '*')
298
+ .replace(' * ', ' `' + field + '`, count(*) as len ');
299
+ sql += ` GROUP BY ${group_by}`;
300
+ sql = 'SELECT * FROM (' + sql + ') a WHERE len > 1';
301
+ list = (await db.run(sql)) || [];
302
+ if (list.length) {
303
+ db.page = 1;
304
+ db.size = 1;
305
+ let key = this.config.key;
306
+ for (let i = 0; i < list.length; i++) {
307
+ let o = list[i];
308
+ let len = o.len - 1;
309
+ delete o.len;
310
+ // 取行同样叠加隔离条件:只按分组字段取第一条会取到他人的记录
311
+ let owhere = this._buildQueryString(db, o, this.config);
312
+ for (let n = 0; n < len; n++) {
313
+ let rows = (await db.getSql(owhere, order_by, key)) || [];
314
+ if (!rows.length) {
315
+ break;
316
+ }
317
+ await db.del(rows[0]);
318
+ }
319
+ }
320
+ } else {
321
+ msg = '没有重复项。';
322
+ }
323
+ } finally {
324
+ db.page = old_page;
325
+ db.size = old_size;
326
+ db.field = old_field;
327
+ }
328
+ return getRet().bl(!msg, msg ? '去重失败!原因:' + msg : '去重成功!');
329
+ }
330
+
331
+ /**
332
+ * 删除重复项
333
+ * @param {object} db 数据库管理器
334
+ * @param {object} query 查询条件
335
+ * @param {object} body 正文参数
336
+ * @returns {Promise<object>} 返回执行结果
337
+ */
338
+ async function delRepeat(db, query, body) {
339
+ let pm = { ...query, ...body };
340
+ return this.delRepeatMain(db, pm);
341
+ }
342
+
343
+ module.exports = {
344
+ importMain,
345
+ _processFilePath,
346
+ _loadImport,
347
+ _importToDb,
348
+ _buildImport,
349
+ import: importData,
350
+ exportMain,
351
+ _prepareExportConfig,
352
+ _getPath,
353
+ _saveExportFile,
354
+ _buildExport,
355
+ export: exportData,
356
+ delRepeatMain,
357
+ delRepeat
358
+ };
package/lib/manager.js CHANGED
@@ -2,6 +2,7 @@ const path = require("path");
2
2
  const Manager = require('mm_machine').Manager;
3
3
  const Dao = require("./dao");
4
4
  const { array } = require("mm_helper");
5
+ const { getSql, sqlAdmin } = require('mm_sql');
5
6
 
6
7
  /**
7
8
  * Sql模板管理器类(融合自 mm_os com/sql)
@@ -55,14 +56,6 @@ class DaoManager extends Manager {
55
56
  run_path: process.cwd() + path.sep,
56
57
  };
57
58
 
58
- /**
59
- * 构造函数
60
- * @param {object} config 配置参数
61
- * @param {object} parent 父级模块
62
- */
63
- constructor(config, parent) {
64
- super({ ...DaoManager.config, ...config }, parent);
65
- }
66
59
  }
67
60
 
68
61
  /**
@@ -104,7 +97,7 @@ DaoManager.prototype._stop = async function () {
104
97
  * @returns {object} 驱动实例
105
98
  */
106
99
  DaoManager.prototype.add = function (config) {
107
- var drive = new this.Drive(
100
+ let drive = new this.Drive(
108
101
  {
109
102
  dict: this.config.dict,
110
103
  user_path: this.config.user_path,
@@ -127,11 +120,11 @@ DaoManager.prototype.add = function (config) {
127
120
  * @returns {Promise<boolean>} 是否删除成功
128
121
  */
129
122
  DaoManager.prototype.del = async function (name) {
130
- var drive = this.get(name);
123
+ let drive = this.get(name);
131
124
  if (drive) {
132
125
  await drive.stop();
133
126
  }
134
- return await Manager.prototype.del.call(this, name);
127
+ return Manager.prototype.del.call(this, name);
135
128
  };
136
129
 
137
130
  /**
@@ -141,7 +134,7 @@ DaoManager.prototype.del = async function (name) {
141
134
  * @returns {Promise<boolean>} 是否执行成功
142
135
  */
143
136
  DaoManager.prototype.reload = async function (name) {
144
- var drive = this.get(name);
137
+ let drive = this.get(name);
145
138
  if (!drive) {
146
139
  this.log("warn", "驱动不存在,无法重启", name);
147
140
  return false;
@@ -181,7 +174,7 @@ DaoManager.prototype._getInfos = function () {
181
174
  * @returns {Array} 排序后的配置列表
182
175
  */
183
176
  DaoManager.prototype.sort = function () {
184
- var infos = this.configs;
177
+ let infos = this.configs;
185
178
  return array.sortBy(infos, "asc", "name");
186
179
  };
187
180
 
@@ -192,14 +185,43 @@ DaoManager.prototype.sort = function () {
192
185
  * @param {object} query 查询参数
193
186
  * @param {object} body 修改参数
194
187
  * @param {object} [db] 数据库管理器,不传时由驱动按自身配置选择数据源
195
- * @returns {object} 执行结果
188
+ * @returns {Promise<object>} 执行结果
189
+ * @throws {Error} 驱动不存在时抛出
196
190
  */
197
191
  DaoManager.prototype.run = async function (name, query, body, db) {
198
- var obj = this.get(name);
199
- if (obj) {
200
- return await obj.run(query, body, db);
192
+ let obj = this.get(name);
193
+ if (!obj) {
194
+ throw new Error('驱动不存在: ' + name);
201
195
  }
202
- return null;
196
+ return obj.run(query, body, db);
197
+ };
198
+
199
+ /**
200
+ * 取本作用域的 Sql 实例:有 config.sql 时按该配置取/建,否则复用主库 os。
201
+ * sqlAdmin / getSql 自身按名字缓存实例,管理器不再另存一份。
202
+ * @returns {object} Sql 实例
203
+ * @private
204
+ */
205
+ DaoManager.prototype._getSql = function () {
206
+ let sql_config = this.config.sql;
207
+ return sql_config ? sqlAdmin(this.config.name || 'os', sql_config) : getSql();
208
+ };
209
+
210
+ /**
211
+ * 获取数据库管理器(mm_api 登录态等按管理器级入口取库)
212
+ * @returns {object} 数据库管理器实例
213
+ */
214
+ DaoManager.prototype.db = function () {
215
+ return this._getSql().db();
216
+ };
217
+
218
+ /**
219
+ * 读取并执行 .sql 脚本(README 初始化示例用法)
220
+ * @param {string} file SQL 文件路径
221
+ * @returns {Promise<object>} 执行结果统计对象
222
+ */
223
+ DaoManager.prototype.load = function (file) {
224
+ return this._getSql().load(file);
203
225
  };
204
226
 
205
227
  module.exports = DaoManager;
package/lib/script.tpl.js CHANGED
@@ -6,12 +6,12 @@ module.exports = {
6
6
  * 仅在需要完全自定义数据操作时使用; 未配置 main 时使用内置引擎。
7
7
  * @param {object} params 参数对象 { query: 查询条件, body: 正文参数 }
8
8
  * @param {object} db 数据管理器, 如: { next: async function(){}, ret: {} }
9
- * @returns {object} 返回执行结果
9
+ * @returns {Promise<object>} 返回执行结果
10
10
  */
11
11
  async main(params, db) {
12
- var { query, body } = params;
12
+ let { query, body } = params;
13
13
  // 内置引擎仍可通过 this 调用, 例如:
14
- // var list = await this.get(db, query, body);
15
- return await this.get(db, query, body);
14
+ // let list = await this.get(db, query, body);
15
+ return this.get(db, query, body);
16
16
  }
17
17
  };
package/package.json CHANGED
@@ -1,65 +1,72 @@
1
1
  {
2
- "name": "mm_dao",
3
- "version": "1.9.0",
4
- "description": "一个通用的SQL帮助类,支持通过切换db_type实现对不同数据库的操作,包括MySQL和SQLite等;另提供 manager.js/drive.js 的SQL模板管理器(融合自 mm_os com/sql,基于 mm_corejs,内置管理器池,无全局/原型扩展依赖)",
5
- "main": "index.js",
6
- "scripts": {
7
- "start": "node tests/test.js",
8
- "test": "node tests/test.js && node --test tests/scope.test.js",
9
- "lint": "eslint . --config eslint.config.js",
10
- "lint:fix": "eslint . --config eslint.config.js --fix",
11
- "lint:check": "eslint . --config eslint.config.js --max-warnings 0"
12
- },
13
- "repository": {
14
- "type": "git",
15
- "url": "git+https://gitee.com/qiuwenwu91/mm_dao.git"
16
- },
17
- "keywords": [
18
- "mm_mod",
19
- "mm_infra",
20
- "mm_runtime",
21
- "sql",
22
- "mysql",
23
- "sql server",
24
- "sqlite",
25
- "database",
26
- "async",
27
- "await",
28
- "promise",
29
- "add",
30
- "del",
31
- "set",
32
- "get",
33
- "query",
34
- "run",
35
- "exec",
36
- "orm",
37
- "多数据库",
38
- "database-adapter"
39
- ],
40
- "layer": "infrastructure",
41
- "group": "persistence",
42
- "author": "邱文武",
43
- "license": "ISC",
44
- "bugs": {
45
- "url": "https://gitee.com/qiuwenwu91/mm_dao/issues"
46
- },
47
- "homepage": "https://gitee.com/qiuwenwu91/mm_dao#readme",
48
- "engines": {
49
- "node": ">=12.0.0"
50
- },
51
- "dependencies": {
52
- "mm_admin": "^1.1.0",
53
- "mm_dbs": "^1.0.1",
54
- "mm_excel": "^1.2.4",
55
- "mm_helper": "^1.0.0",
56
- "mm_machine": "^6.0.0",
57
- "mm_ret": "^1.4.8",
58
- "mm_sql": "^1.8.0"
59
- },
60
- "devDependencies": {
61
- "eslint": "^10.9.1",
62
- "eslint-plugin-jsdoc": "^64.2.1",
63
- "mm_eslint": "^1.7.7"
64
- }
2
+ "name": "mm_dao",
3
+ "version": "1.9.2",
4
+ "description": "一个通用的SQL帮助类,支持通过切换db_type实现对不同数据库的操作,包括MySQL和SQLite等;另提供 manager.js/drive.js 的SQL模板管理器(融合自 mm_os com/sql,基于 mm_corejs,内置管理器池,无全局/原型扩展依赖)",
5
+ "main": "index.js",
6
+ "files": [
7
+ "index.js",
8
+ "lib",
9
+ "tool"
10
+ ],
11
+ "scripts": {
12
+ "start": "node tests/test.js",
13
+ "test": "node tests/test.js && node --test tests/scope.test.js",
14
+ "lint": "eslint . --config eslint.config.js",
15
+ "lint:fix": "eslint . --config eslint.config.js --fix",
16
+ "lint:check": "eslint . --config eslint.config.js --max-warnings 0"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://gitee.com/qiuwenwu91/mm_dao.git"
21
+ },
22
+ "keywords": [
23
+ "mm_mod",
24
+ "mm_infra",
25
+ "sql",
26
+ "mysql",
27
+ "sql server",
28
+ "sqlite",
29
+ "database",
30
+ "async",
31
+ "await",
32
+ "promise",
33
+ "add",
34
+ "del",
35
+ "set",
36
+ "get",
37
+ "query",
38
+ "run",
39
+ "exec",
40
+ "orm",
41
+ "多数据库",
42
+ "database-adapter"
43
+ ],
44
+ "layer": "store",
45
+ "group": "data",
46
+ "author": "邱文武",
47
+ "license": "ISC",
48
+ "bugs": {
49
+ "url": "https://gitee.com/qiuwenwu91/mm_dao/issues"
50
+ },
51
+ "homepage": "https://gitee.com/qiuwenwu91/mm_dao#readme",
52
+ "engines": {
53
+ "node": ">=12.0.0"
54
+ },
55
+ "dependencies": {
56
+ "mm_admin": "^1.1.0",
57
+ "mm_consts": "^1.0.2",
58
+ "mm_dbs": "^1.0.1",
59
+ "mm_eslint": "^1.0.0",
60
+ "mm_excel": "^1.2.4",
61
+ "mm_files": "^1.4.0",
62
+ "mm_helper": "^1.0.0",
63
+ "mm_machine": "^6.0.0",
64
+ "mm_ret": "^1.4.8",
65
+ "mm_sql": "^1.8.0"
66
+ },
67
+ "devDependencies": {
68
+ "eslint": "^10.9.1",
69
+ "eslint-plugin-jsdoc": "^64.2.1",
70
+ "mm_eslint": "^1.7.7"
71
+ }
65
72
  }