pub struct Registry { /* private fields */ }Implementations§
Source§impl Registry
impl Registry
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
14fn main() -> Result<(), String> {
15 // 1. A model is plain JSON — defined once, usable against every backend.
16 let mut registry = Registry::new();
17 registry.register(&json!({
18 "name": "Post",
19 "collection": "posts",
20 "timestamps": true,
21 "fields": {
22 "title": { "type": "string" },
23 "status": { "type": "string", "default": "draft" },
24 "views": { "type": "int" }
25 },
26 "relations": {}
27 }))?;
28
29 // 2. GQL in, MongoDB command JSON out. Values are referenced by `@key`.
30 let gql = "Post($condition:@c0,$sort:@s1,$limit:@l0){title, status, views}";
31 let mut params = Map::new();
32 params.insert("c0".to_string(), json!({ "status": "published" }));
33 params.insert("s1".to_string(), json!({ "views": -1 }));
34 params.insert("l0".to_string(), json!(20));
35
36 let plan = plan_query(gql, ¶ms, ®istry, None)?;
37
38 println!("collection = {}", plan.collection);
39 println!("mode = {}", plan.mode.as_str());
40
41 for cmd in &plan.commands {
42 println!("command = {}", serde_json::to_string(cmd).unwrap());
43
44 // 3. The host runs `cmd` against MongoDB — or, for a SQL backend, asks the
45 // core for parameterized SQL and binds it with its own driver.
46 let sql = translate(Backend::Postgres, cmd, ®istry)?;
47 println!("sql = {}", serde_json::to_string(&sql).unwrap());
48 }
49
50 Ok(())
51}Sourcepub fn register(&mut self, defn: &Value) -> Result<(), String>
pub fn register(&mut self, defn: &Value) -> Result<(), String>
注册一个 schema(含自动注册 <Name>Deleted 归档表),对应 JS register
Examples found in repository?
14fn main() -> Result<(), String> {
15 // 1. A model is plain JSON — defined once, usable against every backend.
16 let mut registry = Registry::new();
17 registry.register(&json!({
18 "name": "Post",
19 "collection": "posts",
20 "timestamps": true,
21 "fields": {
22 "title": { "type": "string" },
23 "status": { "type": "string", "default": "draft" },
24 "views": { "type": "int" }
25 },
26 "relations": {}
27 }))?;
28
29 // 2. GQL in, MongoDB command JSON out. Values are referenced by `@key`.
30 let gql = "Post($condition:@c0,$sort:@s1,$limit:@l0){title, status, views}";
31 let mut params = Map::new();
32 params.insert("c0".to_string(), json!({ "status": "published" }));
33 params.insert("s1".to_string(), json!({ "views": -1 }));
34 params.insert("l0".to_string(), json!(20));
35
36 let plan = plan_query(gql, ¶ms, ®istry, None)?;
37
38 println!("collection = {}", plan.collection);
39 println!("mode = {}", plan.mode.as_str());
40
41 for cmd in &plan.commands {
42 println!("command = {}", serde_json::to_string(cmd).unwrap());
43
44 // 3. The host runs `cmd` against MongoDB — or, for a SQL backend, asks the
45 // core for parameterized SQL and binds it with its own driver.
46 let sql = translate(Backend::Postgres, cmd, ®istry)?;
47 println!("sql = {}", serde_json::to_string(&sql).unwrap());
48 }
49
50 Ok(())
51}pub fn has(&self, name: &str) -> bool
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
清空 schema 注册表(schemas + order),对应绑定层的测试隔离 / 动态重建场景。
只清 schema,不动配置开关(require_context / profile / rbac /
role_rules)与回调表(clear_fns 对称:各清各的)——开关生命周期属
Registry 配置面,不随 schema 集合重建而丢。
Sourcepub fn set_require_context(&mut self, require: bool)
pub fn set_require_context(&mut self, require: bool)
开关「上下文强制」(默认关闭 = fail-open,保持 JS parity)。
开启后:plan 入口遇 ctx: None 报 ERR_NO_CONTEXT(fail-secure)。
内部调用须显式传系统上下文({"internal": true})。
Sourcepub fn require_context(&self) -> bool
pub fn require_context(&self) -> bool
「上下文强制」开关当前值
Sourcepub fn set_profile(&mut self, profile: Profile)
pub fn set_profile(&mut self, profile: Profile)
设置查询档位(standard / text2query)。
档位为单值状态(非栈):由 Host 的 text2query() 上下文管理器负责
进入时设档、退出时恢复(见 py-store / nodejs-store 门面)。判决一律在 core。
Sourcepub fn set_exempt_roles(&mut self, roles: Vec<String>)
pub fn set_exempt_roles(&mut self, roles: Vec<String>)
豁免角色清单:命中者在一切判决环节(静态 + RBAC)直接放行。默认空——无豁免。
Sourcepub fn set_deny_write_roles(&mut self, roles: Vec<String>)
pub fn set_deny_write_roles(&mut self, roles: Vec<String>)
拒写角色清单:命中者一切写路径拒绝(读不受影响)。默认空——无拒写。
Sourcepub fn set_unconfigured_policy(&mut self, policy: UnconfiguredPolicy)
pub fn set_unconfigured_policy(&mut self, policy: UnconfiguredPolicy)
schema 白名单缺失/为空时的默认姿态。默认 Open(保持现状语义)。
Sourcepub fn role_rules(&self) -> &RoleRules
pub fn role_rules(&self) -> &RoleRules
当前角色规则(静态判决函数与 RBAC decide 的共用取参入口)
Sourcepub fn set_rbac(&mut self, policy: Option<&Value>) -> Result<(), String>
pub fn set_rbac(&mut self, policy: Option<&Value>) -> Result<(), String>
注入/清除 RBAC 动态策略;None = 关闭(判决原语直通)。
解析失败 Err(fail-fast,禁静默吞配置错误),成功后判决链路即刻生效。
Sourcepub fn rbac(&self) -> Option<&RbacPolicy>
pub fn rbac(&self) -> Option<&RbacPolicy>
当前 RBAC 策略(None = 未启用)
Sourcepub fn get_by_location(
&self,
source: &str,
namespace: Option<&str>,
collection: &str,
) -> Result<&Schema, String>
pub fn get_by_location( &self, source: &str, namespace: Option<&str>, collection: &str, ) -> Result<&Schema, String>
按定位三元组精确获取 schema(命令路由的唯一定位入口)
(source, namespace, collection) 三元组在 Registry 内唯一(注册期校验),
故此处零候选 = 未注册;一候选 = 精确命中。不做任何猜测回落。
Sourcepub fn get_for_command(
&self,
source: &str,
namespace: Option<&str>,
collection: &str,
) -> Result<&Schema, String>
pub fn get_for_command( &self, source: &str, namespace: Option<&str>, collection: &str, ) -> Result<&Schema, String>
命令结构定位(方言翻译用):优先三元组精确命中;未命中时(route_override
改写 namespace 的多租户场景)回落 (source, collection) 定位 —— 结构 schema
与定位 namespace 正交(见 multi-datasource-routing-plan.md §6:权限/字段
校验仍按结构 schema)。
(source, collection) 恰一候选 → 命中;多候选时取 namespace = null 的
结构声明,无 null 声明 → 报错(拒绝猜测,铁律 3)。
pub fn list(&self) -> Vec<String>
Sourcepub fn schema_datasource(&self, name: &str) -> Result<Option<String>, String>
pub fn schema_datasource(&self, name: &str) -> Result<Option<String>, String>
schema 声明的数据源名(未声明 → None,语义为 default)
Sourcepub fn resolve_datasource(
&self,
schema_name: &str,
config: &Value,
) -> Result<DataSource, String>
pub fn resolve_datasource( &self, schema_name: &str, config: &Value, ) -> Result<DataSource, String>
解析 schema 绑定的数据源(config 为 { "sources": { name: kind } })
纯逻辑:不持有连接(连接句柄留在 Host)。null / 空配置下等价单源 Mongo,
保证既有调用方与 parity fixture 零变更。见 crate::datasource。