87 lines
3.0 KiB
JavaScript
87 lines
3.0 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 将 methods/system.json 全量导入库表(wms_method_doc_meta + wms_api_method_doc,按 method upsert)。
|
||
*
|
||
* 用法:
|
||
* node script/method-doc-import.cjs [--env production] [--doc system] [--file methods/system.json]
|
||
*/
|
||
|
||
const fs = require('node:fs');
|
||
const {
|
||
resolveEnv, createConnection, parseArgs, today, fail,
|
||
} = require('./method-doc-common.cjs');
|
||
|
||
(async () => {
|
||
const args = parseArgs();
|
||
const env = resolveEnv();
|
||
const docName = args.get('doc') || 'system';
|
||
const file = args.get('file') || `methods/${docName}.json`;
|
||
|
||
const doc = JSON.parse(fs.readFileSync(file, 'utf8'));
|
||
if (!doc.methods || !Array.isArray(doc.methods.groups)) {
|
||
fail(`不支持的文档结构:${file} 缺少 methods.groups`);
|
||
}
|
||
const groups = doc.methods.groups || [];
|
||
|
||
const conn = await createConnection(env);
|
||
|
||
// 1. 元信息 upsert
|
||
await conn.query(
|
||
`INSERT INTO wms_method_doc_meta (id, doc_name, module, description, headers, version, updated_at)
|
||
VALUES (1, ?, ?, ?, ?, ?, ?)
|
||
ON DUPLICATE KEY UPDATE module=VALUES(module), description=VALUES(description),
|
||
headers=VALUES(headers), version=VALUES(version), updated_at=VALUES(updated_at)`,
|
||
[
|
||
docName,
|
||
doc.module || docName,
|
||
doc.description || '',
|
||
JSON.stringify(doc.headers || []),
|
||
doc.version || '0.3.0',
|
||
doc.updatedAt || today(),
|
||
]
|
||
);
|
||
|
||
// 2. 方法文档 upsert(保留原始分组/组内顺序)
|
||
const upsertSql = `
|
||
INSERT INTO wms_api_method_doc
|
||
(method, module, group_name, description, is_need_login, is_need_auth,
|
||
headers, params, response, errors, group_sort, item_sort, status, payload)
|
||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?)
|
||
ON DUPLICATE KEY UPDATE
|
||
module=VALUES(module), group_name=VALUES(group_name), description=VALUES(description),
|
||
is_need_login=VALUES(is_need_login), is_need_auth=VALUES(is_need_auth),
|
||
headers=VALUES(headers), params=VALUES(params), response=VALUES(response),
|
||
errors=VALUES(errors), group_sort=VALUES(group_sort), item_sort=VALUES(item_sort),
|
||
status=1, payload=VALUES(payload)`;
|
||
|
||
let upserted = 0;
|
||
for (let g = 0; g < groups.length; g++) {
|
||
const group = groups[g];
|
||
const list = Array.isArray(group.list) ? group.list : [];
|
||
for (let i = 0; i < list.length; i++) {
|
||
const item = list[i];
|
||
if (!item || !item.method) continue;
|
||
await conn.query(upsertSql, [
|
||
item.method,
|
||
group.module || '',
|
||
group.groupName || '',
|
||
item.description || '',
|
||
item.isNeedLogin ? 1 : 0,
|
||
item.isNeedAuth ? 1 : 0,
|
||
JSON.stringify(item.headers || []),
|
||
JSON.stringify(item.params || []),
|
||
JSON.stringify(item.response || []),
|
||
JSON.stringify(item.errors || []),
|
||
g,
|
||
i,
|
||
JSON.stringify(item),
|
||
]);
|
||
upserted++;
|
||
}
|
||
}
|
||
|
||
await conn.end();
|
||
console.log(`[method-doc] 导入完成:doc=${docName} file=${file} groups=${groups.length} methods=${upserted}(env=${env})`);
|
||
})().catch(e => fail(e.message));
|