mm_datas 5.3.0 → 5.3.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/lib/backend.js CHANGED
@@ -1,597 +1,63 @@
1
1
  'use strict';
2
2
 
3
3
  /**
4
- * mm_data 的外部访问层与持久化布局
4
+ * mm_datas 的外部访问层聚合
5
5
  *
6
- * 约定(v5):
6
+ * 约定(v5.3,依赖显式):
7
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)。
8
+ * - 静态数据(kind:static)落盘为 data/<集合名>/000001.json —— 一条记录一个文件,
9
+ * 主键由 mm_jsondb 的 Store 按 pad:6 补零;行数据字段原样。
10
+ * - 动态数据(kind:dynamic)= mm_cache 缓存 + mm_mongodb 持久 + 本地文件快照;
11
+ * mm_sql 为可选的关系型 / JSONDB 数据源适配器,配置后用于 pull / push 集合。
12
+ * - 四个依赖都是硬依赖,模块加载时即解析;连接或调用失败一律抛错,不做静默回退。
13
+ * 未启用的数据源由上层(HybridStore)显式跳过,不视为失败。
16
14
  *
17
15
  * 四个依赖统一经各自的 mm_admin 实例池工厂获取实例,不自造 new:
18
16
  * jsondbAdmin / cacheAdmin / mongodbAdmin / sqlAdmin。
19
17
  * scope 按「命名空间 + 用途」隔离(文件层再叠加目录),避免多池复用同一实例。
20
- *
21
- * 依赖都是「可选增强」:require 不到、连接失败、或默认未启用时一律降级,
22
- * mm_data 的同步 CRUD 永远可用。只有显式的异步入口(connect / flush / restore /
23
- * cacheGet / cacheList)才会真正调用它们。
24
18
  */
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
19
 
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
- }
20
+ const JsonDbLayout = require('./json_db_layout.js');
21
+ const CacheAdapter = require('./cache_adapter.js');
22
+ const MongoAdapter = require('./mongo_adapter.js');
23
+ const SqlAdapter = require('./sql_adapter.js');
85
24
 
86
25
  /**
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 同步实现。
26
+ * 按命名空间创建三个适配器实例
27
+ * @param {object} [config] 配置(namespace/cache/mongodb/sql)
28
+ * @returns {object} 适配器集合 { cache, mongo, sql }
100
29
  */
101
- class JsonDbLayout {
102
- constructor(dir, namespace) {
103
- this.dir = dir || path.join(process.cwd(), 'data');
104
- this.namespace = namespace || 'mm_data';
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_data: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_data', 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_data', host: '127.0.0.1', port: 27017,
416
- database: 'mm_data', table: 'mm_data_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_data' }
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
30
  function createAdapters(config) {
565
31
  config = config || {};
566
32
  // 每个实例池用独立 scope:mm_admin 会按 scope 复用实例,
567
33
  // 否则多个池共用同一个 Cache 客户端,connect() 里的 dispose() 会互相影响
568
- const ns = 'mm_data:' + (config.namespace || 'default');
34
+ let ns = 'mm_datas:' + (config.namespace || 'default');
569
35
  return {
570
36
  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
37
+ mongo: new MongoAdapter(Object.assign({ scope: ns }, config.mongodb || {})),
38
+ sql: new SqlAdapter(Object.assign({ scope: ns }, config.sql || {}))
574
39
  };
575
40
  }
576
41
 
42
+ /**
43
+ * 并行打开全部适配器
44
+ * @param {object} adapters 适配器集合
45
+ * @returns {Promise<object>} 各适配器状态
46
+ */
577
47
  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
- };
48
+ let [cache, mongodb, sql] = await Promise.all([
49
+ adapters.cache.open(),
50
+ adapters.mongo.open(),
51
+ adapters.sql.open()
52
+ ]);
53
+ return { cache, mongodb, sql };
583
54
  }
584
55
 
585
56
  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
57
+ JsonDbLayout,
58
+ CacheAdapter,
59
+ MongoAdapter,
60
+ SqlAdapter,
61
+ createAdapters,
62
+ openAll
597
63
  };
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 编解码与超时工具
5
+ * mm_datas 外部数据源调用的公共助手。
6
+ */
7
+
8
+ /**
9
+ * 解码 JSON 字符串;非字符串原样返回
10
+ * @param {*} value 待解码值
11
+ * @returns {*} 解码结果
12
+ * @throws {SyntaxError} 字符串不是合法 JSON 时抛出
13
+ */
14
+ function decode(value) {
15
+ if (typeof value !== 'string') return value;
16
+ return JSON.parse(value);
17
+ }
18
+
19
+ /**
20
+ * 编码为 JSON 字符串
21
+ * @param {*} value 待编码值
22
+ * @returns {string} JSON 字符串
23
+ */
24
+ function encode(value) { return JSON.stringify(value); }
25
+
26
+ /**
27
+ * 给外部数据源调用套一层超时;超时或失败都直接抛错
28
+ * @param {Promise} promise 外部数据源返回的 Promise
29
+ * @param {number} [ms] 超时毫秒数,缺省 5000
30
+ * @param {string} [label] 超时提示标签
31
+ * @returns {Promise} 带超时的 Promise
32
+ * @throws {Error} 超时时抛出
33
+ */
34
+ function withTimeout(promise, ms, label) {
35
+ let timer;
36
+ let timeout = new Promise(function (resolve, reject) {
37
+ timer = setTimeout(function () { reject(new Error(label + ' 超时')); }, ms || 5000);
38
+ });
39
+ return Promise.race([Promise.resolve(promise), timeout]).finally(function () { clearTimeout(timer); });
40
+ }
41
+
42
+ module.exports = { decode, encode, withTimeout };