从 ssw-mvp 抽取隐私计算编排域,框架换 JNPF Lite 契约 (NestJS + TypeORM + PGlite 嵌入式 Postgres + React 18/Vite), 执行底座对接 ../ssw-mpc Rust CLI。 三个兄弟模块: - sswflow(编排,12 表):DAG 画布(React Flow @xyflow/react)、 19 算子目录、可插拔执行引擎(mpc / simulation)、运行监控 - sswdevice(设备注册,1 表):真源 = ssw-mpc 控制面 GET /nodes, TeeNode 字段逐一对应,含 TTL 90s 在线清单的下线语义 - sswchain(链上存证,1 表):真源 = ssw-mpc outbox 二进制 canonical_bytes + FISCO 国密链自有 RPC,Receipt 四验证位分开报 配套 scripts/ 六个门禁脚本(期望数字写死)+ docs/e2e 端到端演示 + CLAUDE.md 项目级上下文。
121 lines
4.2 KiB
JavaScript
121 lines
4.2 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* 从实体定义生成 db/schema.sql —— 保证 SQL 与 TypeORM 实体零漂移。
|
||
*
|
||
* 用法:node scripts/gen-schema.js
|
||
*
|
||
* 解析规则(只覆盖本项目实体实际用到的写法,刻意不做通用解析器):
|
||
* @Entity({ name: 'xxx' }) -> 表名
|
||
* @PrimaryColumn({ name: 'f_xx', type: 'varchar', length: 50 })
|
||
* @Column({ name: 'f_xx', type: 'varchar', length: 255, nullable: true })
|
||
* @Column({ name: 'f_xx', type: 'int', nullable: true, default: 0 })
|
||
*
|
||
* 输出对齐 JNPF 原生 schema 风格:f_ 前缀 + varchar(50) 主键 + f_delete_mark 软删。
|
||
*/
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
const ROOT = path.resolve(__dirname, '..');
|
||
const ENTITY_DIR = path.join(ROOT, 'src/backend/src');
|
||
const OUT = path.join(ROOT, 'src/backend/db/schema.sql');
|
||
|
||
/** varchar(n) / text / int / timestamp / numeric → Postgres 方言 */
|
||
function sqlType(col) {
|
||
const type = (col.type || 'varchar').toLowerCase();
|
||
if (type === 'text') return 'TEXT';
|
||
if (type === 'int' || type === 'integer') return 'INT';
|
||
if (type === 'bigint') return 'BIGINT';
|
||
if (type === 'timestamp') return 'TIMESTAMP';
|
||
if (type === 'numeric' || type === 'decimal') return 'NUMERIC';
|
||
if (type === 'boolean' || type === 'bool') return 'BOOLEAN';
|
||
if (type === 'json' || type === 'jsonb') return 'JSONB';
|
||
if (type === 'varchar') return col.length ? `VARCHAR(${col.length})` : 'VARCHAR(255)';
|
||
return 'VARCHAR(255)';
|
||
}
|
||
|
||
function parseEntity(file) {
|
||
const src = fs.readFileSync(file, 'utf8');
|
||
const tableMatch = src.match(/@Entity\(\{\s*name:\s*'([^']+)'/);
|
||
if (!tableMatch) return null;
|
||
const table = tableMatch[1];
|
||
|
||
const cols = [];
|
||
const re = /@(Primary)?Column\(\{([^}]*)\}\)/g;
|
||
let m;
|
||
while ((m = re.exec(src))) {
|
||
const isPk = !!m[1];
|
||
const attrs = m[2];
|
||
const name = (attrs.match(/name:\s*'([^']+)'/) || [])[1];
|
||
if (!name) continue;
|
||
const type = (attrs.match(/type:\s*'([^']+)'/) || [])[1];
|
||
const length = (attrs.match(/length:\s*(\d+)/) || [])[1];
|
||
const nullable = !attrs.includes('nullable: false');
|
||
const def = (attrs.match(/default:\s*([^,}]+)/) || [])[1];
|
||
cols.push({
|
||
name,
|
||
sqlType: sqlType({ type, length }),
|
||
nullable,
|
||
default: def !== undefined && def !== 'null' ? def.trim() : null,
|
||
isPk,
|
||
});
|
||
}
|
||
return { table, cols };
|
||
}
|
||
|
||
function render(t) {
|
||
const lines = [];
|
||
lines.push(`CREATE TABLE IF NOT EXISTS ${t.table} (`);
|
||
const body = t.cols.map((c) => {
|
||
let s = ` ${c.name.padEnd(22)}${c.sqlType}`;
|
||
if (c.isPk) s += ' PRIMARY KEY';
|
||
else if (!c.nullable) s += ' NOT NULL';
|
||
if (c.default !== null) s += ` DEFAULT ${c.default}`;
|
||
return s;
|
||
});
|
||
lines.push(body.join(',\n'));
|
||
lines.push(');');
|
||
return lines.join('\n');
|
||
}
|
||
|
||
function walk(dir) {
|
||
const out = [];
|
||
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const full = path.join(dir, entry.name);
|
||
if (entry.isDirectory()) out.push(...walk(full));
|
||
else if (entry.name.endsWith('.entity.ts')) out.push(full);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
const files = walk(ENTITY_DIR).sort();
|
||
const tables = files.map((f) => parseEntity(f)).filter(Boolean);
|
||
|
||
if (!tables.length) {
|
||
console.error('未解析到任何实体,检查目录:' + ENTITY_DIR);
|
||
process.exit(1);
|
||
}
|
||
|
||
const header = `-- JNPF 原生 schema(Postgres 方言)
|
||
-- 本文件由 scripts/gen-schema.js 从 src/backend/src/**/*.entity.ts 自动生成,请勿手改。
|
||
-- 改列结构请改实体后重跑:node scripts/gen-schema.js
|
||
--
|
||
-- 契约(对齐 JNPF / ssw-mvp):
|
||
-- f_ 前缀列名、主键 f_id VARCHAR(50)、软删 f_delete_mark(0=正常 1=已删)
|
||
-- MVP 期由 TypeORM synchronize 自动建表;迁 JNPF 时关掉 synchronize,直接复用本文件。
|
||
|
||
CREATE TABLE IF NOT EXISTS demo_item (
|
||
f_id VARCHAR(50) PRIMARY KEY,
|
||
f_title VARCHAR(255),
|
||
f_status VARCHAR(32),
|
||
f_creator_user_id VARCHAR(50),
|
||
f_creator_time TIMESTAMP,
|
||
f_delete_mark INT DEFAULT 0
|
||
);
|
||
`;
|
||
|
||
const out = header + '\n' + tables.map(render).join('\n\n') + '\n';
|
||
fs.writeFileSync(OUT, out);
|
||
console.log(`已生成 ${OUT}`);
|
||
console.log(` 表数量:${tables.length + 1}(含 demo_item)`);
|
||
for (const t of tables) console.log(` - ${t.table} ${t.cols.length} 列`);
|