mm_datas 0.0.0-stage → 5.3.1
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/example/custom_demo.js +237 -0
- package/example/model/item_model.json +46 -0
- package/example/model/map_model.json +53 -0
- package/example/model/player_model.json +65 -0
- package/example/model/role_model.json +61 -0
- package/example/model/skill_model.json +60 -0
- package/example/preset_demo.js +44 -0
- package/example/rpg_cache.js +179 -0
- package/example/rpg_crud.js +170 -0
- package/example/rpg_demo.js +61 -0
- package/example/rpg_model/bag_model.json +74 -0
- package/example/rpg_model/character_model.json +165 -0
- package/example/rpg_model/character_skill_model.json +61 -0
- package/example/rpg_model/drop_model.json +103 -0
- package/example/rpg_model/item_model.json +189 -0
- package/example/rpg_model/job_model.json +96 -0
- package/example/rpg_model/level_exp_model.json +111 -0
- package/example/rpg_model/map_model.json +166 -0
- package/example/rpg_model/map_state_model.json +63 -0
- package/example/rpg_model/monster_model.json +115 -0
- package/example/rpg_model/npc_model.json +103 -0
- package/example/rpg_model/quest_model.json +106 -0
- package/example/rpg_model/quest_progress_model.json +61 -0
- package/example/rpg_model/save_model.json +42 -0
- package/example/rpg_model/skill_model.json +124 -0
- package/example/rpg_persistence.js +151 -0
- package/example/rpg_relations.js +196 -0
- package/example/rpg_world.js +149 -0
- package/example/storage_demo.js +194 -0
- package/index.js +122 -0
- package/lib/backend.js +597 -0
- package/lib/errors.js +35 -0
- package/lib/manager.js +717 -0
- package/lib/model.js +540 -0
- package/lib/pool.js +277 -0
- package/lib/relation.js +175 -0
- package/lib/store.js +430 -0
- package/package.json +64 -5
- package/test.js +748 -0
- package/tool/data/index.js +107 -0
- package/tool/data/tool.json +211 -0
- package/README.md +0 -3
package/lib/backend.js
ADDED
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* mm_datas 的外部访问层与持久化布局
|
|
5
|
+
*
|
|
6
|
+
* 约定(v5):
|
|
7
|
+
* - 模型文件统一命名为 <集合名>_model.json;扫描到几个这样的文件,就创建几种集合。
|
|
8
|
+
* - 静态数据(kind:static)默认落盘为 data/<集合名>/000001.json —— 一条记录一个文件,
|
|
9
|
+
* 主键补零到 6 位;每种模型对应 data/ 下的一个目录。
|
|
10
|
+
* - 静态逐文件的文件 I/O 委托 mm_jsondb 的 Store 文件层(v5.1),
|
|
11
|
+
* database:'.' + pad:6 => data/<集合名>/000001.json,行数据字段原样。
|
|
12
|
+
* - 动态数据(kind:dynamic)= mm_cache 缓存 + mm_mongodb 持久;
|
|
13
|
+
* 依赖未安装 / 未连接时自动回退到 data/<集合名>/_rows.json,不阻断业务。
|
|
14
|
+
* - mm_sql 是可选的关系型 / JSONDB 数据源适配器,配置后用于 pull / push 集合
|
|
15
|
+
* (mm_sql 的 way:'jsondb' 后端就是 mm_jsondb)。
|
|
16
|
+
*
|
|
17
|
+
* 四个依赖统一经各自的 mm_admin 实例池工厂获取实例,不自造 new:
|
|
18
|
+
* jsondbAdmin / cacheAdmin / mongodbAdmin / sqlAdmin。
|
|
19
|
+
* scope 按「命名空间 + 用途」隔离(文件层再叠加目录),避免多池复用同一实例。
|
|
20
|
+
*
|
|
21
|
+
* 依赖都是「可选增强」:require 不到、连接失败、或默认未启用时一律降级,
|
|
22
|
+
* mm_datas 的同步 CRUD 永远可用。只有显式的异步入口(connect / flush / restore /
|
|
23
|
+
* cacheGet / cacheList)才会真正调用它们。
|
|
24
|
+
*/
|
|
25
|
+
const fs = require('fs');
|
|
26
|
+
const path = require('path');
|
|
27
|
+
const crypto = require('crypto');
|
|
28
|
+
|
|
29
|
+
function optional(name) {
|
|
30
|
+
try { return require(name); } catch (e) { return null; }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const MODULES = {
|
|
34
|
+
mm_cache: optional('mm_cache'),
|
|
35
|
+
mm_sql: optional('mm_sql'),
|
|
36
|
+
mm_mongodb: optional('mm_mongodb'),
|
|
37
|
+
mm_jsondb: optional('mm_jsondb')
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function probe() {
|
|
41
|
+
return {
|
|
42
|
+
mm_cache: !!MODULES.mm_cache,
|
|
43
|
+
mm_sql: !!MODULES.mm_sql,
|
|
44
|
+
mm_mongodb: !!MODULES.mm_mongodb,
|
|
45
|
+
mm_jsondb: !!MODULES.mm_jsondb
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 主键 → 6 位补零文件名;字符串主键做安全化后同样补零 */
|
|
50
|
+
function padId(value) {
|
|
51
|
+
let s;
|
|
52
|
+
if (typeof value === 'number' && isFinite(value)) {
|
|
53
|
+
s = String(Math.trunc(value));
|
|
54
|
+
} else {
|
|
55
|
+
s = String(value === null || value === undefined ? 'row' : value)
|
|
56
|
+
.replace(/[^a-zA-Z0-9_\-.]/g, '_');
|
|
57
|
+
}
|
|
58
|
+
if (s.length < 6) s = ('000000' + s).slice(-6);
|
|
59
|
+
return s;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function rowFile(value) { return padId(value) + '.json'; }
|
|
63
|
+
|
|
64
|
+
function decode(value) {
|
|
65
|
+
if (typeof value !== 'string') return value;
|
|
66
|
+
try { return JSON.parse(value); } catch (e) { return value; }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function encode(value) { return JSON.stringify(value); }
|
|
70
|
+
|
|
71
|
+
/** 外部数据源可能卡在连接上,给每次调用套一层超时,绝不拖住同步业务 */
|
|
72
|
+
function withTimeout(promise, ms, fallback) {
|
|
73
|
+
return new Promise(function (resolve) {
|
|
74
|
+
let done = false;
|
|
75
|
+
const timer = setTimeout(function () {
|
|
76
|
+
if (!done) { done = true; resolve(fallback); }
|
|
77
|
+
}, ms || 5000);
|
|
78
|
+
Promise.resolve(promise).then(function (v) {
|
|
79
|
+
if (!done) { done = true; clearTimeout(timer); resolve(v); }
|
|
80
|
+
}).catch(function () {
|
|
81
|
+
if (!done) { done = true; clearTimeout(timer); resolve(fallback); }
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 静态数据目录布局:data/<集合名>/000001.json
|
|
88
|
+
* 动态数据回退:data/<集合名>/_rows.json(整表快照)
|
|
89
|
+
*
|
|
90
|
+
* v5.2:文件 I/O 委托给 mm_jsondb 的 Store 文件层。
|
|
91
|
+
* - 不用 JsonDB.table().add() / exec():那层会强行往行里塞 `id` 字段并丢掉模型主键、
|
|
92
|
+
* 还会生成 _meta.json / _index/。
|
|
93
|
+
* - 只用 Store 的 createTableDir / writeRow / readRow / deleteRow / listRowIds /
|
|
94
|
+
* listTables;database:'.' 归一化掉库目录层,pad:6 由 mm_jsondb 自己负责补零,
|
|
95
|
+
* 适配层直接传原始主键即可精确得到 000001.json 且字段原样。
|
|
96
|
+
* - 若 mm_jsondb 是 1.1.x(无 pad 配置),fileFor / flushStatic 会回退到适配层补零,
|
|
97
|
+
* 保证输出格式不变。
|
|
98
|
+
* - 同步 save()/load() 是兼容包装:路径与命名仍取自 Store,写入用 fs 同步 I/O;
|
|
99
|
+
* 异步 flush()/restore() 全量走 mm_jsondb。依赖不可用时整体回退到 fs 同步实现。
|
|
100
|
+
*/
|
|
101
|
+
class JsonDbLayout {
|
|
102
|
+
constructor(dir, namespace) {
|
|
103
|
+
this.dir = dir || path.join(process.cwd(), 'data');
|
|
104
|
+
this.namespace = namespace || 'mm_datas';
|
|
105
|
+
this.mod = MODULES.mm_jsondb;
|
|
106
|
+
this.reason = '';
|
|
107
|
+
this.factory = 'jsondbAdmin';
|
|
108
|
+
this._bind(this.dir);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** 实例池 scope:同一「命名空间 + 目录」复用实例,不同目录不串库 */
|
|
112
|
+
_scope() {
|
|
113
|
+
const key = this.namespace + '|' + this.dir;
|
|
114
|
+
const hash = crypto.createHash('sha1').update(key).digest('hex').slice(0, 16);
|
|
115
|
+
return 'mm_datas:jsondb:' + hash;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
_bind(dir) {
|
|
119
|
+
this.dir = dir || this.dir;
|
|
120
|
+
this.db = null;
|
|
121
|
+
this.store = null;
|
|
122
|
+
if (!this.mod || !this.mod.jsondbAdmin) {
|
|
123
|
+
this.reason = '未安装 mm_jsondb(或缺少 jsondbAdmin)';
|
|
124
|
+
this.backend = 'fs-fallback';
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
try {
|
|
128
|
+
// 经 mm_jsondb 的 mm_admin 实例池取实例(与 cacheAdmin/sqlAdmin/mongodbAdmin 一致);
|
|
129
|
+
// Base 构造已完成 _preset(),getStore() 可直接拿到 Store 实例(无需 open/await)
|
|
130
|
+
const db = this.mod.jsondbAdmin(this._scope(),
|
|
131
|
+
{ dir: this.dir, database: '.', pad: 6 });
|
|
132
|
+
const store = db.getStore();
|
|
133
|
+
if (!store || typeof store._getTableDir !== 'function' || typeof store.writeRow !== 'function') {
|
|
134
|
+
throw new Error('mm_jsondb 未提供 Store 文件层');
|
|
135
|
+
}
|
|
136
|
+
this.db = db;
|
|
137
|
+
this.store = store;
|
|
138
|
+
// 1.2.0+ 支持 pad;老版本没有该配置,由适配层兜底补零
|
|
139
|
+
this.nativePad = typeof store.formatId === 'function' && store._pad > 0;
|
|
140
|
+
this.backend = 'mm_jsondb';
|
|
141
|
+
this.reason = '';
|
|
142
|
+
} catch (e) {
|
|
143
|
+
this.reason = e.message;
|
|
144
|
+
this.backend = 'fs-fallback';
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
collectionDir(name) {
|
|
149
|
+
return this.store ? this.store._getTableDir(String(name)) : path.join(this.dir, String(name));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
fileFor(name, keyValue) {
|
|
153
|
+
if (this.store) {
|
|
154
|
+
// getRowPath 是 Store 的公开方法;pad 已开启时由 mm_jsondb 自己补零
|
|
155
|
+
const id = this.nativePad ? keyValue : padId(keyValue);
|
|
156
|
+
return typeof this.store.getRowPath === 'function'
|
|
157
|
+
? this.store.getRowPath(String(name), id)
|
|
158
|
+
: this.store._getRowPath(String(name), id);
|
|
159
|
+
}
|
|
160
|
+
return path.join(this.collectionDir(name), rowFile(keyValue));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
status() {
|
|
164
|
+
return {
|
|
165
|
+
backend: this.backend, installed: !!this.mod, factory: this.factory,
|
|
166
|
+
scope: this._scope(), dir: this.dir, database: '.',
|
|
167
|
+
pad: this.nativePad ? this.store._pad : 'adapter(6)',
|
|
168
|
+
row_naming: 'pad=6 + .json', reason: this.reason,
|
|
169
|
+
async_api: this.backend === 'mm_jsondb'
|
|
170
|
+
? 'Store.createTableDir/writeRow/readRow/deleteRow/listRowIds/listTables'
|
|
171
|
+
: 'fs 同步回退'
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async open() {
|
|
176
|
+
if (this.store && typeof this.store.init === 'function') {
|
|
177
|
+
await this.store.init();
|
|
178
|
+
}
|
|
179
|
+
return this.status();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** 只清理本模块自己写在该集合目录下的 .json,不递归、不触碰其它文件 */
|
|
183
|
+
_clean(dir) {
|
|
184
|
+
if (!fs.existsSync(dir)) return;
|
|
185
|
+
const files = fs.readdirSync(dir, { withFileTypes: true });
|
|
186
|
+
for (let i = 0; i < files.length; i++) {
|
|
187
|
+
if (files[i].isFile() && /\.json$/i.test(files[i].name)) {
|
|
188
|
+
fs.unlinkSync(path.join(dir, files[i].name));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
_rowId(row, key, index) { return row[key] === undefined ? index + 1 : row[key]; }
|
|
194
|
+
|
|
195
|
+
// ---------- 同步兼容包装:save() / load() ----------
|
|
196
|
+
|
|
197
|
+
saveStatic(name, key, rows) {
|
|
198
|
+
const dir = this.collectionDir(name);
|
|
199
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
200
|
+
this._clean(dir);
|
|
201
|
+
for (let i = 0; i < rows.length; i++) {
|
|
202
|
+
fs.writeFileSync(this.fileFor(name, this._rowId(rows[i], key, i)),
|
|
203
|
+
JSON.stringify(rows[i], null, 2), 'utf8');
|
|
204
|
+
}
|
|
205
|
+
return rows.length;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
saveDynamic(name, rows) {
|
|
209
|
+
const dir = this.collectionDir(name);
|
|
210
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
211
|
+
fs.writeFileSync(path.join(dir, '_rows.json'), JSON.stringify(rows, null, 2), 'utf8');
|
|
212
|
+
return 1;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
load(setTable, dir) {
|
|
216
|
+
if (dir) this._bind(dir);
|
|
217
|
+
if (!fs.existsSync(this.dir)) {
|
|
218
|
+
return { ok: false, reason: '目录不存在', backend: 'hybrid', dir: this.dir, table_names: [], collections: [] };
|
|
219
|
+
}
|
|
220
|
+
const names = fs.readdirSync(this.dir, { withFileTypes: true })
|
|
221
|
+
.filter(function (e) { return e.isDirectory() && e.name.charAt(0) !== '_'; })
|
|
222
|
+
.map(function (e) { return e.name; }).sort();
|
|
223
|
+
const info = [];
|
|
224
|
+
for (let i = 0; i < names.length; i++) {
|
|
225
|
+
const name = names[i];
|
|
226
|
+
const dirName = this.collectionDir(name);
|
|
227
|
+
const snapshot = path.join(dirName, '_rows.json');
|
|
228
|
+
let rows;
|
|
229
|
+
let kind;
|
|
230
|
+
if (fs.existsSync(snapshot)) {
|
|
231
|
+
rows = JSON.parse(fs.readFileSync(snapshot, 'utf8'));
|
|
232
|
+
kind = 'dynamic';
|
|
233
|
+
} else {
|
|
234
|
+
const files = fs.readdirSync(dirName)
|
|
235
|
+
.filter(function (f) { return /\.json$/i.test(f) && f !== '_rows.json' && f !== '_meta.json'; }).sort();
|
|
236
|
+
rows = [];
|
|
237
|
+
for (let j = 0; j < files.length; j++) {
|
|
238
|
+
rows.push(JSON.parse(fs.readFileSync(path.join(dirName, files[j]), 'utf8')));
|
|
239
|
+
}
|
|
240
|
+
kind = 'static';
|
|
241
|
+
}
|
|
242
|
+
setTable(name, rows);
|
|
243
|
+
info.push({ name: name, kind: kind, rows: rows.length });
|
|
244
|
+
}
|
|
245
|
+
return {
|
|
246
|
+
ok: true, backend: 'hybrid', dir: this.dir,
|
|
247
|
+
table_names: names, collections: info
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ---------- 异步主路径:flush() / restore() 全量走 mm_jsondb ----------
|
|
252
|
+
|
|
253
|
+
async flushStatic(name, key, rows) {
|
|
254
|
+
const s = this.store;
|
|
255
|
+
if (!s) return this.saveStatic(name, key, rows);
|
|
256
|
+
await s.createTableDir(name);
|
|
257
|
+
const ids = await s.listRowIds(name);
|
|
258
|
+
for (let i = 0; i < ids.length; i++) {
|
|
259
|
+
if (ids[i] !== '_meta' && ids[i] !== '_rows') await s.deleteRow(name, ids[i]);
|
|
260
|
+
}
|
|
261
|
+
for (let i = 0; i < rows.length; i++) {
|
|
262
|
+
const rawId = this._rowId(rows[i], key, i);
|
|
263
|
+
await s.writeRow(name, this.nativePad ? rawId : padId(rawId), rows[i]);
|
|
264
|
+
}
|
|
265
|
+
return rows.length;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async flushDynamic(name, rows) {
|
|
269
|
+
const s = this.store;
|
|
270
|
+
if (!s) return this.saveDynamic(name, rows);
|
|
271
|
+
await s.createTableDir(name);
|
|
272
|
+
await s.writeRow(name, '_rows', rows); // → <集合>/_rows.json
|
|
273
|
+
return 1;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async restore(setTable, dir) {
|
|
277
|
+
if (dir) this._bind(dir);
|
|
278
|
+
if (!fs.existsSync(this.dir)) {
|
|
279
|
+
return { ok: false, reason: '目录不存在', backend: 'hybrid', dir: this.dir, table_names: [], collections: [] };
|
|
280
|
+
}
|
|
281
|
+
const s = this.store;
|
|
282
|
+
if (!s) return this.load(setTable, dir);
|
|
283
|
+
const names = (await s.listTables()).slice().sort();
|
|
284
|
+
const info = [];
|
|
285
|
+
for (let i = 0; i < names.length; i++) {
|
|
286
|
+
const name = names[i];
|
|
287
|
+
const ids = (await s.listRowIds(name)).filter(function (id) { return id !== '_meta'; });
|
|
288
|
+
let rows;
|
|
289
|
+
let kind;
|
|
290
|
+
if (ids.indexOf('_rows') !== -1) {
|
|
291
|
+
rows = await s.readRow(name, '_rows');
|
|
292
|
+
kind = 'dynamic';
|
|
293
|
+
} else {
|
|
294
|
+
rows = [];
|
|
295
|
+
const rowIds = ids.filter(function (id) { return id !== '_rows'; });
|
|
296
|
+
for (let j = 0; j < rowIds.length; j++) {
|
|
297
|
+
const row = await s.readRow(name, rowIds[j]);
|
|
298
|
+
if (row) rows.push(row);
|
|
299
|
+
}
|
|
300
|
+
kind = 'static';
|
|
301
|
+
}
|
|
302
|
+
if (!Array.isArray(rows)) rows = [];
|
|
303
|
+
setTable(name, rows);
|
|
304
|
+
info.push({ name: name, kind: kind, rows: rows.length });
|
|
305
|
+
}
|
|
306
|
+
return {
|
|
307
|
+
ok: true, backend: 'hybrid', dir: this.dir,
|
|
308
|
+
table_names: names, collections: info
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// 保持对外名称不变(index.js 与旧调用方都用 RowFileLayout)
|
|
314
|
+
const RowFileLayout = JsonDbLayout;
|
|
315
|
+
|
|
316
|
+
/** mm_cache 适配器:热数据 / 查询结果缓存,默认 memory 方式,离线可用 */
|
|
317
|
+
class CacheAdapter {
|
|
318
|
+
constructor(config) {
|
|
319
|
+
this.name = 'mm_cache';
|
|
320
|
+
this.config = Object.assign({ scope: 'mm_datas', way: 'memory', ttl: 0 }, config || {});
|
|
321
|
+
this.mod = MODULES.mm_cache;
|
|
322
|
+
this.installed = !!this.mod;
|
|
323
|
+
this.enabled = !!(config && config.enabled !== false);
|
|
324
|
+
this.connected = false;
|
|
325
|
+
this.reason = this.installed ? '' : '未安装 mm_cache';
|
|
326
|
+
this.client = null;
|
|
327
|
+
this.fallback = new Map();
|
|
328
|
+
this.stats = { hit: 0, miss: 0, set: 0, del: 0 };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async open() {
|
|
332
|
+
if (!this.enabled) { this.reason = '已禁用'; return this.status(); }
|
|
333
|
+
if (!this.mod) return this.status();
|
|
334
|
+
try {
|
|
335
|
+
this.client = this.mod.cacheAdmin(this.config.scope, this.config);
|
|
336
|
+
const ok = await this.client.connect();
|
|
337
|
+
this.connected = ok !== false;
|
|
338
|
+
if (!this.connected) this.reason = 'connect 返回 false';
|
|
339
|
+
} catch (e) {
|
|
340
|
+
this.connected = false;
|
|
341
|
+
this.reason = e.message;
|
|
342
|
+
}
|
|
343
|
+
return this.status();
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
status() {
|
|
347
|
+
return {
|
|
348
|
+
name: 'mm_cache', installed: this.installed, enabled: this.enabled,
|
|
349
|
+
factory: 'cacheAdmin', scope: this.config.scope,
|
|
350
|
+
connected: this.connected, way: this.config.way, reason: this.reason,
|
|
351
|
+
fallback: this.connected ? false : 'Map(进程内)', stats: this.stats
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
async get(key) {
|
|
356
|
+
if (this.connected) {
|
|
357
|
+
try {
|
|
358
|
+
const value = await this.client.get(key);
|
|
359
|
+
if (value !== null && value !== undefined) {
|
|
360
|
+
this.stats.hit++;
|
|
361
|
+
return decode(value);
|
|
362
|
+
}
|
|
363
|
+
} catch (e) { /* 落到回退 */ }
|
|
364
|
+
}
|
|
365
|
+
if (this.fallback.has(key)) { this.stats.hit++; return this.fallback.get(key); }
|
|
366
|
+
this.stats.miss++;
|
|
367
|
+
return null;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
async set(key, value, ttl) {
|
|
371
|
+
const seconds = ttl === undefined ? this.config.ttl : ttl;
|
|
372
|
+
if (this.connected) {
|
|
373
|
+
try { await this.client.set(key, encode(value), seconds); }
|
|
374
|
+
catch (e) { this.fallback.set(key, value); }
|
|
375
|
+
} else {
|
|
376
|
+
this.fallback.set(key, value);
|
|
377
|
+
}
|
|
378
|
+
this.stats.set++;
|
|
379
|
+
return true;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
async del(key) {
|
|
383
|
+
if (this.connected) {
|
|
384
|
+
try { await this.client.del(key); } catch (e) { /* ignore */ }
|
|
385
|
+
}
|
|
386
|
+
this.fallback.delete(key);
|
|
387
|
+
this.stats.del++;
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
async clearPrefix(prefix) {
|
|
392
|
+
let n = 0;
|
|
393
|
+
for (const key of Array.from(this.fallback.keys())) {
|
|
394
|
+
if (key.indexOf(prefix) === 0) { this.fallback.delete(key); n++; }
|
|
395
|
+
}
|
|
396
|
+
if (this.connected) {
|
|
397
|
+
try {
|
|
398
|
+
const keys = await this.client.keys(prefix + '*');
|
|
399
|
+
const list = Array.isArray(keys) ? keys : [];
|
|
400
|
+
for (let i = 0; i < list.length; i++) {
|
|
401
|
+
await this.client.del(list[i]);
|
|
402
|
+
n++;
|
|
403
|
+
}
|
|
404
|
+
} catch (e) { /* 内存回退已清 */ }
|
|
405
|
+
}
|
|
406
|
+
return n;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** mm_mongodb 适配器:动态数据的持久层,按集合存整表快照(key-value 接口) */
|
|
411
|
+
class MongoAdapter {
|
|
412
|
+
constructor(config) {
|
|
413
|
+
this.name = 'mm_mongodb';
|
|
414
|
+
this.config = Object.assign({
|
|
415
|
+
scope: 'mm_datas', host: '127.0.0.1', port: 27017,
|
|
416
|
+
database: 'mm_datas', table: 'mm_datas_rows'
|
|
417
|
+
}, config || {});
|
|
418
|
+
this.mod = MODULES.mm_mongodb;
|
|
419
|
+
this.installed = !!this.mod;
|
|
420
|
+
// 需要真实 MongoDB 服务,默认不启用;启用后连不上会自动回退本地文件
|
|
421
|
+
this.enabled = !!(config && config.enabled);
|
|
422
|
+
this.connected = false;
|
|
423
|
+
this.reason = !this.enabled ? '未启用(动态数据回退本地文件)' : (this.installed ? '' : '未安装 mm_mongodb');
|
|
424
|
+
// 允许注入实现 set/get 的客户端(测试 / 无服务演示 / 其它 Mongo 封装)
|
|
425
|
+
this.factory = 'mongodbAdmin';
|
|
426
|
+
if (config && config.client && typeof config.client.set === 'function') {
|
|
427
|
+
this.client = config.client;
|
|
428
|
+
this.connected = true;
|
|
429
|
+
this.enabled = true;
|
|
430
|
+
this.reason = '注入客户端';
|
|
431
|
+
this.factory = 'injected-client';
|
|
432
|
+
} else {
|
|
433
|
+
this.client = null;
|
|
434
|
+
}
|
|
435
|
+
this.stats = { push: 0, pull: 0 };
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
async open() {
|
|
439
|
+
if (this.connected) return this.status();
|
|
440
|
+
if (!this.enabled || !this.mod) return this.status();
|
|
441
|
+
const opened = await withTimeout((async function () {
|
|
442
|
+
const client = this.mod.mongodbAdmin(this.config.scope, this.config);
|
|
443
|
+
await client.open();
|
|
444
|
+
return { ok: true, client: client };
|
|
445
|
+
}).call(this), 6000, { ok: false, reason: 'mm_mongodb 连接超时' });
|
|
446
|
+
if (opened.ok) {
|
|
447
|
+
this.client = opened.client;
|
|
448
|
+
this.connected = true;
|
|
449
|
+
} else {
|
|
450
|
+
this.connected = false;
|
|
451
|
+
this.reason = opened.reason;
|
|
452
|
+
}
|
|
453
|
+
return this.status();
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
status() {
|
|
457
|
+
return {
|
|
458
|
+
name: 'mm_mongodb', installed: this.installed, enabled: this.enabled,
|
|
459
|
+
factory: this.factory, scope: this.config.scope,
|
|
460
|
+
connected: this.connected, reason: this.reason, stats: this.stats
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
_key(name) { return 'rows:' + this.config.database + ':' + name; }
|
|
465
|
+
|
|
466
|
+
async push(name, rows) {
|
|
467
|
+
if (!this.connected) return { ok: false, reason: this.reason || '未连接' };
|
|
468
|
+
const out = await withTimeout(
|
|
469
|
+
this.client.set(this._key(name), encode(rows)), 6000,
|
|
470
|
+
{ ok: false, reason: 'mm_mongodb 写入超时' });
|
|
471
|
+
if (out && out.ok === false) return out;
|
|
472
|
+
this.stats.push++;
|
|
473
|
+
return { ok: true, rows: rows.length };
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
async pull(name) {
|
|
477
|
+
if (!this.connected) return null;
|
|
478
|
+
const value = await withTimeout(this.client.get(this._key(name)), 6000, null);
|
|
479
|
+
if (value === null || value === undefined) return null;
|
|
480
|
+
this.stats.pull++;
|
|
481
|
+
return decode(value);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** mm_sql 适配器:可选的关系型 / JSON 数据源;集合以 (id, body) 两张通用列落地 */
|
|
486
|
+
class SqlAdapter {
|
|
487
|
+
constructor(config) {
|
|
488
|
+
this.name = 'mm_sql';
|
|
489
|
+
this.config = Object.assign({
|
|
490
|
+
db_type: 'sqlite',
|
|
491
|
+
sqlite: { dir: path.join(process.cwd(), 'data', '_sql'), database: 'mm_datas' }
|
|
492
|
+
}, config || {});
|
|
493
|
+
this.mod = MODULES.mm_sql;
|
|
494
|
+
this.installed = !!this.mod;
|
|
495
|
+
this.enabled = !!(config && config.enabled);
|
|
496
|
+
this.connected = false;
|
|
497
|
+
this.reason = !this.enabled ? '未启用(可选数据源)' : (this.installed ? '' : '未安装 mm_sql');
|
|
498
|
+
this.client = null;
|
|
499
|
+
this.factory = 'sqlAdmin';
|
|
500
|
+
this.stats = { push: 0, pull: 0 };
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
async open() {
|
|
504
|
+
if (!this.enabled || !this.mod) return this.status();
|
|
505
|
+
try {
|
|
506
|
+
// 统一经 mm_admin 实例池:sqlAdmin(scope, config) 与其余三个依赖一致;
|
|
507
|
+
// scope 由 createAdapters 按命名空间隔离,池配置不会跨实例串用。
|
|
508
|
+
this.client = this.mod.sqlAdmin(this.config.scope, this.config);
|
|
509
|
+
this.connected = true;
|
|
510
|
+
} catch (e) {
|
|
511
|
+
this.connected = false;
|
|
512
|
+
this.reason = e.message;
|
|
513
|
+
}
|
|
514
|
+
return this.status();
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
status() {
|
|
518
|
+
return {
|
|
519
|
+
name: 'mm_sql', installed: this.installed, enabled: this.enabled,
|
|
520
|
+
factory: this.factory, scope: this.config.scope,
|
|
521
|
+
connected: this.connected,
|
|
522
|
+
way: this.config.way || this.config.db_type, db_type: this.config.db_type,
|
|
523
|
+
reason: this.reason, stats: this.stats
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
_table(name) { return 'd_' + String(name).replace(/[^a-zA-Z0-9_]/g, '_'); }
|
|
528
|
+
|
|
529
|
+
async push(name, key, rows) {
|
|
530
|
+
if (!this.connected) return { ok: false, reason: this.reason || '未连接' };
|
|
531
|
+
const table = this._table(name);
|
|
532
|
+
const client = this.client;
|
|
533
|
+
const job = (async function () {
|
|
534
|
+
await client.exec('CREATE TABLE IF NOT EXISTS ' + table + ' (id TEXT PRIMARY KEY, body TEXT)');
|
|
535
|
+
await client.exec('DELETE FROM ' + table);
|
|
536
|
+
for (let i = 0; i < rows.length; i++) {
|
|
537
|
+
const id = String(rows[i][key] === undefined ? i + 1 : rows[i][key]);
|
|
538
|
+
await client.exec('INSERT INTO ' + table + ' (id, body) VALUES (?, ?)',
|
|
539
|
+
[id, encode(rows[i])]);
|
|
540
|
+
}
|
|
541
|
+
return { ok: true, table: table, rows: rows.length };
|
|
542
|
+
})();
|
|
543
|
+
const out = await withTimeout(job, 8000, { ok: false, reason: 'mm_sql 超时' });
|
|
544
|
+
if (out.ok) this.stats.push++;
|
|
545
|
+
else this.reason = out.reason;
|
|
546
|
+
return out;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
async pull(name) {
|
|
550
|
+
if (!this.connected) return null;
|
|
551
|
+
const rows = await withTimeout(
|
|
552
|
+
this.client.run('SELECT body FROM ' + this._table(name)), 8000, null);
|
|
553
|
+
if (!Array.isArray(rows)) return null;
|
|
554
|
+
this.stats.pull++;
|
|
555
|
+
return rows.map(function (r) { return decode(r.body !== undefined ? r.body : r); });
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
async query(sql, params) {
|
|
559
|
+
if (!this.connected) return null;
|
|
560
|
+
return withTimeout(this.client.run(sql, params || []), 8000, null);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function createAdapters(config) {
|
|
565
|
+
config = config || {};
|
|
566
|
+
// 每个实例池用独立 scope:mm_admin 会按 scope 复用实例,
|
|
567
|
+
// 否则多个池共用同一个 Cache 客户端,connect() 里的 dispose() 会互相影响
|
|
568
|
+
const ns = 'mm_datas:' + (config.namespace || 'default');
|
|
569
|
+
return {
|
|
570
|
+
cache: new CacheAdapter(Object.assign({ scope: ns }, config.cache || {})),
|
|
571
|
+
mongo: new MongoAdapter(Object.assign({ scope: ns }, config.mongodb || config.mangodb || {})),
|
|
572
|
+
sql: new SqlAdapter(Object.assign({ scope: ns }, config.sql || {})),
|
|
573
|
+
probe: probe
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
async function openAll(adapters) {
|
|
578
|
+
return {
|
|
579
|
+
cache: await adapters.cache.open(),
|
|
580
|
+
mongodb: await adapters.mongo.open(),
|
|
581
|
+
sql: await adapters.sql.open()
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
module.exports = {
|
|
586
|
+
MODULES: MODULES,
|
|
587
|
+
probe: probe,
|
|
588
|
+
padId: padId,
|
|
589
|
+
rowFile: rowFile,
|
|
590
|
+
RowFileLayout: RowFileLayout,
|
|
591
|
+
JsonDbLayout: JsonDbLayout,
|
|
592
|
+
CacheAdapter: CacheAdapter,
|
|
593
|
+
MongoAdapter: MongoAdapter,
|
|
594
|
+
SqlAdapter: SqlAdapter,
|
|
595
|
+
createAdapters: createAdapters,
|
|
596
|
+
openAll: openAll
|
|
597
|
+
};
|
package/lib/errors.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 数据错误:核心 API 抛异常,工具适配层捕获后转成 { error: { code, message } }
|
|
5
|
+
*/
|
|
6
|
+
class DataError extends Error {
|
|
7
|
+
constructor(code, message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = 'DataError';
|
|
10
|
+
this.code = code;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function raise(code, message) {
|
|
15
|
+
throw new DataError(code, message);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const CODES = {
|
|
19
|
+
MODEL_NOT_FOUND: '模型未注册',
|
|
20
|
+
MODEL_DUPLICATE: '模型重复定义',
|
|
21
|
+
SCHEMA_VALIDATION: '数据校验失败',
|
|
22
|
+
REF_MISSING: '关联目标不存在',
|
|
23
|
+
REF_UNKNOWN: '关联目标模型未注册',
|
|
24
|
+
DUPLICATE_KEY: '主键重复',
|
|
25
|
+
QUERY_REQUIRED: '缺少查询条件',
|
|
26
|
+
READONLY: '静态表只读',
|
|
27
|
+
UNKNOWN_ACTION: '不支持的操作',
|
|
28
|
+
STORE_ERROR: '存储执行失败',
|
|
29
|
+
SHARD_KEY_MISSING: '缺少分片键',
|
|
30
|
+
VALIDATOR_MISSING: '校验器 mm_validator 不可用',
|
|
31
|
+
VALIDATOR_ERROR: '校验器执行失败',
|
|
32
|
+
SCHEMA_TYPE_UNSUPPORTED: 'schema 使用了不支持的类型'
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
module.exports = { DataError, raise, CODES };
|