Import project sources

This commit is contained in:
2026-11-05 01:04:53 +08:00
commit 0de3879565
29 changed files with 3570 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
import {mkdir,readFile,readdir} from 'node:fs/promises';
import path from 'node:path';
import {root,atomic,dir} from './store';
import {buildSchema,Field,InputError} from './schema';
export type SavedSchema={id:string;name:string;fields:Field[];schema:any;createdAt:string;updatedAt:string;version:number};
export function schemaPath(id:string){dir(id);return path.join(root,'schemas',id+'.json');}
export async function getSchema(id:string):Promise<SavedSchema>{return JSON.parse(await readFile(schemaPath(id),'utf8'));}
export async function listSchemas(){await mkdir(path.join(root,'schemas'),{recursive:true});const files=await readdir(path.join(root,'schemas'));return (await Promise.all(files.filter(f=>f.endsWith('.json')).map(f=>getSchema(f.slice(0,-5))))).sort((a,b)=>b.updatedAt.localeCompare(a.updatedAt));}
export async function putSchema(id:string,body:any,previous?:SavedSchema){if(typeof body?.name!=='string'||!body.name.trim()||body.name.length>120)throw new InputError('name 必须是 1–120 字符的名称');const schema=buildSchema(body.fields);const time=new Date().toISOString();const saved:SavedSchema={id,name:body.name.trim(),fields:body.fields,schema,createdAt:previous?.createdAt||time,updatedAt:time,version:(previous?.version||0)+1};await mkdir(path.join(root,'schemas'),{recursive:true});await atomic(schemaPath(id),saved);return saved;}