mm_datas 0.0.0-stage → 5.3.0
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/store.js
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 可插拔存储层:内存 / JSON 文件
|
|
5
|
+
* 生产环境可替换成 S5 适配器(实现同一组方法即可,world 不感知实现)
|
|
6
|
+
*/
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const path = require('path');
|
|
9
|
+
const { clone, project } = require('./model.js');
|
|
10
|
+
const B = require('./backend.js');
|
|
11
|
+
const Persist = require('mm_persist').Persist;
|
|
12
|
+
|
|
13
|
+
function compare(a, b) {
|
|
14
|
+
if (a === b) return 0;
|
|
15
|
+
if (a === undefined || a === null) return -1;
|
|
16
|
+
if (b === undefined || b === null) return 1;
|
|
17
|
+
if (typeof a === 'number' && typeof b === 'number') return a - b;
|
|
18
|
+
return String(a) < String(b) ? -1 : 1;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isOperatorObject(cond) {
|
|
22
|
+
if (!cond || typeof cond !== 'object' || Array.isArray(cond)) return false;
|
|
23
|
+
const keys = Object.keys(cond);
|
|
24
|
+
return keys.length > 0 && keys.every(function (k) { return k.charAt(0) === '$'; });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function matchValue(actual, cond) {
|
|
28
|
+
if (isOperatorObject(cond)) {
|
|
29
|
+
for (const op in cond) {
|
|
30
|
+
const expected = cond[op];
|
|
31
|
+
switch (op) {
|
|
32
|
+
case '$eq': if (actual !== expected) return false; break;
|
|
33
|
+
case '$ne': if (actual === expected) return false; break;
|
|
34
|
+
case '$gt': if (!(actual > expected)) return false; break;
|
|
35
|
+
case '$gte': if (!(actual >= expected)) return false; break;
|
|
36
|
+
case '$lt': if (!(actual < expected)) return false; break;
|
|
37
|
+
case '$lte': if (!(actual <= expected)) return false; break;
|
|
38
|
+
case '$in':
|
|
39
|
+
if (!Array.isArray(expected) || expected.indexOf(actual) === -1) return false;
|
|
40
|
+
break;
|
|
41
|
+
case '$nin':
|
|
42
|
+
if (Array.isArray(expected) && expected.indexOf(actual) !== -1) return false;
|
|
43
|
+
break;
|
|
44
|
+
case '$like':
|
|
45
|
+
if (typeof actual !== 'string' || actual.indexOf(String(expected)) === -1) return false;
|
|
46
|
+
break;
|
|
47
|
+
case '$contains':
|
|
48
|
+
if (!Array.isArray(actual) || actual.indexOf(expected) === -1) return false;
|
|
49
|
+
break;
|
|
50
|
+
default: return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
if (Array.isArray(cond)) return cond.indexOf(actual) !== -1;
|
|
56
|
+
if (cond && typeof cond === 'object') {
|
|
57
|
+
return JSON.stringify(actual) === JSON.stringify(cond);
|
|
58
|
+
}
|
|
59
|
+
return actual === cond;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function match(row, query) {
|
|
63
|
+
if (!query || typeof query !== 'object') return true;
|
|
64
|
+
for (const k in query) {
|
|
65
|
+
if (k === '$or') {
|
|
66
|
+
const list = query[k] || [];
|
|
67
|
+
let hit = false;
|
|
68
|
+
for (let i = 0; i < list.length; i++) if (match(row, list[i])) { hit = true; break; }
|
|
69
|
+
if (!hit) return false;
|
|
70
|
+
} else if (k === '$and') {
|
|
71
|
+
const list = query[k] || [];
|
|
72
|
+
for (let i = 0; i < list.length; i++) if (!match(row, list[i])) return false;
|
|
73
|
+
} else if (!matchValue(row[k], query[k])) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function parseOrder(order) {
|
|
81
|
+
if (!order) return null;
|
|
82
|
+
if (Array.isArray(order)) {
|
|
83
|
+
return { field: order[0], desc: /desc/i.test(String(order[1] || '')) };
|
|
84
|
+
}
|
|
85
|
+
if (typeof order === 'object') {
|
|
86
|
+
return {
|
|
87
|
+
field: order.field || order.by,
|
|
88
|
+
desc: !!(order.desc || /desc/i.test(String(order.order || '')))
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
const parts = String(order).trim().split(/\s+/);
|
|
92
|
+
return { field: parts[0], desc: /desc/i.test(parts[1] || '') };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
class MemoryStore extends Persist {
|
|
96
|
+
constructor() {
|
|
97
|
+
super({ persist: false });
|
|
98
|
+
this.tables = new Map();
|
|
99
|
+
this.calls = { list: 0, get: 0, insert: 0, update: 0, remove: 0 };
|
|
100
|
+
this.backend = 'memory';
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
table(name) {
|
|
104
|
+
if (!this.tables.has(name)) this.tables.set(name, []);
|
|
105
|
+
return this.tables.get(name);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
tableNames() { return Array.from(this.tables.keys()); }
|
|
109
|
+
|
|
110
|
+
setTable(name, rows) { this.tables.set(name, rows || []); }
|
|
111
|
+
|
|
112
|
+
clear(name) { this.tables.set(name, []); }
|
|
113
|
+
|
|
114
|
+
/** 彻底移除一张表(clear 会留下空表,落盘时会被写成空 .json) */
|
|
115
|
+
dropTable(name) { this.tables.delete(name); }
|
|
116
|
+
|
|
117
|
+
getRows(name) { return this.tables.get(name) || []; }
|
|
118
|
+
|
|
119
|
+
insert(name, row) {
|
|
120
|
+
this.calls.insert++;
|
|
121
|
+
this.table(name).push(clone(row));
|
|
122
|
+
return row;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
update(name, query, updater) {
|
|
126
|
+
this.calls.update++;
|
|
127
|
+
const rows = this.table(name);
|
|
128
|
+
let n = 0;
|
|
129
|
+
for (let i = 0; i < rows.length; i++) {
|
|
130
|
+
if (match(rows[i], query)) { updater(rows[i]); n++; }
|
|
131
|
+
}
|
|
132
|
+
return n;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
remove(name, query) {
|
|
136
|
+
this.calls.remove++;
|
|
137
|
+
const rows = this.table(name);
|
|
138
|
+
const kept = [];
|
|
139
|
+
let n = 0;
|
|
140
|
+
for (let i = 0; i < rows.length; i++) {
|
|
141
|
+
if (match(rows[i], query)) n++;
|
|
142
|
+
else kept.push(rows[i]);
|
|
143
|
+
}
|
|
144
|
+
this.tables.set(name, kept);
|
|
145
|
+
return n;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
count(name, query) {
|
|
149
|
+
const rows = this.getRows(name);
|
|
150
|
+
let n = 0;
|
|
151
|
+
for (let i = 0; i < rows.length; i++) if (match(rows[i], query)) n++;
|
|
152
|
+
return n;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
exists(name, query) {
|
|
156
|
+
const rows = this.getRows(name);
|
|
157
|
+
for (let i = 0; i < rows.length; i++) if (match(rows[i], query)) return true;
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
get(name, query, key) {
|
|
162
|
+
this.calls.get++;
|
|
163
|
+
const rows = this.getRows(name);
|
|
164
|
+
const q = (typeof query !== 'object' || query === null) ? (function () { const o = {}; o[key] = query; return o; })() : query;
|
|
165
|
+
for (let i = 0; i < rows.length; i++) if (match(rows[i], q)) return clone(rows[i]);
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
list(name, query, options, key) {
|
|
170
|
+
this.calls.list++;
|
|
171
|
+
options = options || {};
|
|
172
|
+
query = query || {};
|
|
173
|
+
if (typeof query !== 'object' || Array.isArray(query)) {
|
|
174
|
+
const q = {};
|
|
175
|
+
q[key] = query;
|
|
176
|
+
query = q;
|
|
177
|
+
}
|
|
178
|
+
let rows = this.getRows(name).filter(function (r) { return match(r, query); });
|
|
179
|
+
const ord = parseOrder(options.order);
|
|
180
|
+
if (ord && ord.field) {
|
|
181
|
+
rows = rows.slice().sort(function (a, b) {
|
|
182
|
+
const c = compare(a[ord.field], b[ord.field]);
|
|
183
|
+
return ord.desc ? -c : c;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
const total = rows.length;
|
|
187
|
+
let start = 0;
|
|
188
|
+
if (options.cursor !== undefined && options.cursor !== null && key) {
|
|
189
|
+
const idx = rows.findIndex(function (r) { return String(r[key]) === String(options.cursor); });
|
|
190
|
+
if (idx >= 0) start = idx + 1;
|
|
191
|
+
} else if (options.offset) {
|
|
192
|
+
start = Math.max(0, parseInt(options.offset, 10) || 0);
|
|
193
|
+
}
|
|
194
|
+
let limit = options.limit === undefined ? 1000 : parseInt(options.limit, 10);
|
|
195
|
+
if (isNaN(limit)) limit = 1000;
|
|
196
|
+
const end = limit < 0 ? rows.length : Math.min(rows.length, start + limit);
|
|
197
|
+
let page = rows.slice(start, end).map(function (r) { return project(r, options.fields, key); });
|
|
198
|
+
const next_cursor = end < rows.length && page.length ? page[page.length - 1][key] : null;
|
|
199
|
+
return { list: page, count: total, next_cursor: next_cursor === undefined ? null : next_cursor };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
save() { return { ok: true, backend: 'memory' }; }
|
|
203
|
+
load() { return { ok: true, backend: 'memory', table_names: [] }; }
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
class JsonFileStore extends MemoryStore {
|
|
207
|
+
constructor(dir, options) {
|
|
208
|
+
super();
|
|
209
|
+
this.dir = dir;
|
|
210
|
+
this.backend = 'json';
|
|
211
|
+
this.persist = !(options && options.persist === false);
|
|
212
|
+
this.autoSave = !!(options && options.autoSave);
|
|
213
|
+
this.flush_interval = Math.max(0, Number(options && options.flush_interval) || 0);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
save(dir) {
|
|
217
|
+
if (dir) this.dir = dir;
|
|
218
|
+
fs.mkdirSync(this.dir, { recursive: true });
|
|
219
|
+
const names = this.tableNames();
|
|
220
|
+
for (let i = 0; i < names.length; i++) {
|
|
221
|
+
const file = path.join(this.dir, names[i] + '.json');
|
|
222
|
+
fs.writeFileSync(file, JSON.stringify(this.getRows(names[i]), null, 2), 'utf8');
|
|
223
|
+
}
|
|
224
|
+
fs.writeFileSync(path.join(this.dir, '_tables.json'), JSON.stringify(names, null, 2), 'utf8');
|
|
225
|
+
this.dirty = false;
|
|
226
|
+
return { ok: true, backend: 'json', dir: this.dir, tables: names.length };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
load(modelNames, dir) {
|
|
230
|
+
if (dir) this.dir = dir;
|
|
231
|
+
if (!fs.existsSync(this.dir)) return { ok: false, reason: '目录不存在' };
|
|
232
|
+
const meta = path.join(this.dir, '_tables.json');
|
|
233
|
+
let names = modelNames;
|
|
234
|
+
if (!names && fs.existsSync(meta)) names = JSON.parse(fs.readFileSync(meta, 'utf8'));
|
|
235
|
+
if (!names) return { ok: false, reason: '缺少 _tables.json' };
|
|
236
|
+
for (let i = 0; i < names.length; i++) {
|
|
237
|
+
const file = path.join(this.dir, names[i] + '.json');
|
|
238
|
+
if (fs.existsSync(file)) {
|
|
239
|
+
this.setTable(names[i], JSON.parse(fs.readFileSync(file, 'utf8')));
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return { ok: true, backend: 'json', dir: this.dir, tables: names.length, table_names: names };
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* v5 默认布局:每种模型对应 data/ 下的一个目录
|
|
248
|
+
* - kind:static → data/<name>/000001.json(一条记录一个文件,主键 6 位补零)
|
|
249
|
+
* - kind:dynamic → mm_cache 缓存 + mm_mongodb 持久;依赖不可用时回退
|
|
250
|
+
* data/<name>/_rows.json,保证离线可跑
|
|
251
|
+
* - mm_sql 为可选数据源适配器,启用后 flush 时同步一份关系型副本
|
|
252
|
+
*
|
|
253
|
+
* 同步 CRUD 始终走内存工作集(MemoryStore),外部依赖只在显式异步入口调用:
|
|
254
|
+
* connect() / flush() / restore() / cacheGet() / cacheList()。
|
|
255
|
+
*/
|
|
256
|
+
class HybridStore extends MemoryStore {
|
|
257
|
+
constructor(config) {
|
|
258
|
+
super();
|
|
259
|
+
config = config || {};
|
|
260
|
+
this.dir = config.dir || path.join(process.cwd(), 'data');
|
|
261
|
+
this.backend = 'hybrid';
|
|
262
|
+
this.persist = config.persist !== false;
|
|
263
|
+
this.autoSave = !!config.autoSave;
|
|
264
|
+
this.flush_interval = Math.max(0, Number(config.flush_interval) || 0);
|
|
265
|
+
this.namespace = config.namespace || 'mm_data';
|
|
266
|
+
// 文件层也走 jsondbAdmin 实例池,scope 需带上命名空间与目录
|
|
267
|
+
this.layout = new B.RowFileLayout(this.dir, this.namespace);
|
|
268
|
+
this.adapters = B.createAdapters(config);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
_model(name) { return this.models ? this.models.get(name) : null; }
|
|
272
|
+
/** 落盘只分两类:static → 逐文件;其余(dynamic / link / 未定义)→ 缓存 + 持久 */
|
|
273
|
+
_kind(name) { const m = this._model(name); return m && m.kind === 'static' ? 'static' : 'dynamic'; }
|
|
274
|
+
_key(name) { const m = this._model(name); return m ? m.key : 'id'; }
|
|
275
|
+
|
|
276
|
+
/** 同步落盘:静态逐文件 + 动态本地回退快照(不触碰外部依赖) */
|
|
277
|
+
save(dir) {
|
|
278
|
+
if (dir) { this.dir = dir; this.layout._bind(dir); }
|
|
279
|
+
fs.mkdirSync(this.dir, { recursive: true });
|
|
280
|
+
const names = this.tableNames().filter(function (n) { return n.charAt(0) !== '_'; });
|
|
281
|
+
let staticFiles = 0;
|
|
282
|
+
let dynamicFiles = 0;
|
|
283
|
+
for (let i = 0; i < names.length; i++) {
|
|
284
|
+
const name = names[i];
|
|
285
|
+
const rows = this.getRows(name);
|
|
286
|
+
if (this._kind(name) === 'static') {
|
|
287
|
+
staticFiles += this.layout.saveStatic(name, this._key(name), rows);
|
|
288
|
+
} else {
|
|
289
|
+
dynamicFiles += this.layout.saveDynamic(name, rows);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
this.dirty = false;
|
|
293
|
+
return {
|
|
294
|
+
ok: true, backend: 'hybrid', dir: this.dir, tables: names.length,
|
|
295
|
+
table_names: names, static_files: staticFiles, dynamic_fallback: dynamicFiles
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
/** 从 data/ 下的模型目录恢复;动态集合可被 mm_mongodb / mm_sql 覆盖 */
|
|
299
|
+
load(modelNames, dir) {
|
|
300
|
+
const that = this;
|
|
301
|
+
const res = this.layout.load(function (name, rows) { that.setTable(name, rows); }, dir);
|
|
302
|
+
if (dir) this.dir = dir;
|
|
303
|
+
// 以模型定义为准修正 kind(模型先于数据恢复装载)
|
|
304
|
+
if (res.collections) {
|
|
305
|
+
for (let i = 0; i < res.collections.length; i++) {
|
|
306
|
+
res.collections[i].kind = this._kind(res.collections[i].name);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return res;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** 显式连接可选依赖(未安装 / 未启用 / 连不上都不抛错) */
|
|
313
|
+
async connect() {
|
|
314
|
+
const out = await B.openAll(this.adapters);
|
|
315
|
+
out.file_layer = await this.layout.open();
|
|
316
|
+
return out;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
backends() {
|
|
320
|
+
return {
|
|
321
|
+
backend: 'hybrid', dir: this.dir, dependencies: B.probe(),
|
|
322
|
+
// 文件层单独报告,不塞进 adapters 以免破坏既有三适配器的契约
|
|
323
|
+
file_layer: this.layout.status(),
|
|
324
|
+
adapters: {
|
|
325
|
+
cache: this.adapters.cache.status(),
|
|
326
|
+
mongodb: this.adapters.mongo.status(),
|
|
327
|
+
sql: this.adapters.sql.status()
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** 把内存工作集推到外部层:静态逐文件、动态缓存 + 持久 + 可选 SQL */
|
|
333
|
+
async flush() {
|
|
334
|
+
const report = {
|
|
335
|
+
ok: true, static_files: 0, cache: 0, mongodb: 0, sql: 0,
|
|
336
|
+
local_snapshot: 0, file_fallback: 0, collections: []
|
|
337
|
+
};
|
|
338
|
+
const names = this.tableNames().filter(function (n) { return n.charAt(0) !== '_'; });
|
|
339
|
+
for (let i = 0; i < names.length; i++) {
|
|
340
|
+
const name = names[i];
|
|
341
|
+
const rows = this.getRows(name);
|
|
342
|
+
const one = { name: name, kind: this._kind(name), rows: rows.length };
|
|
343
|
+
if (one.kind === 'static') {
|
|
344
|
+
one.files = await this.layout.flushStatic(name, this._key(name), rows);
|
|
345
|
+
report.static_files += one.files;
|
|
346
|
+
} else {
|
|
347
|
+
await this.adapters.cache.set('rows:' + this.namespace + ':' + name, rows);
|
|
348
|
+
report.cache++;
|
|
349
|
+
const pushed = await this.adapters.mongo.push(name, rows);
|
|
350
|
+
if (pushed.ok) { report.mongodb++; one.mongodb = true; }
|
|
351
|
+
else { report.file_fallback++; one.file_fallback = true; }
|
|
352
|
+
// 本地快照始终保留一份:MongoDB 不可用时仍可恢复,也保证目录布局完整
|
|
353
|
+
await this.layout.flushDynamic(name, rows);
|
|
354
|
+
report.local_snapshot++;
|
|
355
|
+
const sql = await this.adapters.sql.push(name, this._key(name), rows);
|
|
356
|
+
if (sql.ok) { report.sql++; one.sql = sql.table; }
|
|
357
|
+
}
|
|
358
|
+
report.collections.push(one);
|
|
359
|
+
}
|
|
360
|
+
return report;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* 恢复:集合清单以模型定义为准(目录里可能没有动态集合),
|
|
365
|
+
* 先读 data/ 下的静态逐文件与动态本地快照,再用 mm_mongodb / mm_sql 覆盖动态集合。
|
|
366
|
+
*/
|
|
367
|
+
async restore(dir) {
|
|
368
|
+
const that = this;
|
|
369
|
+
const loaded = await this.layout.restore(function (name, rows) { that.setTable(name, rows); }, dir);
|
|
370
|
+
if (dir) this.dir = dir;
|
|
371
|
+
// 以模型定义为准修正 kind(模型先于数据恢复装载)
|
|
372
|
+
if (loaded.collections) {
|
|
373
|
+
for (let i = 0; i < loaded.collections.length; i++) {
|
|
374
|
+
loaded.collections[i].kind = this._kind(loaded.collections[i].name);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
const names = this.models ? Array.from(this.models.keys()) : [];
|
|
378
|
+
for (let i = 0; i < loaded.table_names.length; i++) {
|
|
379
|
+
if (names.indexOf(loaded.table_names[i]) === -1) names.push(loaded.table_names[i]);
|
|
380
|
+
}
|
|
381
|
+
names.sort();
|
|
382
|
+
const sources = {};
|
|
383
|
+
for (let i = 0; i < names.length; i++) {
|
|
384
|
+
const name = names[i];
|
|
385
|
+
if (this._kind(name) !== 'dynamic') {
|
|
386
|
+
sources[name] = this.layout.backend === 'mm_jsondb' ? 'mm_jsondb' : 'static-files';
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
let rows = null;
|
|
390
|
+
let source = '';
|
|
391
|
+
if (this.adapters.mongo.connected) {
|
|
392
|
+
try { rows = await this.adapters.mongo.pull(name); } catch (e) { rows = null; }
|
|
393
|
+
if (Array.isArray(rows)) source = 'mm_mongodb';
|
|
394
|
+
}
|
|
395
|
+
if (rows === null && this.adapters.sql.connected) {
|
|
396
|
+
try { rows = await this.adapters.sql.pull(name); } catch (e) { rows = null; }
|
|
397
|
+
if (Array.isArray(rows)) source = 'mm_sql';
|
|
398
|
+
}
|
|
399
|
+
if (Array.isArray(rows)) {
|
|
400
|
+
this.setTable(name, rows);
|
|
401
|
+
sources[name] = source;
|
|
402
|
+
} else {
|
|
403
|
+
if (loaded.table_names.indexOf(name) === -1) this.setTable(name, []);
|
|
404
|
+
sources[name] = 'local-snapshot';
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
this.seq.clear();
|
|
408
|
+
return { ok: true, backend: 'hybrid', dir: this.dir, table_names: names, sources: sources };
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function createStore(type, config) {
|
|
413
|
+
config = config || {};
|
|
414
|
+
if (type === 'json' || type === 'file') {
|
|
415
|
+
return new JsonFileStore(config.dir || './data', config);
|
|
416
|
+
}
|
|
417
|
+
if (type === 'hybrid' || type === 'rowfile' || type === 'auto') {
|
|
418
|
+
return new HybridStore(config);
|
|
419
|
+
}
|
|
420
|
+
return new MemoryStore();
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
module.exports = {
|
|
424
|
+
MemoryStore: MemoryStore,
|
|
425
|
+
JsonFileStore: JsonFileStore,
|
|
426
|
+
HybridStore: HybridStore,
|
|
427
|
+
createStore: createStore,
|
|
428
|
+
match: match,
|
|
429
|
+
parseOrder: parseOrder
|
|
430
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,65 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "mm_datas",
|
|
3
|
+
"version": "5.3.0",
|
|
4
|
+
"description": "基于 mm_admin 实例池的通用数据管理模块:*_model.json 决定集合,标准 JSON Schema(draft-07 子集,禁用 int/integer)由 mm_validator 校验;静态数据经 mm_jsondb 的 Store 文件层(pad:6 由模块自身补零)按 data/<集合>/000001.json 逐条落盘,动态数据走 mm_cache 缓存 + mm_mongodb 持久,mm_sql 作为可选数据源适配器;四个依赖统一经 jsondbAdmin/cacheAdmin/mongodbAdmin/sqlAdmin 获取;工具参数契约采用 OpenAI function calling 的 parameters 格式",
|
|
5
|
+
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://gitee.com/qiuwenwu91/mm_data.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"mm_mod",
|
|
13
|
+
"mm_data",
|
|
14
|
+
"mm_admin",
|
|
15
|
+
"mm_validator",
|
|
16
|
+
"DataManager",
|
|
17
|
+
"schema",
|
|
18
|
+
"json-schema",
|
|
19
|
+
"draft-07",
|
|
20
|
+
"openai",
|
|
21
|
+
"function-calling",
|
|
22
|
+
"parameters",
|
|
23
|
+
"crud",
|
|
24
|
+
"static-data",
|
|
25
|
+
"dynamic-data",
|
|
26
|
+
"mm_cache",
|
|
27
|
+
"mm_sql",
|
|
28
|
+
"mm_mongodb",
|
|
29
|
+
"mm_jsondb",
|
|
30
|
+
"row-file",
|
|
31
|
+
"game",
|
|
32
|
+
"游戏数据"
|
|
33
|
+
],
|
|
34
|
+
"layer": "store",
|
|
35
|
+
"group": "data",
|
|
36
|
+
"author": "邱文武",
|
|
37
|
+
"license": "ISC",
|
|
38
|
+
"scripts": {
|
|
39
|
+
"test": "node test.js",
|
|
40
|
+
"demo": "node example/custom_demo.js",
|
|
41
|
+
"demo:preset": "node example/preset_demo.js",
|
|
42
|
+
"demo:rpg": "node example/rpg_demo.js",
|
|
43
|
+
"demo:rpg:crud": "node example/rpg_crud.js",
|
|
44
|
+
"demo:rpg:relations": "node example/rpg_relations.js",
|
|
45
|
+
"demo:rpg:cache": "node example/rpg_cache.js",
|
|
46
|
+
"demo:rpg:persistence": "node example/rpg_persistence.js",
|
|
47
|
+
"demo:storage": "node example/storage_demo.js"
|
|
48
|
+
},
|
|
49
|
+
"files": [
|
|
50
|
+
"index.js",
|
|
51
|
+
"lib",
|
|
52
|
+
"tool",
|
|
53
|
+
"example",
|
|
54
|
+
"test.js"
|
|
55
|
+
],
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"mm_admin": "^1.2.0",
|
|
58
|
+
"mm_validator": "^1.0.1",
|
|
59
|
+
"mm_cache": "^1.8.0",
|
|
60
|
+
"mm_sql": "^1.11.0",
|
|
61
|
+
"mm_mongodb": "^2.1.0",
|
|
62
|
+
"mm_jsondb": "^1.2.0",
|
|
63
|
+
"mm_persist": "^1.0.0"
|
|
64
|
+
}
|
|
65
|
+
}
|