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
@@ -1,18 +0,0 @@
1
- {
2
- "table": "tool",
3
- "key": "id",
4
- "auto_increment": 0,
5
- "created_at": "2026-08-06T05:23:05.774Z",
6
- "fields": [
7
- {
8
- "name": "id",
9
- "type": "varchar",
10
- "not_null": true,
11
- "auto": false,
12
- "pk": true,
13
- "default_value": null,
14
- "note": "道具表"
15
- }
16
- ],
17
- "indexes": []
18
- }
@@ -1,7 +0,0 @@
1
- {
2
- "id": "shield_001",
3
- "role_id": "战士",
4
- "name": "铁盾",
5
- "atk": 0,
6
- "def": 30
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "id": "staff_001",
3
- "role_id": "法师",
4
- "name": "魔法杖",
5
- "atk": 40,
6
- "def": 0
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "id": "sword_001",
3
- "role_id": "战士",
4
- "name": "勇者之剑",
5
- "atk": 50,
6
- "def": 0
7
- }
@@ -1,18 +0,0 @@
1
- {
2
- "table": "tool",
3
- "key": "id",
4
- "auto_increment": 0,
5
- "created_at": "2026-08-06T05:00:38.663Z",
6
- "fields": [
7
- {
8
- "name": "id",
9
- "type": "varchar",
10
- "not_null": true,
11
- "auto": false,
12
- "pk": true,
13
- "default_value": null,
14
- "note": "道具表"
15
- }
16
- ],
17
- "indexes": []
18
- }
@@ -1,6 +0,0 @@
1
- {
2
- "id": "sword_001",
3
- "role_id": "战士",
4
- "name": "勇者之剑",
5
- "atk": 50
6
- }
@@ -1,5 +0,0 @@
1
- {
2
- "name": "张三",
3
- "gold": 9999,
4
- "id": 1
5
- }
@@ -1,20 +0,0 @@
1
- {
2
- "key": "id",
3
- "fields": [
4
- {
5
- "name": "id",
6
- "type": "INT",
7
- "pk": true,
8
- "auto": true
9
- },
10
- {
11
- "name": "name",
12
- "type": "VARCHAR(50)"
13
- },
14
- {
15
- "name": "gold",
16
- "type": "INT"
17
- }
18
- ],
19
- "auto_increment": 2
20
- }
package/tests/dao_test.js DELETED
@@ -1,213 +0,0 @@
1
- /**
2
- * @fileOverview dao.js 测试用例
3
- * @description 测试 Dao 实时模型的嵌套访问和自动同步功能
4
- */
5
- const { sqlAdmin } = require('../index');
6
- const { Dao } = require('./dao');
7
-
8
- // 使用 mm_jsondb 后端(无需外部数据库)
9
- const config = {
10
- way: 'json',
11
- json: {
12
- dir: '/test_db/'.fullname(),
13
- database: 'dao_test_' + Date.now(),
14
- debug: false
15
- }
16
- };
17
-
18
- const sql = sqlAdmin('dao_test', config);
19
- const dao = new Dao(sql);
20
-
21
- // 注册模型树
22
- dao.model('account', {
23
- key: 'username',
24
- children: {
25
- role: {
26
- key: 'name',
27
- children: {
28
- tool: {
29
- key: 'id'
30
- }
31
- }
32
- }
33
- }
34
- });
35
-
36
- let passed = 0;
37
- let failed = 0;
38
-
39
- function assert(condition, msg) {
40
- if (condition) {
41
- passed++;
42
- console.log(' ✓ ' + msg);
43
- } else {
44
- failed++;
45
- console.error(' ✗ ' + msg);
46
- }
47
- }
48
-
49
- async function setupTestData() {
50
- console.log('\n=== 准备测试数据 ===');
51
-
52
- const db = sql.db();
53
-
54
- // 创建 account 表
55
- await db.addTable('account', 'username', 'varchar', false, '账户表');
56
- const accDb = sql.table('account', 'username');
57
- await accDb.add({ username: 'player1', password: '123', email: 'p1@test.com' });
58
- await accDb.add({ username: 'player2', password: '456', email: 'p2@test.com' });
59
- console.log(' 已创建 account 表,插入 2 条数据');
60
-
61
- // 创建 role 表
62
- await db.addTable('role', 'name', 'varchar', false, '角色表');
63
- const roleDb = sql.table('role', 'name');
64
- await roleDb.add({ name: '战士', account_id: 'player1', level: 10, exp: 500 });
65
- await roleDb.add({ name: '法师', account_id: 'player1', level: 8, exp: 300 });
66
- await roleDb.add({ name: '弓箭手', account_id: 'player2', level: 5, exp: 100 });
67
- console.log(' 已创建 role 表,插入 3 条数据');
68
-
69
- // 创建 tool 表
70
- await db.addTable('tool', 'id', 'varchar', false, '道具表');
71
- const toolDb = sql.table('tool', 'id');
72
- await toolDb.add({ id: 'sword_001', role_id: '战士', name: '勇者之剑', atk: 50, def: 0, type: 'weapon' });
73
- await toolDb.add({ id: 'shield_001', role_id: '战士', name: '铁盾', atk: 0, def: 30, type: 'armor' });
74
- await toolDb.add({ id: 'staff_001', role_id: '法师', name: '魔法杖', atk: 40, def: 0, type: 'weapon' });
75
- await toolDb.add({ id: 'robe_001', role_id: '法师', name: '法师长袍', atk: 0, def: 20, type: 'armor' });
76
- await toolDb.add({ id: 'bow_001', role_id: '弓箭手', name: '精灵之弓', atk: 35, def: 0, type: 'weapon' });
77
- console.log(' 已创建 tool 表,插入 5 条数据');
78
- }
79
-
80
- async function runTests() {
81
- await setupTestData();
82
-
83
- // ========== 测试1:基本嵌套访问 ==========
84
- console.log('\n=== 测试1:基本嵌套访问 ===');
85
- const account = await dao.account('player1');
86
- assert(account !== null, '获取 account 成功');
87
- assert(account.username === 'player1', '用户名正确');
88
- assert(account.email === 'p1@test.com', '邮箱正确');
89
-
90
- // ========== 测试2:访问子关系 ==========
91
- console.log('\n=== 测试2:访问子关系 ===');
92
- assert(account.role !== undefined, 'role 关系存在');
93
- assert(typeof account.role === 'object', 'role 是对象');
94
- assert(account.role['战士'] !== undefined, '可以访问 role["战士"]');
95
- assert(account.role['法师'] !== undefined, '可以访问 role["法师"]');
96
- assert(account.role['战士'].level === 10, '战士等级为10');
97
- assert(account.role['法师'].level === 8, '法师等级为8');
98
-
99
- // ========== 测试3:嵌套访问道具 ==========
100
- console.log('\n=== 测试3:嵌套访问道具 ===');
101
- const warrior = account.role['战士'];
102
- assert(warrior.tool !== undefined, '战士有 tool 关系');
103
- assert(warrior.tool['sword_001'] !== undefined, '可以访问 tool["sword_001"]');
104
- assert(warrior.tool['sword_001'].name === '勇者之剑', '道具名称正确');
105
- assert(warrior.tool['sword_001'].atk === 50, '道具攻击力正确');
106
- assert(warrior.tool['shield_001'] !== undefined, '可以访问 tool["shield_001"]');
107
- assert(warrior.tool['shield_001'].def === 30, '盾牌防御力正确');
108
-
109
- // ========== 测试4:修改嵌套属性并验证数据库同步 ==========
110
- console.log('\n=== 测试4:修改嵌套属性并验证数据库同步 ===');
111
-
112
- // 修改道具的 atk 属性
113
- account.role['战士'].tool['sword_001'].atk = 100;
114
- console.log(' 已设置 sword_001.atk = 100');
115
-
116
- // 等待异步写入完成
117
- await sleep(500);
118
-
119
- // 从数据库重新读取验证
120
- const toolDb = sql.table('tool', 'id');
121
- const sword = await toolDb.getObj({ id: 'sword_001' });
122
- assert(sword.atk === 100, '数据库同步:sword_001.atk === 100');
123
-
124
- // 修改角色等级
125
- account.role['战士'].level = 15;
126
- console.log(' 已设置 战士.level = 15');
127
- await sleep(500);
128
-
129
- const roleDb = sql.table('role', 'name');
130
- const warrior2 = await roleDb.getObj({ name: '战士' });
131
- assert(warrior2.level === 15, '数据库同步:战士.level === 15');
132
-
133
- // 修改账户邮箱
134
- account.email = 'new_email@test.com';
135
- console.log(' 已设置 account.email = new_email@test.com');
136
- await sleep(500);
137
-
138
- const accDb = sql.table('account', 'username');
139
- const acc2 = await accDb.getObj({ username: 'player1' });
140
- assert(acc2.email === 'new_email@test.com', '数据库同步:account.email === new_email@test.com');
141
-
142
- // ========== 测试5:多角色数据隔离 ==========
143
- console.log('\n=== 测试5:多角色数据隔离 ===');
144
- const account2 = await dao.account('player2');
145
- assert(account2 !== null, '获取 account2 成功');
146
- assert(account2.role['弓箭手'] !== undefined, 'account2 有弓箭手角色');
147
- assert(account2.role['战士'] === undefined, 'account2 没有战士角色(数据隔离正确)');
148
- assert(account2.role['弓箭手'].tool['bow_001'].atk === 35, '弓箭手道具攻击力正确');
149
-
150
- // ========== 测试6:连续修改同一属性 ==========
151
- console.log('\n=== 测试6:连续修改同一属性 ===');
152
- account.role['法师'].tool['staff_001'].atk = 60;
153
- account.role['法师'].tool['staff_001'].atk = 70;
154
- account.role['法师'].tool['staff_001'].atk = 80;
155
- console.log(' 连续设置 staff_001.atk = 60, 70, 80');
156
- await sleep(1000);
157
-
158
- const staff = await toolDb.getObj({ id: 'staff_001' });
159
- assert(staff.atk === 80, '数据库同步:连续修改后 staff_001.atk === 80');
160
-
161
- // ========== 测试7:不存在的记录 ==========
162
- console.log('\n=== 测试7:不存在的记录 ===');
163
- const notFound = await dao.account('nonexistent');
164
- assert(notFound === null, '不存在的用户名返回 null');
165
-
166
- // ========== 测试8:修改后重新加载验证 ==========
167
- console.log('\n=== 测试8:修改后重新加载验证 ===');
168
- const reloaded = await dao.account('player1');
169
- assert(reloaded.role['战士'].tool['sword_001'].atk === 100, '重新加载后 atk 值正确');
170
- assert(reloaded.role['战士'].level === 15, '重新加载后 level 值正确');
171
- assert(reloaded.email === 'new_email@test.com', '重新加载后 email 值正确');
172
-
173
- // ========== 测试9:参数校验 ==========
174
- console.log('\n=== 测试9:参数校验 ===');
175
- try {
176
- new Dao(null);
177
- assert(false, '应抛出异常');
178
- } catch (e) {
179
- assert(e instanceof TypeError, 'Dao(null) 抛出 TypeError');
180
- }
181
-
182
- try {
183
- dao.model('', { key: 'id' });
184
- assert(false, '应抛出异常');
185
- } catch (e) {
186
- assert(e instanceof TypeError, '空模型名抛出 TypeError');
187
- }
188
-
189
- try {
190
- dao.model('test', {});
191
- assert(false, '应抛出异常');
192
- } catch (e) {
193
- assert(e instanceof TypeError, '无 key 抛出 TypeError');
194
- }
195
-
196
- // ========== 结果汇总 ==========
197
- console.log('\n========================================');
198
- console.log(` 测试完成: ${passed} 通过, ${failed} 失败`);
199
- console.log('========================================\n');
200
-
201
- if (failed > 0) {
202
- process.exit(1);
203
- }
204
- }
205
-
206
- function sleep(ms) {
207
- return new Promise(resolve => setTimeout(resolve, ms));
208
- }
209
-
210
- runTests().catch(err => {
211
- console.error('测试异常:', err);
212
- process.exit(1);
213
- });
@@ -1,165 +0,0 @@
1
- const { Sql } = require('../index.js');
2
-
3
- /**
4
- * 测试适配器直接调用
5
- * @param {object} mysql_sql - MySQL实例
6
- */
7
- async function adapterDirect(mysql_sql) {
8
- console.log('\n测试1: 直接调用适配器run方法...');
9
- try {
10
- await mysql_sql._adapter.run('SELECT * FROM non_existent_table_xyz');
11
- console.log('❌ 适配器应该对无效SQL抛出错误,但没有抛出');
12
- } catch (error) {
13
- console.log('✅ 适配器正确抛出错误:', error.message);
14
- }
15
- }
16
-
17
- /**
18
- * 测试mm_sql的run方法调用
19
- * @param {object} mysql_sql - MySQL实例
20
- */
21
- async function sqlRun(mysql_sql) {
22
- console.log('\n测试2: 通过mm_sql的run方法调用...');
23
- try {
24
- await mysql_sql.run('SELECT * FROM non_existent_table_xyz');
25
- console.log('❌ mm_sql应该对无效SQL抛出错误,但没有抛出');
26
- } catch (error) {
27
- console.log('✅ mm_sql正确抛出错误:', error.message);
28
- }
29
- }
30
-
31
- /**
32
- * 测试参数验证
33
- * @param {object} mysql_sql - MySQL实例
34
- */
35
- async function paramValid(mysql_sql) {
36
- console.log('\n测试3: 测试参数验证...');
37
- try {
38
- await mysql_sql.run(123); // 无效参数类型
39
- console.log('❌ mm_sql应该对无效参数抛出TypeError,但没有抛出');
40
- } catch (error) {
41
- if (error instanceof TypeError) {
42
- console.log('✅ mm_sql正确抛出TypeError:', error.message);
43
- } else {
44
- console.log('❌ mm_sql抛出错误但不是TypeError:', error.message);
45
- }
46
- }
47
- }
48
-
49
- /**
50
- * 调试MySQL错误传播
51
- */
52
- async function debugMysqlError() {
53
- console.log('=== 调试MySQL错误传播 ===');
54
-
55
- try {
56
- // 创建mm_sql的MySQL实例
57
- const mysql_sql = new Sql({
58
- db_type: 'mysql',
59
- dir: './db',
60
- host: 'localhost',
61
- user: 'root',
62
- password: 'Asd159357',
63
- database: 'mm'
64
- });
65
-
66
- console.log('✅ mm_sql MySQL实例创建成功');
67
- console.log('适配器类型:', mysql_sql._adapter?.constructor?.name);
68
-
69
- await adapterDirect(mysql_sql);
70
- await sqlRun(mysql_sql);
71
- await paramValid(mysql_sql);
72
-
73
- } catch (error) {
74
- console.log('❌ mm_sql实例创建失败:', error.message);
75
- }
76
- }
77
-
78
- /**
79
- * 测试SQLite适配器直接调用
80
- * @param {object} sqlite_sql - SQLite实例
81
- */
82
- async function sqliteAdapter(sqlite_sql) {
83
- console.log('\n测试1: 直接调用适配器run方法...');
84
- try {
85
- await sqlite_sql._adapter.run('SELECT * FROM non_existent_table_xyz');
86
- console.log('❌ 适配器应该对无效SQL抛出错误,但没有抛出');
87
- } catch (error) {
88
- console.log('✅ 适配器正确抛出错误:', error.message);
89
- }
90
- }
91
-
92
- /**
93
- * 测试SQLite的run方法调用
94
- * @param {object} sqlite_sql - SQLite实例
95
- */
96
- async function sqliteRun(sqlite_sql) {
97
- console.log('\n测试2: 通过mm_sql的run方法调用...');
98
- try {
99
- await sqlite_sql.run('SELECT * FROM non_existent_table_xyz');
100
- console.log('❌ mm_sql应该对无效SQL抛出错误,但没有抛出');
101
- } catch (error) {
102
- console.log('✅ mm_sql正确抛出错误:', error.message);
103
- }
104
- }
105
-
106
- /**
107
- * 测试SQLite参数验证
108
- * @param {object} sqlite_sql - SQLite实例
109
- */
110
- async function sqliteParam(sqlite_sql) {
111
- console.log('\n测试3: 测试参数验证...');
112
- try {
113
- await sqlite_sql.run(123); // 无效参数类型
114
- console.log('❌ mm_sql应该对无效参数抛出TypeError,但没有抛出');
115
- } catch (error) {
116
- if (error instanceof TypeError) {
117
- console.log('✅ mm_sql正确抛出TypeError:', error.message);
118
- } else {
119
- console.log('❌ mm_sql抛出错误但不是TypeError:', error.message);
120
- }
121
- }
122
- }
123
-
124
- /**
125
- * 调试SQLite错误传播
126
- */
127
- async function debugSqliteError() {
128
- console.log('\n=== 调试SQLite错误传播 ===');
129
-
130
- try {
131
- // 创建mm_sql的SQLite实例
132
- const sqlite_sql = new Sql({
133
- db_type: 'sqlite',
134
- dir: './db',
135
- database: 'mm'
136
- });
137
-
138
- console.log('✅ mm_sql SQLite实例创建成功');
139
- console.log('适配器类型:', sqlite_sql._adapter?.constructor?.name);
140
-
141
- await testSqliteAdapter(sqlite_sql);
142
- await testSqliteRun(sqlite_sql);
143
- await testSqliteParam(sqlite_sql);
144
-
145
- } catch (error) {
146
- console.log('❌ mm_sql实例创建失败:', error.message);
147
- }
148
- }
149
-
150
- /**
151
- * 主测试函数
152
- */
153
- async function main() {
154
- console.log('开始调试mm_sql模块错误传播问题...\n');
155
-
156
- await debugMysqlError();
157
- await debugSqliteError();
158
-
159
- console.log('\n调试完成');
160
- }
161
-
162
- // 运行测试
163
- if (require.main === module) {
164
- main().catch(console.error);
165
- }
@@ -1,35 +0,0 @@
1
- const { Sql } = require('../index.js');
2
-
3
- // 创建Sql实例
4
- const sql = new Sql({
5
- db_type: 'sqlite',
6
- dir: './db',
7
- host: 'localhost',
8
- user: 'root',
9
- password: 'Asd159357',
10
- database: 'mm'
11
- });
12
-
13
- console.log('=== 调试tplQuery基本功能 ===');
14
- const param = { name: '张三', age: 25 };
15
- const sql_tpl = { name: "`name` like '%{0}%'", age: '`age` = {0}' };
16
- const result = sql.tplQuery(param, sql_tpl);
17
- const expected = "`name` like '%张三%' AND `age` = 25";
18
-
19
- console.log('参数:', param);
20
- console.log('模板:', sql_tpl);
21
- console.log('实际输出:', result);
22
- console.log('期望输出:', expected);
23
- console.log('是否匹配:', result === expected);
24
-
25
- console.log('\n=== 调试tplBody基本功能 ===');
26
- const param_body = { name: '李四', email: 'lisi@example.com' };
27
- const sql_tpl_body = { name: '`name` = {0}' };
28
- const res_body = sql.tplBody(param_body, sql_tpl_body);
29
- const exp_body = "`name` = '李四' , `email` = 'lisi@example.com'";
30
-
31
- console.log('参数:', param_body);
32
- console.log('模板:', sql_tpl_body);
33
- console.log('实际输出:', res_body);
34
- console.log('期望输出:', exp_body);
35
- console.log('是否匹配:', res_body === exp_body);
@@ -1,45 +0,0 @@
1
- const { Sql } = require('../index.js');
2
-
3
- // 创建Sql实例
4
- const sql = new Sql({
5
- db_type: 'sqlite',
6
- dir: './db',
7
- host: 'localhost',
8
- user: 'root',
9
- password: 'Asd159357',
10
- database: 'mm'
11
- });
12
-
13
- console.log('=== 详细调试tplQuery基本功能 ===');
14
- const param = { name: '张三', age: 25 };
15
- const sql_tpl = { name: "`name` like '%{0}%'", age: '`age` = {0}' };
16
- const result = sql.tplQuery(param, sql_tpl);
17
- const expected = "`name` like '%张三%' AND `age` = 25";
18
-
19
- console.log('实际输出长度:', result.length);
20
- console.log('期望输出长度:', expected.length);
21
- console.log('实际输出字符:');
22
- for (let i = 0; i < result.length; i++) {
23
- console.log(` ${i}: '${result[i]}' (${result.charCodeAt(i)})`);
24
- }
25
- console.log('期望输出字符:');
26
- for (let i = 0; i < expected.length; i++) {
27
- console.log(` ${i}: '${expected[i]}' (${expected.charCodeAt(i)})`);
28
- }
29
-
30
- console.log('\n=== 详细调试tplBody基本功能 ===');
31
- const param_body = { name: '李四', email: 'lisi@example.com' };
32
- const sql_tpl_body = { name: '`name` = {0}' };
33
- const res_body = sql.tplBody(param_body, sql_tpl_body);
34
- const exp_body = "`name` = '李四' , `email` = 'lisi@example.com'";
35
-
36
- console.log('实际输出长度:', res_body.length);
37
- console.log('期望输出长度:', exp_body.length);
38
- console.log('实际输出字符:');
39
- for (let i = 0; i < res_body.length; i++) {
40
- console.log(` ${i}: '${res_body[i]}' (${res_body.charCodeAt(i)})`);
41
- }
42
- console.log('期望输出字符:');
43
- for (let i = 0; i < exp_body.length; i++) {
44
- console.log(` ${i}: '${exp_body[i]}' (${exp_body.charCodeAt(i)})`);
45
- }
@@ -1,54 +0,0 @@
1
- // 模板替换调试
2
-
3
- console.log('=== 详细调试tplBody模板替换 ===');
4
- const param = { name: '李四', email: 'lisi@example.com' };
5
- const sql_tpl = { name: '`name` = {0}' };
6
-
7
- console.log('参数:', param);
8
- console.log('模板:', sql_tpl);
9
-
10
- // 手动模拟模板替换
11
- const template = sql_tpl || {};
12
- const segments = [];
13
-
14
- for (const key in param) {
15
- if (template[key]) {
16
- // 替换占位符{0}为参数值
17
- const seg = template[key].replace(/\{0\}/g, param[key]);
18
- console.log(`模板替换 ${key}: "${template[key]}" -> "${seg}"`);
19
- segments.push(seg);
20
- }
21
- else {
22
- const seg = '`' + key + "` = '" + param[key] + "'";
23
- console.log(`默认处理 ${key}: "${seg}"`);
24
- segments.push(seg);
25
- }
26
- }
27
-
28
- const result = segments.join(' , ');
29
- console.log('最终结果:', result);
30
-
31
- // 期望的结果应该是:`name` = '李四' , `email` = 'lisi@example.com'
32
- // 但实际是:`name` = 李四 , `email` = 'lisi@example.com'
33
- // 问题在于模板替换时没有添加单引号
34
-
35
- console.log('\n=== 修复方案 ===');
36
- // 正确的模板应该包含单引号
37
- const right_tpl = { name: "`name` = '{0}'" };
38
- const right_seg = [];
39
-
40
- for (const key in param) {
41
- if (right_tpl[key]) {
42
- const seg = right_tpl[key].replace(/\{0\}/g, param[key]);
43
- console.log(`正确模板替换 ${key}: "${right_tpl[key]}" -> "${seg}"`);
44
- right_seg.push(seg);
45
- }
46
- else {
47
- const seg = '`' + key + "` = '" + param[key] + "'";
48
- console.log(`默认处理 ${key}: "${seg}"`);
49
- right_seg.push(seg);
50
- }
51
- }
52
-
53
- const right_res = right_seg.join(' , ');
54
- console.log('正确结果:', right_res);