jeeflow dynamic table persist (DynamicTableWriter + PersistPostInterceptor + Meta)
moon new my-persist && cd my-persist
moon add mldong/jeeflow-core # engine + SPI + memory repository
moon add mldong/jeeflow-persist # this module: metadata-driven persist
moon add moonbitlang/async # async methods need it for async main// cmd/main/moon.pkg
import {
"mldong/jeeflow-core/json" @json,
"mldong/jeeflow-core/memory" @memory,
"mldong/jeeflow-core/spi" @spi,
"mldong/jeeflow-persist" @persist,
"moonbitlang/async",
}
pkgtype(kind: "executable")// cmd/main/main.mbt
fn as_json(row : Map[String, Json]) -> Json {
let obj = @json.empty_object()
for k, v in row {
@json.obj_set(obj, k, v)
}
obj
}
async fn main raise {
// 元数据=这张表怎么写库的唯一来源:一列一档
let provider = @persist.InMemoryMetaProvider::new()
let meta = @persist.TableMeta::make("expense", "报销", primary_key="expense_id")
meta.add_field(@persist.FieldMeta::make("process_instance_id", "BIGINT", "流程实例ID"))
// JSON 档:一个表单字段(对象/数组)收成主表的一列
meta.add_field(@persist.FieldMeta::make(
"detail_json", "TEXT", "明细", name="detail", storage_type=@persist.StorageType::Json,
))
// ONE2MANY 档:一个数组字段递归插子表,外键回落主表主键
meta.add_field(@persist.FieldMeta::make(
"items", "", "子项",
storage_type=@persist.StorageType::One2Many, target_table="expense_item",
))
provider.register(meta)
// 装饰器套在任意 DynamicTableWriter 外面:内存档验形状,真库档换 MysqlTableWriter
let sink = @persist.InMemoryTableWriter::new()
let writer = @persist.MetaTableWriter::make(sink, provider.as_provider())
let pk = writer.insert("expense", {
"process_instance_id": 7,
"detail": [ "上海", "北京" ],
"items": [ { "city": "上海" }, { "city": "北京" } ],
})
println("main pk=\{pk} row=\{@json.stringify(as_json(sink.get_row("expense", pk).unwrap()))}")
for _, row in sink.get_table_rows("expense_item") {
println("sub row=\{@json.stringify(as_json(row))}")
}
// 引擎侧装配:后置拦截器(order=100);节点级声明档下按类名挂
let repo = @memory.MemoryRepository::new()
let ctx = @spi.Ctx::new(repo, repo)
let itc = @persist.PersistPostInterceptor::make(provider, writer)
ctx.register_interceptor(itc.as_interceptor())
ctx.register_named_interceptor(@persist.PERSIST_CLASS_NAME, itc.as_interceptor())
println("interceptor wired")
}main pk=1 row={"process_instance_id":7,"detail_json":"[\"上海\",\"北京\"]","id":1}
sub row={"city":"上海","expense_id":1,"id":2}
sub row={"city":"北京","expense_id":1,"id":3}
interceptor wired{"persistMode": "ARCHIVE", "relTableName": "expense"}一句实话:PersistPostInterceptor 的两个字段都是必填,所以 Java 参考实现里 「未注入 writer ⇒ 从服务容器兜底、再找不到就静默跳过」那一档在本栈结构上不存在。 这不是免检:哪天把它改成可选字段或可选注册,spec/09 §4.6 的「未注入静默跳过 vs 显性报错」 判据必须连着补回来。
pub(open) trait DynamicTableWriter {
async fn insert(Self, String, Map[String, Json]) -> Int64 raise JeeflowError
async fn update(Self, String, Int64, Map[String, Json]) -> Unit raise JeeflowError
async fn exists(Self, String, Int64) -> Bool raise JeeflowError
async fn exists_by_key(Self, String, String, Int64) -> Bool raise JeeflowError
async fn update_by_key(Self, String, Map[String, Json], String, Int64) -> Unit raise JeeflowError
async fn columns(Self, String, Array[String]) -> Array[String] raise JeeflowError
}pub(all) struct DynamicMetaProviderFns {
load_table_meta : async (String) -> TableMeta? raise JeeflowError
}pub(all) struct DynamicTableReaderFns {
row_by_key : async (TableKeyQuery) -> Map[String, Json]? raise JeeflowError
rows_by_key : async (TableKeyQuery) -> Array[Map[String, Json]] raise JeeflowError
}pub(all) struct FieldMeta {
column_name : String
column_type : String
display_name : String
nullable : Bool
default_value : String?
permission : Int
name : String
storage_type : StorageType
expand_fields : Map[String, String]
target_table : String
foreign_key : String
}fn FieldMeta::make(column_name : String, column_type : String, display_name : String, name? : String, storage_type? : StorageType, expand_fields? : Map[String, String], target_table? : String, foreign_key? : String) -> FieldMetafn InMemoryMetaProvider::get_table_meta(self : InMemoryMetaProvider, table_name : String) -> TableMeta?impl DynamicTableWriter for InMemoryTableWriterasync fn columns(self : InMemoryTableWriter, _table_name : String, candidates : Array[String]) -> Array[String] raise JeeflowErrorasync fn exists(self : InMemoryTableWriter, table_name : String, id : Int64) -> Bool raise JeeflowErrorasync fn exists_by_key(self : InMemoryTableWriter, table_name : String, key : String, value : Int64) -> Bool raise JeeflowErrorasync fn insert(self : InMemoryTableWriter, table_name : String, data : Map[String, Json]) -> Int64 raise JeeflowErrorasync fn update(self : InMemoryTableWriter, table_name : String, id : Int64, data : Map[String, Json]) -> Unit raise JeeflowErrorasync fn update_by_key(self : InMemoryTableWriter, table_name : String, data : Map[String, Json], key : String, value : Int64) -> Unit raise JeeflowErrorasync fn InMemoryTableWriter::columns(self : InMemoryTableWriter, _table_name : String, candidates : Array[String]) -> Array[String] raise JeeflowErrorasync fn InMemoryTableWriter::exists(self : InMemoryTableWriter, table_name : String, id : Int64) -> Bool raise JeeflowErrorasync fn InMemoryTableWriter::exists_by_key(self : InMemoryTableWriter, table_name : String, key : String, value : Int64) -> Bool raise JeeflowErrorfn InMemoryTableWriter::get_row(self : InMemoryTableWriter, table_name : String, id : Int64) -> Map[String, Json]?fn InMemoryTableWriter::get_row_by_key(self : InMemoryTableWriter, table_name : String, key : String, value : Int64) -> Map[String, Json]?fn InMemoryTableWriter::get_table_rows(self : InMemoryTableWriter, table_name : String) -> Array[Map[String, Json]]async fn InMemoryTableWriter::insert(self : InMemoryTableWriter, table_name : String, data : Map[String, Json]) -> Int64 raise JeeflowErrorasync fn InMemoryTableWriter::update(self : InMemoryTableWriter, table_name : String, id : Int64, data : Map[String, Json]) -> Unit raise JeeflowErrorasync fn InMemoryTableWriter::update_by_key(self : InMemoryTableWriter, table_name : String, data : Map[String, Json], key : String, value : Int64) -> Unit raise JeeflowErrorfn MetaTableReader::as_biz_data_reader(self : MetaTableReader) -> (async (String, Int64) -> Map[String, Json]? raise JeeflowError)async fn MetaTableReader::assemble(self : MetaTableReader, meta : TableMeta, row : Map[String, Json]) -> Map[String, Json] raise JeeflowErrorfn MetaTableReader::make(meta : DynamicMetaProviderFns, reader : DynamicTableReaderFns) -> MetaTableReaderasync fn MetaTableReader::read_by_process_instance(self : MetaTableReader, table : String, instance_id : Int64) -> Map[String, Json]? raise JeeflowErrorimpl DynamicTableWriter for MetaTableWriterasync fn columns(self : MetaTableWriter, table : String, candidates : Array[String]) -> Array[String] raise JeeflowErrorasync fn exists_by_key(self : MetaTableWriter, table : String, key : String, value : Int64) -> Bool raise JeeflowErrorasync fn insert(self : MetaTableWriter, table : String, data : Map[String, Json]) -> Int64 raise JeeflowErrorasync fn update(self : MetaTableWriter, table : String, id : Int64, data : Map[String, Json]) -> Unit raise JeeflowErrorasync fn update_by_key(self : MetaTableWriter, table : String, data : Map[String, Json], key : String, value : Int64) -> Unit raise JeeflowErrorasync fn MetaTableWriter::columns(self : MetaTableWriter, table : String, candidates : Array[String]) -> Array[String] raise JeeflowErrorasync fn MetaTableWriter::exists(self : MetaTableWriter, table : String, id : Int64) -> Bool raise JeeflowErrorasync fn MetaTableWriter::exists_by_key(self : MetaTableWriter, table : String, key : String, value : Int64) -> Bool raise JeeflowErrorasync fn MetaTableWriter::insert(self : MetaTableWriter, table : String, data : Map[String, Json]) -> Int64 raise JeeflowErrorfn MetaTableWriter::make(base : &DynamicTableWriter, meta : DynamicMetaProviderFns) -> MetaTableWriterasync fn MetaTableWriter::update(self : MetaTableWriter, table : String, id : Int64, data : Map[String, Json]) -> Unit raise JeeflowErrorasync fn MetaTableWriter::update_by_key(self : MetaTableWriter, table : String, data : Map[String, Json], key : String, value : Int64) -> Unit raise JeeflowErrorpub(all) struct PersistPostInterceptor {
meta : DynamicMetaProviderFns
table_writer : &DynamicTableWriter
system_fields : SystemFields
strict_columns : Bool
}async fn PersistPostInterceptor::intercept(self : PersistPostInterceptor, exec : Execution) -> Unit raise JeeflowErrorfn PersistPostInterceptor::make(meta_provider : InMemoryMetaProvider, table_writer : &DynamicTableWriter) -> PersistPostInterceptorfn PersistPostInterceptor::make_from_provider(meta : DynamicMetaProviderFns, table_writer : &DynamicTableWriter) -> PersistPostInterceptorfn PersistPostInterceptor::with_strict_columns(self : PersistPostInterceptor, strict_columns : Bool) -> PersistPostInterceptorfn PersistPostInterceptor::with_system_fields(self : PersistPostInterceptor, system_fields : SystemFields) -> PersistPostInterceptorpub(all) struct SystemFields {
create_time_col : String?
create_user_col : String?
update_time_col : String?
update_user_col : String?
is_deleted_col : String?
default_user_value : String
}pub(all) struct TableKeyQuery {
table : String
column : String
value : Int64
}fn column_names_equal(a : String, b : String, strict? : Bool) -> Boolfn fill_system_fields_with(cfg : SystemFields, data : Map[String, Json], operator : String, is_insert : Bool) -> Unitfn normalize_column(name : String) -> StringInstall
Download zipjeeflow dynamic table persist (DynamicTableWriter + PersistPostInterceptor + Meta)