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/example/custom_demo.js +18 -18
- package/example/preset_demo.js +10 -10
- package/example/rpg_cache.js +39 -39
- package/example/rpg_crud.js +19 -19
- package/example/rpg_demo.js +5 -5
- package/example/rpg_persistence.js +39 -39
- package/example/rpg_relations.js +28 -28
- package/example/rpg_world.js +7 -7
- package/example/storage_demo.js +33 -35
- package/index.js +9 -57
- package/lib/backend.js +35 -569
- package/lib/backend_codec.js +42 -0
- package/lib/cache_adapter.js +112 -0
- package/lib/data_error.js +19 -0
- package/lib/errors.js +11 -10
- package/lib/hybrid_store.js +225 -0
- package/lib/json_db_layout.js +266 -0
- package/lib/json_file_store.js +66 -0
- package/lib/manager.js +236 -509
- package/lib/manager_define.js +131 -0
- package/lib/manager_ext.js +412 -0
- package/lib/manager_helper.js +97 -0
- package/lib/manager_store.js +196 -0
- package/lib/memory_store.js +203 -0
- package/lib/model.js +114 -288
- package/lib/model_check.js +184 -0
- package/lib/model_validator.js +104 -0
- package/lib/mongo_adapter.js +106 -0
- package/lib/pool.js +61 -45
- package/lib/relation.js +81 -55
- package/lib/sql_adapter.js +116 -0
- package/lib/store.js +16 -409
- package/lib/store_query.js +119 -0
- package/lib/tool_scan.js +106 -0
- package/package.json +4 -5
- package/test.js +25 -46
- package/tool/data/index.js +19 -8
- package/tool/data/tool.json +2 -2
- package/tool/data_admin/index.js +38 -0
- package/tool/data_admin/tool.json +42 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const runner = require('./model_validator.js');
|
|
4
|
+
const M = require('./model.js');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 校验执行层
|
|
8
|
+
* 标准关键词 → mm_validator 规则翻译与递归校验;规则判定全部由 mm_validator 工具执行。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 三候选取值
|
|
13
|
+
* @param {*} a 候选 a
|
|
14
|
+
* @param {*} b 候选 b
|
|
15
|
+
* @param {*} c 候选 c
|
|
16
|
+
* @returns {*} 第一个非 undefined 的值
|
|
17
|
+
*/
|
|
18
|
+
function pick(a, b, c) {
|
|
19
|
+
if (a !== undefined) return a;
|
|
20
|
+
if (b !== undefined) return b;
|
|
21
|
+
return c;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 拼接校验错误路径
|
|
26
|
+
* @param {string} prefix 前缀
|
|
27
|
+
* @param {string} seg 片段
|
|
28
|
+
* @returns {string} 完整路径
|
|
29
|
+
*/
|
|
30
|
+
function joinPath(prefix, seg) {
|
|
31
|
+
return prefix ? prefix + '.' + seg : seg;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 标准关键词 → mm_validator/tool/params 的单字段 rules */
|
|
35
|
+
function ruleOf(def, value) {
|
|
36
|
+
let r = {};
|
|
37
|
+
if (!def || typeof def !== 'object') return r;
|
|
38
|
+
if (def.required === true) r.required = true;
|
|
39
|
+
let t = Array.isArray(def.type) ? def.type[0] : def.type;
|
|
40
|
+
// null 无法由 params 的 typeof 判定(typeof null === 'object'),故不生成类型规则
|
|
41
|
+
if (t && t !== 'null') r.type = t;
|
|
42
|
+
let en = def.enum || (def.const !== undefined ? [def.const] : null);
|
|
43
|
+
if (en) r.enum = en;
|
|
44
|
+
if (def.pattern) r.pattern = def.pattern;
|
|
45
|
+
|
|
46
|
+
if (def.minimum !== undefined) r.min = def.minimum;
|
|
47
|
+
if (def.maximum !== undefined) r.max = def.maximum;
|
|
48
|
+
// exclusive* 按非严格边界处理(mm_validator 无 exclusive 语义)
|
|
49
|
+
if (def.exclusiveMinimum !== undefined && r.min === undefined) r.min = def.exclusiveMinimum;
|
|
50
|
+
if (def.exclusiveMaximum !== undefined && r.max === undefined) r.max = def.exclusiveMaximum;
|
|
51
|
+
|
|
52
|
+
let minLen = pick(def.minLength, def.minItems, def.min_length);
|
|
53
|
+
let maxLen = pick(def.maxLength, def.maxItems, def.max_length);
|
|
54
|
+
if (minLen !== undefined) r.min_length = minLen;
|
|
55
|
+
if (maxLen !== undefined) r.max_length = maxLen;
|
|
56
|
+
return r;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** params 会跳过空串,改用 mm_validator 的单项工具补检,避免约束被静默弱化 */
|
|
60
|
+
function checkEmpty(fullPath, field, value, def, errs) {
|
|
61
|
+
let minLen = pick(def.minLength, def.minItems, def.min_length);
|
|
62
|
+
let maxLen = pick(def.maxLength, def.maxItems, def.max_length);
|
|
63
|
+
if (minLen !== undefined || maxLen !== undefined) {
|
|
64
|
+
let out = runner.run('length', { field: field, value: value, min: minLen, max: maxLen });
|
|
65
|
+
if (out && out.errors) for (let i = 0; i < out.errors.length; i++) errs.push(fullPath + ': ' + String(out.errors[i]));
|
|
66
|
+
}
|
|
67
|
+
let en = def.enum || (def.const !== undefined ? [def.const] : null);
|
|
68
|
+
if (en) {
|
|
69
|
+
let out = runner.run('enum', { field: field, value: value, options: en });
|
|
70
|
+
if (out && out.errors) for (let i = 0; i < out.errors.length; i++) errs.push(fullPath + ': ' + String(out.errors[i]));
|
|
71
|
+
}
|
|
72
|
+
if (def.pattern) {
|
|
73
|
+
let out = runner.run('pattern', { field: field, value: value, pattern: def.pattern });
|
|
74
|
+
if (out && out.errors) for (let i = 0; i < out.errors.length; i++) errs.push(fullPath + ': ' + String(out.errors[i]));
|
|
75
|
+
}
|
|
76
|
+
let t = Array.isArray(def.type) ? def.type[0] : def.type;
|
|
77
|
+
if (t && t !== 'null') {
|
|
78
|
+
let out = runner.run('type', { field: field, value: value, expected_type: t });
|
|
79
|
+
if (out && out.errors) for (let i = 0; i < out.errors.length; i++) errs.push(fullPath + ': ' + String(out.errors[i]));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 收集 params 批量校验的错误到统一错误列表
|
|
85
|
+
* @param {object} out params 输出
|
|
86
|
+
* @param {string} prefix 路径前缀
|
|
87
|
+
* @param {Array} errs 错误列表
|
|
88
|
+
* @private
|
|
89
|
+
*/
|
|
90
|
+
function collectParamErrors(out, prefix, errs) {
|
|
91
|
+
if (!out || !out.errors) return;
|
|
92
|
+
for (let f in out.errors) {
|
|
93
|
+
let list = out.errors[f] || [];
|
|
94
|
+
for (let i = 0; i < list.length; i++) {
|
|
95
|
+
errs.push(joinPath(prefix, f) + ': ' + String(list[i]));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 一层 object 的校验:字段清单比对 + 一次 params 批量调用 + 嵌套递归 */
|
|
101
|
+
function validateLevel(fields, obj, prefix, errs, noExtra) {
|
|
102
|
+
if (!obj || typeof obj !== 'object' || Array.isArray(obj)) return;
|
|
103
|
+
|
|
104
|
+
if (noExtra) {
|
|
105
|
+
for (let k in obj) {
|
|
106
|
+
if (!Object.prototype.hasOwnProperty.call(fields, k)) {
|
|
107
|
+
errs.push(joinPath(prefix, k) + ': 未在 schema 中声明(additionalProperties: false)');
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let rules = {};
|
|
113
|
+
let empty_fields = [];
|
|
114
|
+
for (let f in fields) {
|
|
115
|
+
let r = ruleOf(fields[f], obj[f]);
|
|
116
|
+
if (Object.keys(r).length) rules[f] = r;
|
|
117
|
+
if (obj[f] === '') empty_fields.push(f);
|
|
118
|
+
}
|
|
119
|
+
if (Object.keys(rules).length) {
|
|
120
|
+
collectParamErrors(runner.run('params', { input: obj, rules: rules }), prefix, errs);
|
|
121
|
+
}
|
|
122
|
+
for (let i = 0; i < empty_fields.length; i++) {
|
|
123
|
+
let f = empty_fields[i];
|
|
124
|
+
checkEmpty(joinPath(prefix, f), f, obj[f], fields[f], errs);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
for (let f in fields) {
|
|
128
|
+
let def = fields[f];
|
|
129
|
+
let v = obj[f];
|
|
130
|
+
if (v === undefined || v === null) continue;
|
|
131
|
+
let p = joinPath(prefix, f);
|
|
132
|
+
if (def.properties && typeof v === 'object' && !Array.isArray(v)) {
|
|
133
|
+
validateLevel(M.childFields(def), v, p, errs, def.additionalProperties === false);
|
|
134
|
+
}
|
|
135
|
+
if (def.items && Array.isArray(v)) {
|
|
136
|
+
for (let i = 0; i < v.length; i++) {
|
|
137
|
+
let elem = v[i];
|
|
138
|
+
let ep = p + '[' + i + ']';
|
|
139
|
+
let idef = def.items;
|
|
140
|
+
if (idef.properties && elem && typeof elem === 'object' && !Array.isArray(elem)) {
|
|
141
|
+
validateLevel(M.childFields(idef), elem, ep, errs, idef.additionalProperties === false);
|
|
142
|
+
} else {
|
|
143
|
+
let r = ruleOf(idef, elem);
|
|
144
|
+
if (Object.keys(r).length) {
|
|
145
|
+
let out = runner.run('params', { input: { _: elem }, rules: { _: r } });
|
|
146
|
+
if (out && out.errors && out.errors._) {
|
|
147
|
+
for (let j = 0; j < out.errors._.length; j++) {
|
|
148
|
+
errs.push(ep + ': ' + String(out.errors._[j]).replace(/^_\s*/, ''));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (elem === '') checkEmpty(ep, '_', elem, idef, errs);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 递归校验数据体(供 model.check 调用)
|
|
161
|
+
* @param {object} model 模型定义
|
|
162
|
+
* @param {object} body 数据体
|
|
163
|
+
* @returns {Array|null} 错误数组或 null
|
|
164
|
+
* @private
|
|
165
|
+
*/
|
|
166
|
+
function validate(model, body) {
|
|
167
|
+
if (!model || model.schemaless || !model.json_schema) return null;
|
|
168
|
+
let errs = [];
|
|
169
|
+
validateLevel(model.schema, body || {}, '', errs, model.additionalProperties === false);
|
|
170
|
+
return errs.length ? errs : null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 只校验、不写入
|
|
175
|
+
* @param {object} model 模型定义
|
|
176
|
+
* @param {object} row 数据行
|
|
177
|
+
* @returns {object} 校验结果 { ok, errors }
|
|
178
|
+
*/
|
|
179
|
+
function check(model, row) {
|
|
180
|
+
let errors = validate(model, row) || [];
|
|
181
|
+
return { ok: errors.length === 0, errors: errors };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
module.exports = { validate, check };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const { raise } = require('./errors.js');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* mm_validator 调用层
|
|
8
|
+
* 负责 mm_validator 的延迟加载、工具缓存与调用统计。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const ALLOWED_TYPES = ['string', 'number', 'boolean', 'object', 'array', 'null'];
|
|
12
|
+
const DRAFT = 'http://json-schema.org/draft-07/schema#';
|
|
13
|
+
|
|
14
|
+
// mm_validator 缺失时延迟报错:让 schema 编译等不依赖校验器的路径先行可用
|
|
15
|
+
let V = null;
|
|
16
|
+
let v_error = null;
|
|
17
|
+
try {
|
|
18
|
+
V = require('mm_validator');
|
|
19
|
+
} catch (e) {
|
|
20
|
+
v_error = e.message;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 已加载的校验器工具缓存与调用统计(可变对象,非常量)
|
|
24
|
+
let tool_cache = {};
|
|
25
|
+
let call_stats = { total: 0, by_tool: {} };
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 取一个 mm_validator 工具(带缓存与存在性校验)
|
|
29
|
+
* @param {string} name 工具名
|
|
30
|
+
* @returns {object} 工具模块
|
|
31
|
+
* @throws {Error} mm_validator 不可用或工具缺失时抛出
|
|
32
|
+
*/
|
|
33
|
+
function tool(name) {
|
|
34
|
+
if (!V) {
|
|
35
|
+
raise('VALIDATOR_MISSING', 'schema 校验依赖 mm_validator:模块未安装或无法解析' +
|
|
36
|
+
(v_error ? '(' + v_error + ')' : ''));
|
|
37
|
+
}
|
|
38
|
+
if (!tool_cache[name]) {
|
|
39
|
+
try {
|
|
40
|
+
tool_cache[name] = require(path.join(V.TOOL_DIR, name));
|
|
41
|
+
} catch (e) {
|
|
42
|
+
raise('VALIDATOR_MISSING', '无法加载 mm_validator/tool/' + name + ':' + e.message);
|
|
43
|
+
}
|
|
44
|
+
if (!tool_cache[name] || typeof tool_cache[name].main !== 'function') {
|
|
45
|
+
raise('VALIDATOR_MISSING', 'mm_validator/tool/' + name + ' 未导出 main');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return tool_cache[name];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 调用一个 mm_validator 工具;工具当前同步写 ctx.output,若未来异步化则明确报错而非放行
|
|
53
|
+
* @param {string} name 工具名
|
|
54
|
+
* @param {object} params 工具参数
|
|
55
|
+
* @returns {object} 校验结果
|
|
56
|
+
* @throws {Error} 工具执行失败或未同步返回时抛出
|
|
57
|
+
*/
|
|
58
|
+
function run(name, params) {
|
|
59
|
+
let ctx = { output: null };
|
|
60
|
+
let ret = null;
|
|
61
|
+
try {
|
|
62
|
+
ret = tool(name).main(params, ctx, {});
|
|
63
|
+
} catch (e) {
|
|
64
|
+
raise('VALIDATOR_ERROR', 'mm_validator/' + name + ' 执行失败:' + e.message);
|
|
65
|
+
}
|
|
66
|
+
call_stats.total++;
|
|
67
|
+
call_stats.by_tool[name] = (call_stats.by_tool[name] || 0) + 1;
|
|
68
|
+
// 异步返回统一在这里拒绝,避免 unhandled rejection;主流程由下方「未同步返回」报错
|
|
69
|
+
if (ret && typeof ret.then === 'function') ret.catch(function () {});
|
|
70
|
+
if (!ctx.output) {
|
|
71
|
+
raise('VALIDATOR_ERROR', 'mm_validator/' + name + ' 未同步返回校验结果(工具已异步化,需同步适配)');
|
|
72
|
+
}
|
|
73
|
+
return ctx.output;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 当前生效的校验器信息
|
|
78
|
+
* @returns {object} 校验器信息
|
|
79
|
+
*/
|
|
80
|
+
function validatorInfo() {
|
|
81
|
+
let version = null;
|
|
82
|
+
if (V) {
|
|
83
|
+
let pkg_path = path.join(V.TOOL_DIR, '..', 'package.json');
|
|
84
|
+
if (require('fs').existsSync(pkg_path)) {
|
|
85
|
+
version = require(pkg_path).version;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
package: 'mm_validator',
|
|
90
|
+
version: version,
|
|
91
|
+
loaded: !!V,
|
|
92
|
+
tool_dir: V ? V.TOOL_DIR : null,
|
|
93
|
+
available_tools: V && typeof V.listTools === 'function' ? V.listTools() : [],
|
|
94
|
+
engine_tools: Object.keys(call_stats.by_tool),
|
|
95
|
+
schema_draft: DRAFT,
|
|
96
|
+
allowed_types: ALLOWED_TYPES.slice(),
|
|
97
|
+
forbidden_types: ['int', 'integer', 'any', 'float', 'long', 'double', 'bigint'],
|
|
98
|
+
calls: call_stats.total,
|
|
99
|
+
calls_by_tool: Object.assign({}, call_stats.by_tool),
|
|
100
|
+
error: v_error
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
module.exports = { tool, run, validatorInfo };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { mongodbAdmin } = require('mm_mongodb');
|
|
4
|
+
const { decode, encode, withTimeout } = require('./backend_codec.js');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* mm_mongodb 适配器:动态数据的持久层,按集合存整表快照(key-value 接口)
|
|
8
|
+
*/
|
|
9
|
+
class MongoAdapter {
|
|
10
|
+
/**
|
|
11
|
+
* 构造函数
|
|
12
|
+
* @param {object} [config] 适配器配置(scope/host/port/database/table/enabled/client)
|
|
13
|
+
* @throws {Error} 注入客户端未实现 set/get 时抛出
|
|
14
|
+
*/
|
|
15
|
+
constructor(config) {
|
|
16
|
+
this.name = 'mm_mongodb';
|
|
17
|
+
this.config = Object.assign({
|
|
18
|
+
scope: 'mm_datas', host: '127.0.0.1', port: 27017,
|
|
19
|
+
database: 'mm_datas', table: 'mm_datas_rows'
|
|
20
|
+
}, config || {});
|
|
21
|
+
// 需要真实 MongoDB 服务,默认不启用;启用后连不上直接抛错
|
|
22
|
+
this.enabled = !!(config && config.enabled);
|
|
23
|
+
this.connected = false;
|
|
24
|
+
this.reason = this.enabled ? '' : '未启用';
|
|
25
|
+
this.factory = 'mongodbAdmin';
|
|
26
|
+
// 调用方可显式注入实现了 set/get 的客户端(数据源由调用方负责,不是兜底实现)
|
|
27
|
+
if (config && config.client) {
|
|
28
|
+
if (typeof config.client.set !== 'function' || typeof config.client.get !== 'function') {
|
|
29
|
+
throw new Error('注入的 mongodb 客户端必须实现 set/get');
|
|
30
|
+
}
|
|
31
|
+
this.client = config.client;
|
|
32
|
+
this.connected = true;
|
|
33
|
+
this.enabled = true;
|
|
34
|
+
this.reason = '注入客户端';
|
|
35
|
+
this.factory = 'injected-client';
|
|
36
|
+
} else {
|
|
37
|
+
this.client = null;
|
|
38
|
+
}
|
|
39
|
+
this.stats = { push: 0, pull: 0 };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 连接 mongodb 客户端
|
|
44
|
+
* @returns {Promise<object>} 状态信息
|
|
45
|
+
*/
|
|
46
|
+
async open() {
|
|
47
|
+
if (this.connected || !this.enabled) return this.status();
|
|
48
|
+
let client = mongodbAdmin(this.config.scope, this.config);
|
|
49
|
+
await withTimeout(client.open(), 6000, 'mm_mongodb 连接');
|
|
50
|
+
this.client = client;
|
|
51
|
+
this.connected = true;
|
|
52
|
+
return this.status();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 获取状态信息
|
|
57
|
+
* @returns {object} 状态信息
|
|
58
|
+
*/
|
|
59
|
+
status() {
|
|
60
|
+
return {
|
|
61
|
+
name: this.name, factory: this.factory, scope: this.config.scope,
|
|
62
|
+
enabled: this.enabled, connected: this.connected,
|
|
63
|
+
reason: this.reason, stats: this.stats
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 取集合存储键
|
|
69
|
+
* @param {string} name 集合名
|
|
70
|
+
* @returns {string} 存储键
|
|
71
|
+
* @private
|
|
72
|
+
*/
|
|
73
|
+
_key(name) { return 'rows:' + this.config.database + ':' + name; }
|
|
74
|
+
|
|
75
|
+
/** 未连接即报错 */
|
|
76
|
+
_client() {
|
|
77
|
+
if (!this.connected) throw new Error('mm_mongodb 未连接');
|
|
78
|
+
return this.client;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 推送集合快照
|
|
83
|
+
* @param {string} name 集合名
|
|
84
|
+
* @param {Array} rows 记录列表
|
|
85
|
+
* @returns {Promise<object>} 推送结果
|
|
86
|
+
*/
|
|
87
|
+
async push(name, rows) {
|
|
88
|
+
await withTimeout(this._client().set(this._key(name), encode(rows)), 6000, 'mm_mongodb 写入');
|
|
89
|
+
this.stats.push++;
|
|
90
|
+
return { ok: true, rows: rows.length };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 拉取集合快照
|
|
95
|
+
* @param {string} name 集合名
|
|
96
|
+
* @returns {Promise<Array|null>} 记录列表,无数据为 null
|
|
97
|
+
*/
|
|
98
|
+
async pull(name) {
|
|
99
|
+
let value = await withTimeout(this._client().get(this._key(name)), 6000, 'mm_mongodb 读取');
|
|
100
|
+
if (value === null || value === undefined) return null;
|
|
101
|
+
this.stats.pull++;
|
|
102
|
+
return decode(value);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
module.exports = MongoAdapter;
|
package/lib/pool.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* 基于 mm_admin 的实例池封装
|
|
5
5
|
*
|
|
6
6
|
* 范式与 mm_dao / mm_store 一致:
|
|
7
|
-
* const pool = reg('
|
|
7
|
+
* const pool = reg('mm_datas'); // 全局注册表里唯一的管理器池
|
|
8
8
|
* pool.setClass(DataManager); // 绑定本模块的管理数据类
|
|
9
9
|
* pool.setServer(sharedStore); // 注入共享存储(所有集合共用)
|
|
10
10
|
* pool.get('role', config) // 按名字缓存 DataManager 实例
|
|
@@ -14,23 +14,31 @@
|
|
|
14
14
|
const fs = require('fs');
|
|
15
15
|
const path = require('path');
|
|
16
16
|
const S = require('./store.js');
|
|
17
|
-
const B = require('./backend.js');
|
|
18
17
|
const mm = require('mm_admin');
|
|
19
|
-
const
|
|
18
|
+
const DataManager = require('./manager.js');
|
|
19
|
+
const { EXAMPLE_MODEL_DIR } = require('./manager_helper.js');
|
|
20
20
|
|
|
21
|
-
const DEFAULT_NAMESPACE = '
|
|
21
|
+
const DEFAULT_NAMESPACE = 'mm_datas';
|
|
22
22
|
let _current = null;
|
|
23
23
|
let _resetSeq = 0;
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* 创建存储实例:优先使用注入的自定义 adapter,
|
|
27
|
+
* 配置了 hybrid/rowfile/auto 或模型目录时按 v5 目录布局落盘,否则按指定方式创建
|
|
28
|
+
* @param {object} config - 存储配置项,可含 store、models、dir 等
|
|
29
|
+
* @param {string} namespace - 存储命名空间(未显式指定时作为 config.namespace)
|
|
30
|
+
* @returns {object} 存储实例(含共享的 models 与 seq 表)
|
|
31
|
+
*/
|
|
25
32
|
function makeStore(config, namespace) {
|
|
26
|
-
|
|
33
|
+
let cfg = Object.assign({}, config || {}, { namespace: (config && config.namespace) || namespace });
|
|
27
34
|
let store;
|
|
28
35
|
if (cfg.store && typeof cfg.store === 'object' && typeof cfg.store.list === 'function') {
|
|
29
36
|
store = cfg.store; // 自定义 adapter(MySQL / Redis / S5 ...)
|
|
30
37
|
} else if (cfg.store === 'hybrid' || cfg.store === 'rowfile' || cfg.store === 'auto' ||
|
|
31
38
|
(!cfg.store && (cfg.models || cfg.dir))) {
|
|
32
39
|
// 装载了 *_model.json(或显式给了数据目录)时,默认按 v5 目录布局落盘:
|
|
33
|
-
// 静态 data/<name>/000001.json;动态 mm_cache +
|
|
40
|
+
// 静态 data/<name>/000001.json;动态 mm_cache + 本地 _rows.json 快照
|
|
41
|
+
// (mm_mongodb / mm_sql 只在显式启用时再同步一份)
|
|
34
42
|
store = S.createStore('hybrid', cfg);
|
|
35
43
|
} else {
|
|
36
44
|
store = S.createStore(cfg.store || 'memory', cfg);
|
|
@@ -40,16 +48,15 @@ function makeStore(config, namespace) {
|
|
|
40
48
|
return store;
|
|
41
49
|
}
|
|
42
50
|
|
|
43
|
-
/**
|
|
51
|
+
/** 扫描模型目录:只识别 <集合名>_model.json */
|
|
44
52
|
function scanModelFiles(dir) {
|
|
45
|
-
|
|
53
|
+
let target = dir || EXAMPLE_MODEL_DIR;
|
|
46
54
|
if (!fs.existsSync(target)) return [];
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
});
|
|
55
|
+
return fs.readdirSync(target)
|
|
56
|
+
.filter(function (f) { return /_model\.json$/i.test(f); }).sort()
|
|
57
|
+
.map(function (f) {
|
|
58
|
+
return { file: f, path: path.join(target, f), name: path.basename(f, '.json').replace(/_model$/i, '') };
|
|
59
|
+
});
|
|
53
60
|
}
|
|
54
61
|
|
|
55
62
|
function clearStore(store) {
|
|
@@ -57,30 +64,30 @@ function clearStore(store) {
|
|
|
57
64
|
if (store.models) store.models.clear();
|
|
58
65
|
if (store.seq) store.seq.clear();
|
|
59
66
|
if (typeof store.tableNames === 'function') {
|
|
60
|
-
|
|
67
|
+
let names = store.tableNames();
|
|
61
68
|
for (let i = 0; i < names.length; i++) store.clear(names[i]);
|
|
62
69
|
}
|
|
63
70
|
}
|
|
64
71
|
|
|
65
72
|
/** 借一个临时实例执行批量装载,避免把引导集合留在实例池里 */
|
|
66
73
|
function loadModels(pool, dir) {
|
|
67
|
-
|
|
68
|
-
// 装载期间暂停自动落盘:seed 会触发
|
|
69
|
-
|
|
70
|
-
if (
|
|
74
|
+
let store = pool.getServer();
|
|
75
|
+
// 装载期间暂停自动落盘:seed 会触发 auto_save,否则引导集合会被写成一个空 .json
|
|
76
|
+
let auto_save = store.auto_save;
|
|
77
|
+
if (auto_save) store.auto_save = false;
|
|
71
78
|
try {
|
|
72
|
-
|
|
79
|
+
let boot = new DataManager(
|
|
73
80
|
{ name: '__bootstrap__', namespace: pool.config.namespace || DEFAULT_NAMESPACE },
|
|
74
81
|
store
|
|
75
82
|
);
|
|
76
|
-
|
|
83
|
+
let names = boot.loadModels(dir);
|
|
77
84
|
store.models.delete('__bootstrap__');
|
|
78
85
|
// 引导集合必须整表移除:只 clear 会留下空表,被 save() 写成 __bootstrap__.json
|
|
79
86
|
if (typeof store.dropTable === 'function') store.dropTable('__bootstrap__');
|
|
80
87
|
else if (typeof store.clear === 'function') store.clear('__bootstrap__');
|
|
81
88
|
return names;
|
|
82
89
|
} finally {
|
|
83
|
-
if (
|
|
90
|
+
if (auto_save) store.auto_save = auto_save;
|
|
84
91
|
}
|
|
85
92
|
}
|
|
86
93
|
|
|
@@ -89,23 +96,23 @@ function poolData(pool, name, def) {
|
|
|
89
96
|
if (!name || typeof name !== 'string') {
|
|
90
97
|
throw new Error('集合名必须是非空字符串');
|
|
91
98
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
99
|
+
let ns = pool.config.namespace || DEFAULT_NAMESPACE;
|
|
100
|
+
let existed = pool.has(name);
|
|
101
|
+
let cfg = { namespace: ns };
|
|
95
102
|
if (def !== undefined) cfg.def = def;
|
|
96
|
-
|
|
103
|
+
let inst = pool.get(name, cfg);
|
|
97
104
|
if (existed && def !== undefined) inst.define(def);
|
|
98
105
|
return inst;
|
|
99
106
|
}
|
|
100
107
|
|
|
101
108
|
/**
|
|
102
109
|
* 创建(或取得)一个具名实例池。不同 namespace 之间数据完全隔离。
|
|
103
|
-
* @param {string} [namespace] 池名,缺省 '
|
|
104
|
-
* @param {object} [config] { store, dir,
|
|
110
|
+
* @param {string} [namespace] 池名,缺省 'mm_datas'
|
|
111
|
+
* @param {object} [config] { store, dir, auto_save, persist, flush_interval, models }
|
|
105
112
|
*/
|
|
106
113
|
function createPool(namespace, config) {
|
|
107
|
-
|
|
108
|
-
|
|
114
|
+
let ns = namespace || DEFAULT_NAMESPACE;
|
|
115
|
+
let pool = mm.reg(ns);
|
|
109
116
|
if (!pool.getClass()) pool.setClass(DataManager);
|
|
110
117
|
if (!pool.getServer()) pool.setServer(makeStore(config, ns));
|
|
111
118
|
if (!pool.config.namespace) pool.setConfig({ namespace: ns });
|
|
@@ -122,12 +129,12 @@ function createPool(namespace, config) {
|
|
|
122
129
|
if (!pool.flushNow) pool.flushNow = function () { return flushNow(pool); };
|
|
123
130
|
if (!pool.close) pool.close = function () { return closePool(pool); };
|
|
124
131
|
if (!pool.startAutoFlush) pool.startAutoFlush = function (interval) {
|
|
125
|
-
|
|
132
|
+
let store = pool.getServer();
|
|
126
133
|
if (store && typeof store.startAutoFlush === 'function') store.startAutoFlush(interval);
|
|
127
134
|
return pool;
|
|
128
135
|
};
|
|
129
136
|
if (!pool.stopAutoFlush) pool.stopAutoFlush = function () {
|
|
130
|
-
|
|
137
|
+
let store = pool.getServer();
|
|
131
138
|
if (store && typeof store.stopAutoFlush === 'function') store.stopAutoFlush();
|
|
132
139
|
return pool;
|
|
133
140
|
};
|
|
@@ -142,7 +149,7 @@ function createPool(namespace, config) {
|
|
|
142
149
|
* @returns {object} 落盘结果
|
|
143
150
|
*/
|
|
144
151
|
function flushNow(pool) {
|
|
145
|
-
|
|
152
|
+
let store = pool.getServer();
|
|
146
153
|
if (!store) return { ok: true, skipped: true, reason: '池未绑定 store' };
|
|
147
154
|
return typeof store.saveNow === 'function'
|
|
148
155
|
? store.saveNow()
|
|
@@ -155,7 +162,7 @@ function flushNow(pool) {
|
|
|
155
162
|
* @returns {object} 落盘结果
|
|
156
163
|
*/
|
|
157
164
|
function closePool(pool) {
|
|
158
|
-
|
|
165
|
+
let store = pool.getServer();
|
|
159
166
|
if (!store) return { ok: true, skipped: true, reason: '池未绑定 store' };
|
|
160
167
|
return typeof store.close === 'function' ? store.close() : flushNow(pool);
|
|
161
168
|
}
|
|
@@ -166,21 +173,31 @@ function closePool(pool) {
|
|
|
166
173
|
* @returns {object} 状态信息
|
|
167
174
|
*/
|
|
168
175
|
function persistStatus(pool) {
|
|
169
|
-
|
|
176
|
+
let store = pool.getServer();
|
|
170
177
|
if (!store) return { backend: '', persist: false, dirty: false };
|
|
171
178
|
return typeof store.persistStatus === 'function'
|
|
172
179
|
? store.persistStatus()
|
|
173
180
|
: { backend: store.constructor.name, persist: false, dirty: false };
|
|
174
181
|
}
|
|
175
182
|
|
|
183
|
+
/**
|
|
184
|
+
* 取池的服务端存储实例(外部访问层入口)
|
|
185
|
+
* @param {object} pool 实例池
|
|
186
|
+
* @returns {object} 存储实例
|
|
187
|
+
* @throws {Error} 纯内存 store 时抛出(需用 createPool 开启外部访问层)
|
|
188
|
+
*/
|
|
176
189
|
function storeOf(pool) {
|
|
177
|
-
|
|
190
|
+
let s = pool.getServer();
|
|
178
191
|
if (!s || typeof s.connect !== 'function') {
|
|
179
192
|
throw new Error('当前池是纯内存 store,请用 createPool(ns, { store:"hybrid", dir, models }) 开启外部访问层');
|
|
180
193
|
}
|
|
181
194
|
return s;
|
|
182
195
|
}
|
|
183
196
|
|
|
197
|
+
/**
|
|
198
|
+
* 取当前生效的默认池(mm_datas.DEFAULT_NAMESPACE),不存在时创建
|
|
199
|
+
* @returns {object} 默认实例池
|
|
200
|
+
*/
|
|
184
201
|
function currentPool() {
|
|
185
202
|
if (!_current) _current = createPool(DEFAULT_NAMESPACE);
|
|
186
203
|
return _current;
|
|
@@ -202,11 +219,11 @@ function dataAdmin(name, def, config) {
|
|
|
202
219
|
return currentPool();
|
|
203
220
|
}
|
|
204
221
|
if (typeof name === 'object' && !Array.isArray(name)) {
|
|
205
|
-
|
|
222
|
+
let pool = createPool(name.namespace || name.name, name);
|
|
206
223
|
_current = pool;
|
|
207
224
|
return pool;
|
|
208
225
|
}
|
|
209
|
-
|
|
226
|
+
let pool = config ? createPool(config.namespace || DEFAULT_NAMESPACE, config) : currentPool();
|
|
210
227
|
return poolData(pool, name, def);
|
|
211
228
|
}
|
|
212
229
|
|
|
@@ -216,7 +233,7 @@ dataAdmin.reset = function (config) {
|
|
|
216
233
|
if (config && (config.store || config.dir || config.namespace)) {
|
|
217
234
|
_current = createPool(config.namespace || (DEFAULT_NAMESPACE + ':r' + (++_resetSeq)), config);
|
|
218
235
|
} else {
|
|
219
|
-
|
|
236
|
+
let pool = currentPool();
|
|
220
237
|
pool.clear();
|
|
221
238
|
clearStore(pool.getServer());
|
|
222
239
|
}
|
|
@@ -226,7 +243,6 @@ dataAdmin.loadModels = function (dir) { return loadModels(currentPool(), dir ||
|
|
|
226
243
|
dataAdmin.modelFiles = function (dir) { return scanModelFiles(dir || EXAMPLE_MODEL_DIR); };
|
|
227
244
|
dataAdmin.createPool = createPool;
|
|
228
245
|
dataAdmin.class = DataManager;
|
|
229
|
-
dataAdmin.probe = B.probe;
|
|
230
246
|
dataAdmin.connect = async function (pool) { return storeOf(pool || currentPool()).connect(); };
|
|
231
247
|
dataAdmin.flush = async function (pool) { return storeOf(pool || currentPool()).flush(); };
|
|
232
248
|
dataAdmin.flushNow = function (pool) { return flushNow(pool || currentPool()); };
|
|
@@ -234,8 +250,8 @@ dataAdmin.close = function (pool) { return closePool(pool || currentPool()); };
|
|
|
234
250
|
dataAdmin.persistStatus = function (pool) { return persistStatus(pool || currentPool()); };
|
|
235
251
|
dataAdmin.restore = async function (dir, pool) { return storeOf(pool || currentPool()).restore(dir); };
|
|
236
252
|
dataAdmin.backends = function (pool) {
|
|
237
|
-
|
|
238
|
-
return typeof s.backends === 'function' ? s.backends() : { backend: s.constructor.name
|
|
253
|
+
let s = (pool || currentPool()).getServer();
|
|
254
|
+
return typeof s.backends === 'function' ? s.backends() : { backend: s.constructor.name };
|
|
239
255
|
};
|
|
240
256
|
|
|
241
257
|
/**
|
|
@@ -243,9 +259,9 @@ dataAdmin.backends = function (pool) {
|
|
|
243
259
|
* 用于 verify / stats / save / load / dump / loadModels 这类全局操作。
|
|
244
260
|
*/
|
|
245
261
|
function registry(pool) {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
262
|
+
let p = pool || currentPool();
|
|
263
|
+
let store = p.getServer();
|
|
264
|
+
let inst = Object.create(DataManager.prototype);
|
|
249
265
|
inst.config = {};
|
|
250
266
|
inst.name = '__registry__';
|
|
251
267
|
inst.server = store;
|