/** * module: ledger —— 本模块要外界提供的能力(编码规范 §10.1 / §5.3)。 */ import type { EnvelopeDraft, LedgerKey, LedgerQuery, LedgerRow } from '@contracts'; import type { MaterializedObject, SchemaChange } from './internal/domain/schema-merge.ts'; import type { DraftEntry } from './internal/domain/task-view.ts'; export type Clock = { now(): Date; }; export type IdGenerator = { next(): string; }; /** 数据事件的发射口 —— app 注入 `event` 模块的出口(§5.2) */ export type EventPublisher = { publish(envelope: EnvelopeDraft): Promise; }; export type ObjectRef = { readonly domain: string; readonly object: string; }; /** * 台账存储 —— 运行时 DDL / DML 由声明生成,ORM 静态 schema 不适用, * 一律 node-postgres 直写(§1 / ADR-002)。 */ export type LedgerStore = { /** 已物化对象的列与主键;未物化返回 undefined */ describe(ref: ObjectRef): Promise; listObjects(): Promise; /** 执行「只增不破坏」的变更计划 */ applyChanges(ref: ObjectRef, changes: readonly SchemaChange[]): Promise; select(query: LedgerQuery): Promise<{ rows: LedgerRow[]; total: number; }>; selectByKeys(ref: ObjectRef, keys: readonly LedgerKey[]): Promise; upsert(ref: ObjectRef, key: LedgerKey, values: LedgerRow): Promise; deleteRow(ref: ObjectRef, key: LedgerKey): Promise; /** 放行写的原子性:草稿合并与事件登记同一事务 */ transaction(fn: (store: LedgerStore, drafts: DraftStore) => Promise): Promise; }; /** 任务级草稿层 —— 推荐形态 ledger.__draft;物理形态可换,功能约束不可变(§7.2) */ export type DraftStore = { list(taskId: string): Promise>; listForObject(taskId: string, ref: ObjectRef): Promise; put(taskId: string, ref: ObjectRef, entry: DraftEntry): Promise; clear(taskId: string): Promise; };