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.
- package/README.md +11 -2
- package/index.js +3 -13
- package/lib/admin.js +24 -0
- package/lib/dao.js +38 -1219
- package/lib/dao_crud.js +379 -0
- package/lib/dao_fields.js +145 -0
- package/lib/dao_query.js +238 -0
- package/lib/dao_scope.js +203 -0
- package/lib/dao_stats.js +129 -0
- package/lib/dao_transfer.js +358 -0
- package/lib/manager.js +40 -18
- package/lib/script.tpl.js +4 -4
- package/package.json +70 -63
- package/tool/dao/index.js +176 -0
- package/tool/dao/tool.json +121 -0
- package/tool/dao_admin/index.js +36 -0
- package/tool/dao_admin/tool.json +11 -0
- package/db/mm/user_account/_meta.json +0 -54
- package/db/mm.db +0 -0
- package/eslint.config.js +0 -235
- package/example/index.js +0 -78
- package/test_db/dao_test_1785991977965/account/1.json +0 -6
- package/test_db/dao_test_1785991977965/account/2.json +0 -6
- package/test_db/dao_test_1785991977965/account/_meta.json +0 -18
- package/test_db/dao_test_1785991977965/role/1.json +0 -7
- package/test_db/dao_test_1785991977965/role/2.json +0 -7
- package/test_db/dao_test_1785991977965/role/3.json +0 -7
- package/test_db/dao_test_1785991977965/role/_meta.json +0 -18
- package/test_db/dao_test_1785991977965/tool/_meta.json +0 -18
- package/test_db/dao_test_1785991977965/tool/bow_001.json +0 -8
- package/test_db/dao_test_1785991977965/tool/robe_001.json +0 -8
- package/test_db/dao_test_1785991977965/tool/shield_001.json +0 -8
- package/test_db/dao_test_1785991977965/tool/staff_001.json +0 -8
- package/test_db/dao_test_1785991977965/tool/sword_001.json +0 -8
- package/test_db/dao_test_1785993793800/account/1.json +0 -58
- package/test_db/dao_test_1785993793800/account/2.json +0 -25
- package/test_db/dao_test_1785993793800/account/_meta.json +0 -18
- package/test_db/dao_test_1785993793800/role/1.json +0 -7
- package/test_db/dao_test_1785993793800/role/2.json +0 -7
- package/test_db/dao_test_1785993793800/role/3.json +0 -7
- package/test_db/dao_test_1785993793800/role/_meta.json +0 -18
- package/test_db/dao_test_1785993793800/tool/_meta.json +0 -18
- package/test_db/dao_test_1785993793800/tool/bow_001.json +0 -8
- package/test_db/dao_test_1785993793800/tool/robe_001.json +0 -8
- package/test_db/dao_test_1785993793800/tool/shield_001.json +0 -8
- package/test_db/dao_test_1785993793800/tool/staff_001.json +0 -8
- package/test_db/dao_test_1785993793800/tool/sword_001.json +0 -8
- package/test_db/dao_test_1785993841615/account/1.json +0 -58
- package/test_db/dao_test_1785993841615/account/2.json +0 -25
- package/test_db/dao_test_1785993841615/account/_meta.json +0 -18
- package/test_db/dao_test_1785993841615/role/1.json +0 -7
- package/test_db/dao_test_1785993841615/role/2.json +0 -7
- package/test_db/dao_test_1785993841615/role/3.json +0 -7
- package/test_db/dao_test_1785993841615/role/_meta.json +0 -18
- package/test_db/dao_test_1785993841615/tool/_meta.json +0 -18
- package/test_db/dao_test_1785993841615/tool/bow_001.json +0 -8
- package/test_db/dao_test_1785993841615/tool/robe_001.json +0 -8
- package/test_db/dao_test_1785993841615/tool/shield_001.json +0 -8
- package/test_db/dao_test_1785993841615/tool/staff_001.json +0 -8
- package/test_db/dao_test_1785993841615/tool/sword_001.json +0 -8
- package/test_db/dao_test_1785998204124/account/1.json +0 -58
- package/test_db/dao_test_1785998204124/account/2.json +0 -25
- package/test_db/dao_test_1785998204124/account/_meta.json +0 -18
- package/test_db/dao_test_1785998204124/role/1.json +0 -7
- package/test_db/dao_test_1785998204124/role/2.json +0 -7
- package/test_db/dao_test_1785998204124/role/3.json +0 -7
- package/test_db/dao_test_1785998204124/role/_meta.json +0 -18
- package/test_db/dao_test_1785998204124/tool/_meta.json +0 -18
- package/test_db/dao_test_1785998204124/tool/bow_001.json +0 -8
- package/test_db/dao_test_1785998204124/tool/robe_001.json +0 -8
- package/test_db/dao_test_1785998204124/tool/shield_001.json +0 -8
- package/test_db/dao_test_1785998204124/tool/staff_001.json +0 -8
- package/test_db/dao_test_1785998204124/tool/sword_001.json +0 -8
- package/test_db/dao_test_1785998207460/account/1.json +0 -58
- package/test_db/dao_test_1785998207460/account/2.json +0 -25
- package/test_db/dao_test_1785998207460/account/_meta.json +0 -18
- package/test_db/dao_test_1785998207460/role/1.json +0 -7
- package/test_db/dao_test_1785998207460/role/2.json +0 -7
- package/test_db/dao_test_1785998207460/role/3.json +0 -7
- package/test_db/dao_test_1785998207460/role/_meta.json +0 -18
- package/test_db/dao_test_1785998207460/tool/_meta.json +0 -18
- package/test_db/dao_test_1785998207460/tool/bow_001.json +0 -8
- package/test_db/dao_test_1785998207460/tool/robe_001.json +0 -8
- package/test_db/dao_test_1785998207460/tool/shield_001.json +0 -8
- package/test_db/dao_test_1785998207460/tool/staff_001.json +0 -8
- package/test_db/dao_test_1785998207460/tool/sword_001.json +0 -8
- package/test_db/dao_test_1785998210638/account/1.json +0 -58
- package/test_db/dao_test_1785998210638/account/2.json +0 -25
- package/test_db/dao_test_1785998210638/account/_meta.json +0 -18
- package/test_db/dao_test_1785998210638/role/1.json +0 -7
- package/test_db/dao_test_1785998210638/role/2.json +0 -7
- package/test_db/dao_test_1785998210638/role/3.json +0 -7
- package/test_db/dao_test_1785998210638/role/_meta.json +0 -18
- package/test_db/dao_test_1785998210638/tool/_meta.json +0 -18
- package/test_db/dao_test_1785998210638/tool/bow_001.json +0 -8
- package/test_db/dao_test_1785998210638/tool/robe_001.json +0 -8
- package/test_db/dao_test_1785998210638/tool/shield_001.json +0 -8
- package/test_db/dao_test_1785998210638/tool/staff_001.json +0 -8
- package/test_db/dao_test_1785998210638/tool/sword_001.json +0 -8
- package/test_db/dao_test_1785998213852/account/1.json +0 -58
- package/test_db/dao_test_1785998213852/account/2.json +0 -25
- package/test_db/dao_test_1785998213852/account/_meta.json +0 -18
- package/test_db/dao_test_1785998213852/role/1.json +0 -7
- package/test_db/dao_test_1785998213852/role/2.json +0 -7
- package/test_db/dao_test_1785998213852/role/3.json +0 -7
- package/test_db/dao_test_1785998213852/role/_meta.json +0 -18
- package/test_db/dao_test_1785998213852/tool/_meta.json +0 -18
- package/test_db/dao_test_1785998213852/tool/bow_001.json +0 -8
- package/test_db/dao_test_1785998213852/tool/robe_001.json +0 -8
- package/test_db/dao_test_1785998213852/tool/shield_001.json +0 -8
- package/test_db/dao_test_1785998213852/tool/staff_001.json +0 -8
- package/test_db/dao_test_1785998213852/tool/sword_001.json +0 -8
- package/test_db/dao_test_1785998217094/account/1.json +0 -58
- package/test_db/dao_test_1785998217094/account/2.json +0 -25
- package/test_db/dao_test_1785998217094/account/_meta.json +0 -18
- package/test_db/dao_test_1785998217094/role/1.json +0 -7
- package/test_db/dao_test_1785998217094/role/2.json +0 -7
- package/test_db/dao_test_1785998217094/role/3.json +0 -7
- package/test_db/dao_test_1785998217094/role/_meta.json +0 -18
- package/test_db/dao_test_1785998217094/tool/_meta.json +0 -18
- package/test_db/dao_test_1785998217094/tool/bow_001.json +0 -8
- package/test_db/dao_test_1785998217094/tool/robe_001.json +0 -8
- package/test_db/dao_test_1785998217094/tool/shield_001.json +0 -8
- package/test_db/dao_test_1785998217094/tool/staff_001.json +0 -8
- package/test_db/dao_test_1785998217094/tool/sword_001.json +0 -8
- package/test_db/dao_test_1786000291805/account/1.json +0 -58
- package/test_db/dao_test_1786000291805/account/2.json +0 -25
- package/test_db/dao_test_1786000291805/account/_meta.json +0 -18
- package/test_db/dao_test_1786000291805/role/1.json +0 -7
- package/test_db/dao_test_1786000291805/role/2.json +0 -7
- package/test_db/dao_test_1786000291805/role/3.json +0 -7
- package/test_db/dao_test_1786000291805/role/_meta.json +0 -18
- package/test_db/dao_test_1786000291805/tool/_meta.json +0 -18
- package/test_db/dao_test_1786000291805/tool/bow_001.json +0 -8
- package/test_db/dao_test_1786000291805/tool/robe_001.json +0 -8
- package/test_db/dao_test_1786000291805/tool/shield_001.json +0 -8
- package/test_db/dao_test_1786000291805/tool/staff_001.json +0 -8
- package/test_db/dao_test_1786000291805/tool/sword_001.json +0 -8
- package/test_db/dao_test_1786001183999/account/1.json +0 -58
- package/test_db/dao_test_1786001183999/account/2.json +0 -25
- package/test_db/dao_test_1786001183999/account/_meta.json +0 -18
- package/test_db/dao_test_1786001183999/role/1.json +0 -7
- package/test_db/dao_test_1786001183999/role/2.json +0 -7
- package/test_db/dao_test_1786001183999/role/3.json +0 -7
- package/test_db/dao_test_1786001183999/role/_meta.json +0 -18
- package/test_db/dao_test_1786001183999/tool/_meta.json +0 -18
- package/test_db/dao_test_1786001183999/tool/bow_001.json +0 -8
- package/test_db/dao_test_1786001183999/tool/robe_001.json +0 -8
- package/test_db/dao_test_1786001183999/tool/shield_001.json +0 -8
- package/test_db/dao_test_1786001183999/tool/staff_001.json +0 -8
- package/test_db/dao_test_1786001183999/tool/sword_001.json +0 -8
- package/test_db/dao_test_1786001297521/account/1.json +0 -58
- package/test_db/dao_test_1786001297521/account/2.json +0 -25
- package/test_db/dao_test_1786001297521/account/_meta.json +0 -18
- package/test_db/dao_test_1786001297521/role/1.json +0 -7
- package/test_db/dao_test_1786001297521/role/2.json +0 -7
- package/test_db/dao_test_1786001297521/role/3.json +0 -7
- package/test_db/dao_test_1786001297521/role/_meta.json +0 -18
- package/test_db/dao_test_1786001297521/tool/_meta.json +0 -18
- package/test_db/dao_test_1786001297521/tool/bow_001.json +0 -8
- package/test_db/dao_test_1786001297521/tool/robe_001.json +0 -8
- package/test_db/dao_test_1786001297521/tool/shield_001.json +0 -8
- package/test_db/dao_test_1786001297521/tool/staff_001.json +0 -8
- package/test_db/dao_test_1786001297521/tool/sword_001.json +0 -8
- package/test_db/debug_dao_1785992422444/account/1.json +0 -24
- package/test_db/debug_dao_1785992422444/account/_meta.json +0 -18
- package/test_db/debug_dao_1785992422444/role/1.json +0 -7
- package/test_db/debug_dao_1785992422444/role/2.json +0 -7
- package/test_db/debug_dao_1785992422444/role/_meta.json +0 -18
- package/test_db/debug_dao_1785992422444/tool/_meta.json +0 -18
- package/test_db/debug_dao_1785992422444/tool/shield_001.json +0 -7
- package/test_db/debug_dao_1785992422444/tool/staff_001.json +0 -7
- package/test_db/debug_dao_1785992422444/tool/sword_001.json +0 -7
- package/test_db/debug_dao_1785993785567/account/1.json +0 -47
- package/test_db/debug_dao_1785993785567/account/_meta.json +0 -18
- package/test_db/debug_dao_1785993785567/role/1.json +0 -7
- package/test_db/debug_dao_1785993785567/role/2.json +0 -7
- package/test_db/debug_dao_1785993785567/role/_meta.json +0 -18
- package/test_db/debug_dao_1785993785567/tool/_meta.json +0 -18
- package/test_db/debug_dao_1785993785567/tool/shield_001.json +0 -7
- package/test_db/debug_dao_1785993785567/tool/staff_001.json +0 -7
- package/test_db/debug_dao_1785993785567/tool/sword_001.json +0 -7
- package/test_db/debug_dao_check/tool/_meta.json +0 -18
- package/test_db/debug_dao_check/tool/sword_001.json +0 -6
- package/test_db/test_1785973671479/users/1.json +0 -5
- package/test_db/test_1785973671479/users/_meta.json +0 -20
- package/tests/dao_test.js +0 -213
- package/tests/debug_error_test.js +0 -165
- package/tests/debug_test.js +0 -35
- package/tests/debug_test2.js +0 -45
- package/tests/debug_test3.js +0 -54
- package/tests/detailed_error_test.js +0 -171
- package/tests/main.test.js +0 -261
- package/tests/query.test.js +0 -142
- package/tests/scope.test.js +0 -72
- package/tests/test.js +0 -325
- package/tests/test2.js +0 -64
package/lib/dao_crud.js
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
const { array, string } = require('mm_helper');
|
|
2
|
+
const { getRet } = require('mm_ret');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 增删改查实现
|
|
6
|
+
* 本文件为 Dao 原型扩展的职责分片:get / set / add / del / addOrSet 的主要实现与包装。
|
|
7
|
+
* 函数最终挂载到 Dao.prototype,内部通过 this 访问 Dao 实例。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 查询(主要)
|
|
12
|
+
* @param {object} db 数据库操作类
|
|
13
|
+
* @param {object} query 查询条件
|
|
14
|
+
* @param {object} method 方法
|
|
15
|
+
* @returns {Promise<object>} 返回查询结果
|
|
16
|
+
*/
|
|
17
|
+
async function getMain(db, query, method) {
|
|
18
|
+
if (string.has(this.config.field, '*{0}*')) {
|
|
19
|
+
let field = query[this._getFilter(this.config).field];
|
|
20
|
+
if (field && array.getMatch(this.config.field_hide, field)) {
|
|
21
|
+
return getRet().error(70003, '不合法的查询参数');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
let query_str = this.toWhere(db, query, method);
|
|
25
|
+
if (db.count_ret === 'true' || db.count_ret === true) {
|
|
26
|
+
return getRet().body(await db.getCountSql(query_str, db.orderby, db.field), null);
|
|
27
|
+
}
|
|
28
|
+
return getRet().list(await db.getSql(query_str, db.orderby, db.field));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 查询
|
|
33
|
+
* @param {object} db 数据库操作类
|
|
34
|
+
* @param {object} query 查询条件
|
|
35
|
+
* @param {object} body 修改项
|
|
36
|
+
* @returns {Promise<object>} 返回查询结果
|
|
37
|
+
*/
|
|
38
|
+
async function get(db, query, body) {
|
|
39
|
+
return this.getMain(db, query);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 查询单条数据
|
|
44
|
+
* @param {object} db 数据库操作类
|
|
45
|
+
* @param {object} query 查询条件
|
|
46
|
+
* @param {object} body 修改项
|
|
47
|
+
* @returns {Promise<object>} 返回查询结果
|
|
48
|
+
*/
|
|
49
|
+
async function getObj(db, query, body) {
|
|
50
|
+
let qy = { ...query };
|
|
51
|
+
qy.page = 1;
|
|
52
|
+
qy.size = 1;
|
|
53
|
+
// get_obj 只是「取一条」,它改的是共享表模型上的分页与字段状态,用完必须还原:
|
|
54
|
+
// 否则同一实例上的后续列表查询会被截断成 1 条,字段也可能串成 field_obj
|
|
55
|
+
let old_page = db.page;
|
|
56
|
+
let old_size = db.size;
|
|
57
|
+
let old_field = db.field;
|
|
58
|
+
try {
|
|
59
|
+
let ret = await this.getMain(db, qy, 'get_obj');
|
|
60
|
+
if (ret.result) {
|
|
61
|
+
let list = ret.result.list;
|
|
62
|
+
return getRet().body({ obj: list && list.length > 0 ? list[0] : null });
|
|
63
|
+
}
|
|
64
|
+
return ret;
|
|
65
|
+
} finally {
|
|
66
|
+
db.page = old_page;
|
|
67
|
+
db.size = old_size;
|
|
68
|
+
db.field = old_field;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 修改(主要)
|
|
74
|
+
* @param {object} db 数据库操作类
|
|
75
|
+
* @param {object} query 查询条件
|
|
76
|
+
* @param {object} body 修改项
|
|
77
|
+
* @returns {Promise<object>} 返回修改结果
|
|
78
|
+
*/
|
|
79
|
+
async function setMain(db, query, body) {
|
|
80
|
+
let { cg, qy } = this.ready(db, query);
|
|
81
|
+
let key = cg.key;
|
|
82
|
+
if (body[key]) {
|
|
83
|
+
qy[key] = body[key];
|
|
84
|
+
}
|
|
85
|
+
let query_str = this._buildQueryString(db, qy, cg);
|
|
86
|
+
let n = await db.setSql(query_str, db.tplBody(body, cg.update));
|
|
87
|
+
if (n < 1) {
|
|
88
|
+
let msg = this._errMsg(db);
|
|
89
|
+
return getRet().error(500, msg ? '修改失败!\n' + msg : '修改失败!');
|
|
90
|
+
}
|
|
91
|
+
return getRet().bl(true, '修改成功!');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 修改
|
|
96
|
+
* @param {object} db 数据库操作类
|
|
97
|
+
* @param {object} query 查询条件
|
|
98
|
+
* @param {object} body 修改项
|
|
99
|
+
* @returns {Promise<object>} 返回查询结果
|
|
100
|
+
*/
|
|
101
|
+
async function set(db, query, body) {
|
|
102
|
+
return this.setMain(db, query, body);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 填充默认写入值
|
|
107
|
+
* body_default 是「字段 = 值」模板,常用于补 user_id、org_id 这类隔离字段;
|
|
108
|
+
* 查询条件里的 query_default 会校验同样的字段,写入侧必须把值补齐,否则
|
|
109
|
+
* 新增的记录缺少隔离字段,按同一用户再也查不到(表现为反复新增重复数据)
|
|
110
|
+
* @param {object} db 数据库管理器
|
|
111
|
+
* @param {object} body 待写入数据
|
|
112
|
+
* @param {object} bt 值模板
|
|
113
|
+
* @param {boolean} force 是否无条件覆盖
|
|
114
|
+
* @returns {object} 补充默认值后的数据副本
|
|
115
|
+
* @private
|
|
116
|
+
*/
|
|
117
|
+
/**
|
|
118
|
+
* 按 body 模板填充字段(默认值/强制值),模板值为含上下文变量的表达式
|
|
119
|
+
* @param {object} db - 数据库连接实例(取 context 作为模板变量)
|
|
120
|
+
* @param {object} body - 写入数据体
|
|
121
|
+
* @param {object} bt - body 模板表(body_default 或 body_force)
|
|
122
|
+
* @param {boolean} force - true 时强制覆盖已有字段值(body_force),false 时仅填充缺失字段
|
|
123
|
+
* @returns {object} 填充后的数据体
|
|
124
|
+
* @private
|
|
125
|
+
*/
|
|
126
|
+
function _applyBodyTpl(db, body, bt, force) {
|
|
127
|
+
let dt = { ...body };
|
|
128
|
+
if (!bt || Object.keys(bt).length === 0) {
|
|
129
|
+
return dt;
|
|
130
|
+
}
|
|
131
|
+
let ctx = (db && db.context) || {};
|
|
132
|
+
for (let k in bt) {
|
|
133
|
+
let tpl = this._fillTpl(bt[k], ctx);
|
|
134
|
+
// 模板格式:`字段` = 值(空格、反引号、引号可省略)
|
|
135
|
+
// 不能用 left/right/trim 按单字符截取:值里带空格时会残留成 `user_id`` 这种非法字段名
|
|
136
|
+
let arr = tpl.split('=');
|
|
137
|
+
if (arr.length < 2) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
let fkey = arr[0].replace(/`/g, '').trim();
|
|
141
|
+
let fval = arr.slice(1).join('=').trim().replace(/^'(.*)'$/, '$1');
|
|
142
|
+
if (!fkey) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
// body_default:只在缺值时补;body_force:无条件以服务端值覆盖
|
|
146
|
+
if (!force && dt[fkey]) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
dt[fkey] = fval;
|
|
150
|
+
}
|
|
151
|
+
return dt;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 补默认字段(缺值才补)
|
|
156
|
+
* @param {object} db 数据库操作类
|
|
157
|
+
* @param {object} body 写入数据
|
|
158
|
+
* @param {object} cg 配置
|
|
159
|
+
* @returns {object} 补充后的数据副本
|
|
160
|
+
*/
|
|
161
|
+
function _fillBodyDefault(db, body, cg) {
|
|
162
|
+
return this._applyBodyTpl(db, body, cg.body_default, false);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 强制字段(无条件用上下文值覆盖客户端传值)
|
|
167
|
+
*
|
|
168
|
+
* 用于「归属字段不可由客户端决定」的场景:新增时传一个别家的 org_id
|
|
169
|
+
* 就能把记录塞过去,编辑时传一个别家的 org_id 就能把记录搬走。
|
|
170
|
+
* 这里不猜字段名,只按 dao.json 的 body_force 声明逐个覆盖。
|
|
171
|
+
* @param {object} db 数据库操作类
|
|
172
|
+
* @param {object} body 写入数据
|
|
173
|
+
* @param {object} cg 配置
|
|
174
|
+
* @returns {object} 覆盖后的数据副本
|
|
175
|
+
*/
|
|
176
|
+
function _fillBodyForce(db, body, cg) {
|
|
177
|
+
return this._applyBodyTpl(db, body, cg.body_force, true);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 添加(主要)
|
|
182
|
+
* @param {object} db 数据库操作类
|
|
183
|
+
* @param {object} body 添加项
|
|
184
|
+
* @returns {Promise<object>} 返回查询结果
|
|
185
|
+
*/
|
|
186
|
+
async function addMain(db, body) {
|
|
187
|
+
let cg = this.config;
|
|
188
|
+
let ret;
|
|
189
|
+
if (Object.keys(body).length > 0) {
|
|
190
|
+
let dt = this._fillBodyForce(db, this._fillBodyDefault(db, body, cg), cg);
|
|
191
|
+
let n = await db.add(dt);
|
|
192
|
+
if (n < 1) {
|
|
193
|
+
let msg = this._errMsg(db);
|
|
194
|
+
ret = getRet().error(500, msg ? '添加失败!\n' + msg : '添加失败!');
|
|
195
|
+
} else {
|
|
196
|
+
ret = getRet().bl(true, '添加成功!');
|
|
197
|
+
}
|
|
198
|
+
} else {
|
|
199
|
+
ret = getRet().error(30001, '参数不能为空');
|
|
200
|
+
}
|
|
201
|
+
if (cg.log) {
|
|
202
|
+
this.log('debug', '添加SQL语句', db.sql);
|
|
203
|
+
}
|
|
204
|
+
return ret;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 添加
|
|
209
|
+
* @param {object} db 数据库操作类
|
|
210
|
+
* @param {object} query 查询条件
|
|
211
|
+
* @param {object} body 修改项
|
|
212
|
+
* @returns {Promise<object>} 返回添加结果
|
|
213
|
+
*/
|
|
214
|
+
async function add(db, query, body) {
|
|
215
|
+
return this.addMain(db, body);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 批量添加(主要)
|
|
220
|
+
* @param {object} db 数据库操作类
|
|
221
|
+
* @param {Array} list 添加项列表
|
|
222
|
+
* @returns {Promise<object>} 返回批量添加结果
|
|
223
|
+
*/
|
|
224
|
+
async function addListMain(db, list) {
|
|
225
|
+
if (!Array.isArray(list) || list.length === 0) {
|
|
226
|
+
return getRet().error(30001, '参数不能为空');
|
|
227
|
+
}
|
|
228
|
+
let success_num = 0;
|
|
229
|
+
let error_num = 0;
|
|
230
|
+
let errors = [];
|
|
231
|
+
let list_error = [];
|
|
232
|
+
for (let i = 0; i < list.length; i++) {
|
|
233
|
+
let ret = await this.addMain(db, list[i]);
|
|
234
|
+
if (ret.error) {
|
|
235
|
+
error_num++;
|
|
236
|
+
errors.push(ret.error.message);
|
|
237
|
+
list_error.push(list[i]);
|
|
238
|
+
} else {
|
|
239
|
+
success_num++;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
let done = getRet().bl(success_num > 0, success_num > 0 ? '批量添加成功!' : '批量添加失败!');
|
|
243
|
+
Object.assign(done.result, {
|
|
244
|
+
total: list.length,
|
|
245
|
+
success: success_num,
|
|
246
|
+
error: error_num,
|
|
247
|
+
list_error: list_error,
|
|
248
|
+
errors: errors
|
|
249
|
+
});
|
|
250
|
+
return done;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* 批量添加
|
|
255
|
+
* @param {object} db 数据库操作类
|
|
256
|
+
* @param {Array} list 添加项列表
|
|
257
|
+
* @returns {Promise<object>} 返回批量添加结果
|
|
258
|
+
*/
|
|
259
|
+
async function addList(db, list) {
|
|
260
|
+
return this.addListMain(db, list);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* 删除(主要)
|
|
265
|
+
* @param {object} db 数据库操作类
|
|
266
|
+
* @param {object} query 查询条件
|
|
267
|
+
* @returns {Promise<object>} 返回查询结果
|
|
268
|
+
*/
|
|
269
|
+
async function delMain(db, query) {
|
|
270
|
+
let { cg, qy } = this.ready(db, query);
|
|
271
|
+
let ret;
|
|
272
|
+
// 与 get / set 保持一致:删除同样追加 query_default,
|
|
273
|
+
// 否则“查到的范围”与“删掉的范围”不一致(默认条件常用于用户/机构隔离,缺失会删到别人数据)
|
|
274
|
+
let query_str = this._buildQueryString(db, qy, cg);
|
|
275
|
+
// 拒绝空条件删除:部分适配器会把空 where 当成“全删”,不能靠它们兜底
|
|
276
|
+
if (!query_str) {
|
|
277
|
+
return getRet().error(30001, '删除条件不能为空');
|
|
278
|
+
}
|
|
279
|
+
let bl = await db.delSql(query_str);
|
|
280
|
+
if (bl < 1) {
|
|
281
|
+
let msg = this._errMsg(db);
|
|
282
|
+
ret = getRet().error(500, msg ? '删除失败!\n' + msg : '删除失败!');
|
|
283
|
+
} else {
|
|
284
|
+
ret = getRet().bl(true, '删除成功!');
|
|
285
|
+
}
|
|
286
|
+
if (cg.log) {
|
|
287
|
+
this.log('debug', '删除SQL语句', db.sql);
|
|
288
|
+
}
|
|
289
|
+
return ret;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* 删除
|
|
294
|
+
* @param {object} db 数据库操作类
|
|
295
|
+
* @param {object} query 查询条件
|
|
296
|
+
* @param {object} body 修改项
|
|
297
|
+
* @returns {Promise<object>} 返回删除结果
|
|
298
|
+
*/
|
|
299
|
+
async function del(db, query, body = {}) {
|
|
300
|
+
return this.delMain(db, { ...query, ...body });
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* 添加或修改(主要)
|
|
305
|
+
* @param {object} db 数据库操作类
|
|
306
|
+
* @param {object} query 查询条件
|
|
307
|
+
* @param {object} body 修改项
|
|
308
|
+
* @returns {Promise<object>} 返回修改结果
|
|
309
|
+
*/
|
|
310
|
+
async function addOrSetMain(db, query, body) {
|
|
311
|
+
let { cg, qy } = this.ready(db, query);
|
|
312
|
+
let ret;
|
|
313
|
+
if (Object.keys(body).length > 0 && Object.keys(qy).length > 0) {
|
|
314
|
+
// 与 get / set / del 同源:判断「是否已存在」同样带上 query_default,
|
|
315
|
+
// 否则会命中他人的记录并直接改掉(默认条件通常是用户/机构隔离条件)
|
|
316
|
+
let where = this._buildQueryString(db, qy, cg);
|
|
317
|
+
if (!where) {
|
|
318
|
+
return getRet().error(30001, '条件不能为空');
|
|
319
|
+
}
|
|
320
|
+
// 新增分支不会用到 where 里的隔离字段,需把 body_default 补进写入数据,
|
|
321
|
+
// 否则插入的记录缺少隔离字段,后续按同一用户再也查不到(反复新增重复数据)
|
|
322
|
+
let dt = this._fillBodyForce(db, this._fillBodyDefault(db, body, cg), cg);
|
|
323
|
+
let n;
|
|
324
|
+
if (typeof db.countSql === 'function') {
|
|
325
|
+
// 自行判断存在性再分派:部分适配器的 addOrSet 在新增分支只写 set 的数据,
|
|
326
|
+
// 不会把条件字段写进新记录,导致新记录不满足条件,下次调用又新增一条(重复数据)
|
|
327
|
+
let count = await db.countSql(where);
|
|
328
|
+
if (count > 0) {
|
|
329
|
+
n = await db.setSql(where, db.tplBody(body, cg.update));
|
|
330
|
+
} else {
|
|
331
|
+
n = await db.add({ ...qy, ...dt });
|
|
332
|
+
}
|
|
333
|
+
} else {
|
|
334
|
+
n = await db.addOrSet(where, dt);
|
|
335
|
+
}
|
|
336
|
+
if (n < 1) {
|
|
337
|
+
let msg = this._errMsg(db);
|
|
338
|
+
ret = getRet().error(500, msg ? '操作失败!\n' + msg : '操作失败!');
|
|
339
|
+
} else {
|
|
340
|
+
ret = getRet().bl(true, '操作成功!');
|
|
341
|
+
}
|
|
342
|
+
} else {
|
|
343
|
+
ret = getRet().error(30001, '参数不能为空');
|
|
344
|
+
}
|
|
345
|
+
if (cg.log) {
|
|
346
|
+
this.log('debug', '添加或修改SQL语句', db.sql);
|
|
347
|
+
}
|
|
348
|
+
return ret;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* 添加或修改
|
|
353
|
+
* @param {object} db 数据库操作类
|
|
354
|
+
* @param {object} query 查询条件
|
|
355
|
+
* @param {object} body 修改项
|
|
356
|
+
* @returns {Promise<object>} 返回修改结果
|
|
357
|
+
*/
|
|
358
|
+
async function addOrSet(db, query, body) {
|
|
359
|
+
return this.addOrSetMain(db, query, body);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
module.exports = {
|
|
363
|
+
getMain,
|
|
364
|
+
get,
|
|
365
|
+
getObj,
|
|
366
|
+
setMain,
|
|
367
|
+
set,
|
|
368
|
+
_applyBodyTpl,
|
|
369
|
+
_fillBodyDefault,
|
|
370
|
+
_fillBodyForce,
|
|
371
|
+
addMain,
|
|
372
|
+
add,
|
|
373
|
+
addListMain,
|
|
374
|
+
addList,
|
|
375
|
+
delMain,
|
|
376
|
+
del,
|
|
377
|
+
addOrSetMain,
|
|
378
|
+
addOrSet
|
|
379
|
+
};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
const { array, string } = require('mm_helper');
|
|
2
|
+
const dbAdmin = require('mm_dbs');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 字段、参数与导入导出格式
|
|
6
|
+
* 本文件为 Dao 原型扩展的职责分片:表字段获取、参数过滤与格式列表。
|
|
7
|
+
* 函数最终挂载到 Dao.prototype,内部通过 this 访问 Dao 实例。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 获取数据类型
|
|
12
|
+
* @param {string} data_type 数据类型
|
|
13
|
+
* @returns {string} 返回数据类型
|
|
14
|
+
*/
|
|
15
|
+
function getType(data_type) {
|
|
16
|
+
if (!data_type) {
|
|
17
|
+
return 'string';
|
|
18
|
+
}
|
|
19
|
+
return String(data_type).indexOf('int') !== -1 ? 'number' : 'string';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 获取数据
|
|
24
|
+
* @param {object} db 数据库操作类
|
|
25
|
+
* @param {string} field 要获取的字段
|
|
26
|
+
* @returns {Promise<Array>} 返回参数信息列表
|
|
27
|
+
*/
|
|
28
|
+
async function getParam(db, field) {
|
|
29
|
+
let cg = this.config;
|
|
30
|
+
if (cg.params) {
|
|
31
|
+
return cg.params;
|
|
32
|
+
}
|
|
33
|
+
if (this.params) {
|
|
34
|
+
return this.params;
|
|
35
|
+
}
|
|
36
|
+
let lt = await this._getFieldsFromCache(cg);
|
|
37
|
+
if (!lt.length) {
|
|
38
|
+
lt = await this._getFieldsFromDb(db, cg);
|
|
39
|
+
}
|
|
40
|
+
return this._filterFields(lt, field, cg);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 从缓存获取字段列表
|
|
45
|
+
* @param {object} cg 配置
|
|
46
|
+
* @returns {Array} 字段列表
|
|
47
|
+
* @private
|
|
48
|
+
*/
|
|
49
|
+
function _getFieldsFromCache(cg) {
|
|
50
|
+
let lt = [];
|
|
51
|
+
let pool = dbAdmin('os');
|
|
52
|
+
if (pool) {
|
|
53
|
+
let dt = pool.get(cg.table);
|
|
54
|
+
if (dt && dt.config) {
|
|
55
|
+
let list = dt.config.fields;
|
|
56
|
+
for (let i = 0; i < list.length; i++) {
|
|
57
|
+
let name = list[i].replace(/`/g, '');
|
|
58
|
+
lt.push({ name, title: name });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return lt;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 从数据库获取字段列表
|
|
67
|
+
* @param {object} db 数据库操作类
|
|
68
|
+
* @param {object} cg 配置
|
|
69
|
+
* @returns {Promise<Array>} 字段列表
|
|
70
|
+
* @private
|
|
71
|
+
*/
|
|
72
|
+
async function _getFieldsFromDb(db, cg) {
|
|
73
|
+
let lt = [];
|
|
74
|
+
db.table = cg.table;
|
|
75
|
+
let list = await db.fields();
|
|
76
|
+
for (let i = 0; i < list.length; i++) {
|
|
77
|
+
let o = list[i];
|
|
78
|
+
let name = o.name;
|
|
79
|
+
let note = o.note ? o.note.replace(':', ':') : name;
|
|
80
|
+
lt.push({
|
|
81
|
+
name,
|
|
82
|
+
title: string.left(note, ':', true),
|
|
83
|
+
type: this.getType(o.type),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return lt;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 过滤字段
|
|
91
|
+
* @param {Array} lt 字段列表
|
|
92
|
+
* @param {string} field 字段筛选
|
|
93
|
+
* @param {object} cg 配置
|
|
94
|
+
* @returns {Array} 过滤后的字段列表
|
|
95
|
+
* @private
|
|
96
|
+
*/
|
|
97
|
+
function _filterFields(lt, field, cg) {
|
|
98
|
+
let fields = field || cg.field_default;
|
|
99
|
+
if (fields && fields !== '*') {
|
|
100
|
+
let arr = fields.replace(/`/g, '').split(',');
|
|
101
|
+
return arr.map((name) => array.getObj(lt, { name })).filter((obj) => obj);
|
|
102
|
+
}
|
|
103
|
+
return lt;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 获取导入导出格式
|
|
108
|
+
* @param {object} db 数据库操作类
|
|
109
|
+
* @returns {Promise<Array>} 格式列表
|
|
110
|
+
*/
|
|
111
|
+
async function getFormat(db) {
|
|
112
|
+
// 用原型继承派生一个“不分页”的视图:直接 {...db} 只复制自有属性,
|
|
113
|
+
// getSql 等原型方法会丢失,取格式数据时会报 dbs.getSql is not a function
|
|
114
|
+
let dbs = Object.create(db);
|
|
115
|
+
dbs.size = 0;
|
|
116
|
+
let fmt = this.config.format;
|
|
117
|
+
for (let i = 0; i < fmt.length; i++) {
|
|
118
|
+
let o = fmt[i];
|
|
119
|
+
if (o.table) {
|
|
120
|
+
if (!o.list || o.list.length === 0) {
|
|
121
|
+
dbs.table = o.table;
|
|
122
|
+
if (!o.id) {
|
|
123
|
+
o.id = o.key;
|
|
124
|
+
}
|
|
125
|
+
o.list = await dbs.getSql(o.where, null, o.id + ',' + o.name);
|
|
126
|
+
if (o.id !== o.key) {
|
|
127
|
+
for (let j = 0; j < o.list.length; j++) {
|
|
128
|
+
let m = o.list[j];
|
|
129
|
+
m[o.key] = m[o.name];
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return fmt;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
module.exports = {
|
|
139
|
+
getType,
|
|
140
|
+
getParam,
|
|
141
|
+
_getFieldsFromCache,
|
|
142
|
+
_getFieldsFromDb,
|
|
143
|
+
_filterFields,
|
|
144
|
+
getFormat
|
|
145
|
+
};
|