rust_store_core/command/
finalize.rs1use serde_json::Value;
4
5use crate::computes::{
6 process_node, run_async_fns, select_async_fns, strip_dep_injected, FnRegistry, InjectInfo,
7};
8use crate::permission::Context;
9use crate::pipeline::Ast;
10use crate::schema::Registry;
11
12fn postprocess_ast(postprocess: &Value) -> Result<Ast, String> {
14 let ast_v = postprocess
15 .get("ast")
16 .ok_or_else(|| "postprocess 缺少 ast".to_string())?;
17 Ast::from_value(ast_v)
18}
19
20pub fn prepare_query(
27 postprocess: &Value,
28 items: &mut [Value],
29 registry: &Registry,
30 fn_registry: Option<&dyn FnRegistry>,
31 ctx: Option<&Context>,
32) -> Result<Vec<String>, String> {
33 if postprocess.is_null() {
34 return Ok(Vec::new());
35 }
36 let mut ast = postprocess_ast(postprocess)?;
37 let schema = registry.get(&ast.model)?;
38
39 for item in items.iter_mut() {
41 process_node(
42 item,
43 &mut ast.fields,
44 &mut ast.relations,
45 schema,
46 ctx,
47 registry,
48 fn_registry,
49 )?;
50 }
51
52 Ok(select_async_fns(registry.role_rules(), schema, ctx)
54 .into_iter()
55 .map(|e| e.fn_ref)
56 .collect())
57}
58
59pub fn strip_query(postprocess: &Value, items: &mut [Value]) {
61 if postprocess.is_null() {
62 return;
63 }
64 if let Some(inj_v) = postprocess.get("inject") {
65 if !inj_v.is_null() {
66 let info = InjectInfo::from_value(inj_v);
67 strip_dep_injected(items, &info);
68 }
69 }
70}
71
72pub fn finalize_query(
85 postprocess: &Value,
86 items: &mut [Value],
87 registry: &Registry,
88 fn_registry: Option<&dyn FnRegistry>,
89 ctx: Option<&Context>,
90) -> Result<(), String> {
91 if postprocess.is_null() {
92 return Ok(());
93 }
94
95 prepare_query(postprocess, items, registry, fn_registry, ctx)?;
97
98 let ast = postprocess_ast(postprocess)?;
100 let schema = registry.get(&ast.model)?;
101 run_async_fns(items, registry.role_rules(), schema, ctx, fn_registry)?;
102
103 strip_query(postprocess, items);
105 Ok(())
106}