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
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* data 工具 —— AI / 工作流调用面
|
|
4
|
+
* 契约签名 async main(params, ctx, options),写 ctx.output 并返回
|
|
5
|
+
*
|
|
6
|
+
* 底层基于 mm_admin 实例池:model 是任意集合名,dataAdmin(model) 取得的
|
|
7
|
+
* DataManager 实例就是该集合;写入统一走 model schema 严格约束
|
|
8
|
+
* (schema 是标准 JSON Schema,规则判定委托 mm_validator,类型不含 int/integer)。
|
|
9
|
+
*
|
|
10
|
+
* 契约 tool.json 的参数定义采用 OpenAI function calling 的 parameters 格式
|
|
11
|
+
* (type:object + properties + required),type 保持仓库工作流通道 data;
|
|
12
|
+
* 通过 mm.getFunctionTool('data') 可导出标准 function 工具定义直接注册给 LLM。
|
|
13
|
+
*/
|
|
14
|
+
const mm = require('../../index.js');
|
|
15
|
+
|
|
16
|
+
function injected(params, options) {
|
|
17
|
+
return (params && (params.admin || params.world)) ||
|
|
18
|
+
(options && (options.admin || options.world)) || null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** 取集合实例:支持注入 DataManager 实例 / mm_admin 池 / 默认池 */
|
|
22
|
+
function col(params, options, name) {
|
|
23
|
+
const a = injected(params, options);
|
|
24
|
+
if (a && typeof a.collection === 'function') return a.collection(name);
|
|
25
|
+
if (a && typeof a.data === 'function') return a.data(name);
|
|
26
|
+
return mm.dataAdmin(name);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 取注册表级入口(verify/stats/save/load/dump 等) */
|
|
30
|
+
function registry(params, options) {
|
|
31
|
+
const a = injected(params, options);
|
|
32
|
+
if (a && typeof a.verify === 'function') return a;
|
|
33
|
+
if (a && typeof a.getServer === 'function') return mm.dataAdmin.registry(a);
|
|
34
|
+
return mm.dataAdmin.registry();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function opts(params) {
|
|
38
|
+
const o = Object.assign({}, params.options || {});
|
|
39
|
+
if (params.with) o.with = params.with;
|
|
40
|
+
return o;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function main(params, ctx, options) {
|
|
44
|
+
params = params || {};
|
|
45
|
+
ctx = ctx || {};
|
|
46
|
+
options = options || {};
|
|
47
|
+
const action = params.action || 'list';
|
|
48
|
+
const model = params.model || params.collection;
|
|
49
|
+
const query = params.query === undefined ? {} : params.query;
|
|
50
|
+
const body = params.body || {};
|
|
51
|
+
const o = opts(params);
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
let out;
|
|
55
|
+
const c = function () { return col(params, options, model); };
|
|
56
|
+
const g = function () { return registry(params, options); };
|
|
57
|
+
|
|
58
|
+
switch (action) {
|
|
59
|
+
case 'add': out = c().add(body, o); break;
|
|
60
|
+
case 'addMany': out = c().addMany(body.rows || body.list || body, o); break;
|
|
61
|
+
case 'set': out = c().set(query, body, o); break;
|
|
62
|
+
case 'patch': out = c().patch(query, body, o); break;
|
|
63
|
+
case 'upsert': out = c().upsert(query, body, o); break;
|
|
64
|
+
case 'del':
|
|
65
|
+
case 'delMany': out = c().del(query, o); break;
|
|
66
|
+
case 'get':
|
|
67
|
+
case 'resolve': out = { obj: c().get(query, o) }; break;
|
|
68
|
+
case 'list':
|
|
69
|
+
case 'scroll': out = c().list(query, o); break;
|
|
70
|
+
case 'all': out = { list: c().all(o), count: c().count({}) }; break;
|
|
71
|
+
case 'count': out = { count: c().count(query) }; break;
|
|
72
|
+
case 'validate': out = { obj: c().validate(body, o) }; break;
|
|
73
|
+
case 'reverse': out = c().reverse(query, o); break;
|
|
74
|
+
case 'generate': out = c().generate(o.count, o.template, o); break;
|
|
75
|
+
case 'seed': out = c().seed(body.rows || body.list || body, o); break;
|
|
76
|
+
case 'reload': out = c().reload(body.rows || body.list, o); break;
|
|
77
|
+
|
|
78
|
+
case 'collection':
|
|
79
|
+
case 'info': out = { obj: Object.assign({}, c().info(), { validator: mm.validator() }) }; break;
|
|
80
|
+
case 'validator': out = { obj: mm.validator(), msg: 'schema 规则由 mm_validator 执行' }; break;
|
|
81
|
+
case 'collectionList':
|
|
82
|
+
case 'modelList': out = { list: g().models(), count: g().models().length }; break;
|
|
83
|
+
case 'define': out = { obj: c().define(body.def || body), msg: '集合已定义' }; break;
|
|
84
|
+
case 'redefine': out = { obj: c().define(body.def || body, o), msg: '集合已重定义' }; break;
|
|
85
|
+
case 'field': out = { obj: c().field(body.name, body.def), msg: '字段已定义' }; break;
|
|
86
|
+
case 'ref': out = { obj: c().ref(body.field, body.target, body.options), msg: '关联已声明' }; break;
|
|
87
|
+
case 'drop': out = c().drop(); break;
|
|
88
|
+
case 'clear': out = c().clear(); break;
|
|
89
|
+
|
|
90
|
+
case 'verify': out = g().verify(); break;
|
|
91
|
+
case 'stats': out = g().stats(); break;
|
|
92
|
+
case 'loadModels': out = { list: g().loadModels(o.dir || o.model_dir) }; break;
|
|
93
|
+
case 'save': out = g().save(o.dir); break;
|
|
94
|
+
case 'load': out = g().load(o.dir); break;
|
|
95
|
+
case 'dump': out = { obj: g().dump() }; break;
|
|
96
|
+
default:
|
|
97
|
+
out = { error: { code: 'MGR_UNKNOWN_ACTION', message: '不支持的操作: ' + action } };
|
|
98
|
+
}
|
|
99
|
+
ctx.output = out;
|
|
100
|
+
return ctx.output;
|
|
101
|
+
} catch (e) {
|
|
102
|
+
ctx.output = { error: { code: e.code || 'MGR_ERROR', message: e.message } };
|
|
103
|
+
return ctx.output;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
module.exports = { main: main };
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "data",
|
|
3
|
+
"handler": "manager",
|
|
4
|
+
"title": "通用数据集合管理器(DataManager)",
|
|
5
|
+
"description": "基于 mm_admin 实例池:model 填任意集合名,取得的就是该集合的 DataManager 实例,同名复用。集合名、字段、主键、静态/动态、关联由调用方自定义;model schema 是标准 JSON Schema(draft-07 子集:$schema/type:object/properties/required/additionalProperties,字段用 type/required/enum/const/minimum/maximum/minLength/maxLength/minItems/maxItems/pattern/items/properties),类型只允许 string/number/boolean/object/array/null,禁止 int/integer(整数一律用 number)。规则判定全部委托 mm_validator,mm_datas 只做关键词翻译与错误路径归一化;action=validator 可查询校验器与类型白名单。读在 obj / list+count,写在 msg+affected/id,异常返回 error。工具参数 parameters 采用 OpenAI function calling 标准格式(type:object + properties + required),type 为工作流通道 data,可由 getFunctionTool() 导出标准 function 工具定义直接注册给 LLM。工作流提示:action 选 get/list/add/set/del,query 传条件或主键值,body 传写入数据,with 传关联名;先 define/field/ref 声明结构再写入;validate 可只校验不写入。",
|
|
6
|
+
"type": "data",
|
|
7
|
+
"parameters": {
|
|
8
|
+
"type": "object",
|
|
9
|
+
"properties": {
|
|
10
|
+
"action": {
|
|
11
|
+
"type": "string",
|
|
12
|
+
"enum": [
|
|
13
|
+
"get",
|
|
14
|
+
"list",
|
|
15
|
+
"all",
|
|
16
|
+
"count",
|
|
17
|
+
"scroll",
|
|
18
|
+
"resolve",
|
|
19
|
+
"reverse",
|
|
20
|
+
"add",
|
|
21
|
+
"addMany",
|
|
22
|
+
"set",
|
|
23
|
+
"patch",
|
|
24
|
+
"upsert",
|
|
25
|
+
"del",
|
|
26
|
+
"delMany",
|
|
27
|
+
"generate",
|
|
28
|
+
"validate",
|
|
29
|
+
"collection",
|
|
30
|
+
"validator",
|
|
31
|
+
"collectionList",
|
|
32
|
+
"verify",
|
|
33
|
+
"stats",
|
|
34
|
+
"define",
|
|
35
|
+
"redefine",
|
|
36
|
+
"field",
|
|
37
|
+
"ref",
|
|
38
|
+
"drop",
|
|
39
|
+
"clear",
|
|
40
|
+
"seed",
|
|
41
|
+
"reload",
|
|
42
|
+
"loadModels",
|
|
43
|
+
"save",
|
|
44
|
+
"load",
|
|
45
|
+
"dump"
|
|
46
|
+
],
|
|
47
|
+
"description": "操作类型。CRUD: get/list/all/add/set/del;schema(标准 JSON Schema): define/field/ref/validate(规则由 mm_validator 执行);进阶: patch/addMany/upsert/generate/reverse;全局: validator/collectionList/verify/stats/loadModels/save/load/dump",
|
|
48
|
+
"default": "list"
|
|
49
|
+
},
|
|
50
|
+
"model": {
|
|
51
|
+
"type": "string",
|
|
52
|
+
"description": "集合名,完全自定义,如 role / map / item / guild。不存在时自动创建为自由集合(任意字段、自动 id);定义标准 JSON Schema 后即启用严格约束"
|
|
53
|
+
},
|
|
54
|
+
"collection": {
|
|
55
|
+
"type": "string",
|
|
56
|
+
"description": "集合名别名,与 model 二选一"
|
|
57
|
+
},
|
|
58
|
+
"query": {
|
|
59
|
+
"type": "object",
|
|
60
|
+
"description": "查询条件,可直接传主键值。支持 { field: value } 或 { field: { $gt: N, $in: [...], $like: 'x' } };写操作必须带条件;reverse 时传被引用的键值",
|
|
61
|
+
"default": {}
|
|
62
|
+
},
|
|
63
|
+
"body": {
|
|
64
|
+
"type": "object",
|
|
65
|
+
"description": "写入数据;addMany/seed 可传 { rows: [...] };patch 传 { $inc:{gold:100}, $set:{...} };define 传集合定义(其中 schema 为一份标准 JSON Schema 文档);field 传 { name, def }(def 是字段级 JSON Schema);ref 传 { field, target, options };validate 传待校验 object",
|
|
66
|
+
"default": {}
|
|
67
|
+
},
|
|
68
|
+
"with": {
|
|
69
|
+
"type": "array",
|
|
70
|
+
"description": "读时展开的关联名,由集合定义里的 ref / relations 决定。可传 ['map','items'] 或 [{ ref:'item', as:'item_info', select:['name','price'] }]",
|
|
71
|
+
"default": []
|
|
72
|
+
},
|
|
73
|
+
"options": {
|
|
74
|
+
"type": "object",
|
|
75
|
+
"description": "附加选项: { limit: 50, offset: 0, order: 'id DESC', fields: [...], cursor: 12, count: 5, template: {...}, allow_all: false, force: true, raw: false, dir: './data', model_dir: '...' }",
|
|
76
|
+
"default": {}
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"required": [
|
|
80
|
+
"action"
|
|
81
|
+
]
|
|
82
|
+
},
|
|
83
|
+
"params": {
|
|
84
|
+
"type": "object",
|
|
85
|
+
"properties": {
|
|
86
|
+
"action": {
|
|
87
|
+
"type": "string",
|
|
88
|
+
"enum": [
|
|
89
|
+
"get",
|
|
90
|
+
"list",
|
|
91
|
+
"all",
|
|
92
|
+
"count",
|
|
93
|
+
"scroll",
|
|
94
|
+
"resolve",
|
|
95
|
+
"reverse",
|
|
96
|
+
"add",
|
|
97
|
+
"addMany",
|
|
98
|
+
"set",
|
|
99
|
+
"patch",
|
|
100
|
+
"upsert",
|
|
101
|
+
"del",
|
|
102
|
+
"delMany",
|
|
103
|
+
"generate",
|
|
104
|
+
"validate",
|
|
105
|
+
"collection",
|
|
106
|
+
"validator",
|
|
107
|
+
"collectionList",
|
|
108
|
+
"verify",
|
|
109
|
+
"stats",
|
|
110
|
+
"define",
|
|
111
|
+
"redefine",
|
|
112
|
+
"field",
|
|
113
|
+
"ref",
|
|
114
|
+
"drop",
|
|
115
|
+
"clear",
|
|
116
|
+
"seed",
|
|
117
|
+
"reload",
|
|
118
|
+
"loadModels",
|
|
119
|
+
"save",
|
|
120
|
+
"load",
|
|
121
|
+
"dump"
|
|
122
|
+
],
|
|
123
|
+
"description": "操作类型。CRUD: get/list/all/add/set/del;schema(标准 JSON Schema): define/field/ref/validate(规则由 mm_validator 执行);进阶: patch/addMany/upsert/generate/reverse;全局: validator/collectionList/verify/stats/loadModels/save/load/dump",
|
|
124
|
+
"default": "list"
|
|
125
|
+
},
|
|
126
|
+
"model": {
|
|
127
|
+
"type": "string",
|
|
128
|
+
"description": "集合名,完全自定义,如 role / map / item / guild。不存在时自动创建为自由集合(任意字段、自动 id);定义标准 JSON Schema 后即启用严格约束"
|
|
129
|
+
},
|
|
130
|
+
"collection": {
|
|
131
|
+
"type": "string",
|
|
132
|
+
"description": "集合名别名,与 model 二选一"
|
|
133
|
+
},
|
|
134
|
+
"query": {
|
|
135
|
+
"type": "object",
|
|
136
|
+
"description": "查询条件,可直接传主键值。支持 { field: value } 或 { field: { $gt: N, $in: [...], $like: 'x' } };写操作必须带条件;reverse 时传被引用的键值",
|
|
137
|
+
"default": {}
|
|
138
|
+
},
|
|
139
|
+
"body": {
|
|
140
|
+
"type": "object",
|
|
141
|
+
"description": "写入数据;addMany/seed 可传 { rows: [...] };patch 传 { $inc:{gold:100}, $set:{...} };define 传集合定义(其中 schema 为一份标准 JSON Schema 文档);field 传 { name, def }(def 是字段级 JSON Schema);ref 传 { field, target, options };validate 传待校验 object",
|
|
142
|
+
"default": {}
|
|
143
|
+
},
|
|
144
|
+
"with": {
|
|
145
|
+
"type": "array",
|
|
146
|
+
"description": "读时展开的关联名,由集合定义里的 ref / relations 决定。可传 ['map','items'] 或 [{ ref:'item', as:'item_info', select:['name','price'] }]",
|
|
147
|
+
"default": []
|
|
148
|
+
},
|
|
149
|
+
"options": {
|
|
150
|
+
"type": "object",
|
|
151
|
+
"description": "附加选项: { limit: 50, offset: 0, order: 'id DESC', fields: [...], cursor: 12, count: 5, template: {...}, allow_all: false, force: true, raw: false, dir: './data', model_dir: '...' }",
|
|
152
|
+
"default": {}
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
"required": [
|
|
156
|
+
"action"
|
|
157
|
+
]
|
|
158
|
+
},
|
|
159
|
+
"returns": {
|
|
160
|
+
"description": "统一返回格式:读操作返回 obj 或 list+count+next_cursor,写操作返回 msg+affected/id/ids,异常返回 error",
|
|
161
|
+
"type": "object",
|
|
162
|
+
"properties": {
|
|
163
|
+
"obj": {
|
|
164
|
+
"type": "object",
|
|
165
|
+
"description": "单条结果(get/resolve/collection/define/validate/dump)"
|
|
166
|
+
},
|
|
167
|
+
"list": {
|
|
168
|
+
"type": "array",
|
|
169
|
+
"description": "查询结果列表"
|
|
170
|
+
},
|
|
171
|
+
"count": {
|
|
172
|
+
"type": "number",
|
|
173
|
+
"description": "匹配总数"
|
|
174
|
+
},
|
|
175
|
+
"next_cursor": {
|
|
176
|
+
"description": "下一页游标"
|
|
177
|
+
},
|
|
178
|
+
"msg": {
|
|
179
|
+
"type": "string",
|
|
180
|
+
"description": "写操作结果描述"
|
|
181
|
+
},
|
|
182
|
+
"affected": {
|
|
183
|
+
"type": "number",
|
|
184
|
+
"description": "受影响行数"
|
|
185
|
+
},
|
|
186
|
+
"id": {
|
|
187
|
+
"description": "新增记录主键"
|
|
188
|
+
},
|
|
189
|
+
"ids": {
|
|
190
|
+
"type": "array",
|
|
191
|
+
"description": "批量新增主键列表"
|
|
192
|
+
},
|
|
193
|
+
"error": {
|
|
194
|
+
"type": "object",
|
|
195
|
+
"description": "错误对象 { code, message }"
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
"options": {
|
|
200
|
+
"admin": {
|
|
201
|
+
"type": "object",
|
|
202
|
+
"description": "可注入已创建的 DataManager 实例或 mm_admin 实例池,省略时使用模块默认池 reg('mm_datas')"
|
|
203
|
+
},
|
|
204
|
+
"config": {
|
|
205
|
+
"type": "object",
|
|
206
|
+
"description": "创建独立实例池时的配置: { namespace:'game2', store:'memory'|'json', dir:'./data', models:'./my_models' }",
|
|
207
|
+
"default": {}
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
"group": "data"
|
|
211
|
+
}
|
package/README.md
DELETED