added realm to jspg processing

This commit is contained in:
2026-04-17 18:25:14 -04:00
parent 8ebf6a69bf
commit f450f8ab8b
59 changed files with 3884 additions and 2194 deletions

View File

@ -52,7 +52,7 @@ impl Schema {
// 1. Resolve INHERITANCE dependencies first
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &self.obj.type_ {
if !crate::database::object::is_primitive_type(t) {
if let Some(parent) = db.schemas.get(t) {
if let Some(parent) = db.get_scoped_schema(crate::database::realm::SchemaRealm::Type, t) {
parent.as_ref().compile(db, t, t.clone(), errors);
if let Some(p_props) = parent.obj.compiled_properties.get() {
props.extend(p_props.clone());
@ -86,7 +86,7 @@ impl Schema {
for t in types {
if !crate::database::object::is_primitive_type(t) {
if let Some(parent) = db.schemas.get(t) {
if let Some(parent) = db.get_scoped_schema(crate::database::realm::SchemaRealm::Type, t) {
parent.as_ref().compile(db, t, t.clone(), errors);
}
}

View File

@ -29,7 +29,7 @@ impl Schema {
format!("{}.{}", family_prefix, var)
};
if db.schemas.contains_key(&target_id) {
if db.get_scoped_schema(crate::database::realm::SchemaRealm::Type, &target_id).is_some() {
options.insert(var.to_string(), (None, Some(target_id)));
}
}

View File

@ -6,12 +6,11 @@ pub mod formats;
pub mod object;
pub mod page;
pub mod punc;
pub mod realm;
pub mod relation;
pub mod schema;
pub mod r#type;
// External mock exports inside the executor sub-folder
use r#enum::Enum;
use executors::DatabaseExecutor;
@ -22,6 +21,7 @@ use executors::pgrx::SpiExecutor;
use executors::mock::MockExecutor;
use punc::Punc;
use realm::SchemaRealm;
use relation::Relation;
use schema::Schema;
use serde_json::Value;
@ -36,8 +36,6 @@ pub struct Database {
pub puncs: HashMap<String, Punc>,
pub relations: HashMap<String, Relation>,
#[serde(skip)]
pub schemas: HashMap<String, Arc<Schema>>,
#[serde(skip)]
pub executor: Box<dyn DatabaseExecutor + Send + Sync>,
}
@ -48,7 +46,6 @@ impl Database {
types: HashMap::new(),
relations: HashMap::new(),
puncs: HashMap::new(),
schemas: HashMap::new(),
#[cfg(not(test))]
executor: Box::new(SpiExecutor::new()),
#[cfg(test)]
@ -157,26 +154,6 @@ impl Database {
}
}
if let Some(map) = val.get("schemas").and_then(|v| v.as_object()) {
for (key, item) in map.iter() {
match serde_json::from_value::<Schema>(item.clone()) {
Ok(schema) => {
db.schemas.insert(key.clone(), Arc::new(schema));
}
Err(e) => {
errors.push(crate::drop::Error {
code: "DATABASE_SCHEMA_PARSE_FAILED".to_string(),
message: format!("Failed to parse database schema key '{}': {}", key, e),
details: crate::drop::ErrorDetails {
context: Some(serde_json::json!(key)),
..Default::default()
},
});
}
}
}
}
db.compile(&mut errors);
let drop = if errors.is_empty() {
crate::drop::Drop::success()
@ -213,30 +190,26 @@ impl Database {
}
pub fn compile(&mut self, errors: &mut Vec<crate::drop::Error>) {
// Collect existing schemas patched in the databse
let mut harvested = Vec::new();
for (id, schema_arc) in &self.schemas {
crate::database::schema::Schema::collect_schemas(
schema_arc,
id,
id.clone(),
&mut harvested,
errors,
);
}
for (id, schema_arc) in harvested {
self.schemas.insert(id, schema_arc);
}
self.collect_schemas(errors);
// Mathematically evaluate all property inheritances, formats, schemas, and foreign key edges topographically over OnceLocks
for (id, schema_arc) in &self.schemas {
// First compile pass initializes exact structural root_id mapping to resolve DB constraints
let root_id = id.split('/').next().unwrap_or(id);
schema_arc
.as_ref()
.compile(self, root_id, id.clone(), errors);
// Formally evaluate properties with strict 3-pass Ordered Graph execution natively
for (_, enum_def) in &self.enums {
for (schema_id, schema_arc) in &enum_def.schemas {
let root_id = schema_id.split('/').next().unwrap_or(schema_id);
schema_arc.as_ref().compile(self, root_id, schema_id.clone(), errors);
}
}
for (_, type_def) in &self.types {
for (schema_id, schema_arc) in &type_def.schemas {
let root_id = schema_id.split('/').next().unwrap_or(schema_id);
schema_arc.as_ref().compile(self, root_id, schema_id.clone(), errors);
}
}
for (_, punc_def) in &self.puncs {
for (schema_id, schema_arc) in &punc_def.schemas {
let root_id = schema_id.split('/').next().unwrap_or(schema_id);
schema_arc.as_ref().compile(self, root_id, schema_id.clone(), errors);
}
}
// Phase 2: Synthesize Composed Filter References
@ -260,16 +233,15 @@ impl Database {
let mut filter_ids = Vec::new();
for (type_name, id, filter_arc) in filter_schemas {
filter_ids.push(id.clone());
self.schemas.insert(id.clone(), filter_arc.clone());
filter_ids.push((type_name.clone(), id.clone()));
if let Some(t) = self.types.get_mut(&type_name) {
t.schemas.insert(id, filter_arc);
}
}
// Now actively compile the newly injected filters to lock all nested compose references natively
for id in filter_ids {
if let Some(filter_arc) = self.schemas.get(&id).cloned() {
for (type_name, id) in filter_ids {
if let Some(filter_arc) = self.types.get(&type_name).and_then(|t| t.schemas.get(&id)).cloned() {
let root_id = id.split('/').next().unwrap_or(&id);
filter_arc
.as_ref()
@ -282,13 +254,11 @@ impl Database {
let mut type_insert = Vec::new();
let mut punc_insert = Vec::new();
let mut enum_insert = Vec::new();
let mut global_insert = Vec::new();
// Pass 1: Extract all Schemas structurally off top level definitions into the master registry.
// Validate every node recursively via string filters natively!
for (type_name, type_def) in &self.types {
for (id, schema_arc) in &type_def.schemas {
global_insert.push((id.clone(), Arc::clone(schema_arc)));
let mut local_insert = Vec::new();
crate::database::schema::Schema::collect_schemas(
schema_arc,
@ -299,14 +269,12 @@ impl Database {
);
for entry in &local_insert {
type_insert.push((type_name.clone(), entry.0.clone(), Arc::clone(&entry.1)));
global_insert.push((entry.0.clone(), Arc::clone(&entry.1)));
}
}
}
for (punc_name, punc_def) in &self.puncs {
for (id, schema_arc) in &punc_def.schemas {
global_insert.push((id.clone(), Arc::clone(schema_arc)));
let mut local_insert = Vec::new();
crate::database::schema::Schema::collect_schemas(
schema_arc,
@ -317,14 +285,12 @@ impl Database {
);
for entry in &local_insert {
punc_insert.push((punc_name.clone(), entry.0.clone(), Arc::clone(&entry.1)));
global_insert.push((entry.0.clone(), Arc::clone(&entry.1)));
}
}
}
for (enum_name, enum_def) in &self.enums {
for (id, schema_arc) in &enum_def.schemas {
global_insert.push((id.clone(), Arc::clone(schema_arc)));
let mut local_insert = Vec::new();
crate::database::schema::Schema::collect_schemas(
schema_arc,
@ -335,16 +301,10 @@ impl Database {
);
for entry in &local_insert {
enum_insert.push((enum_name.clone(), entry.0.clone(), Arc::clone(&entry.1)));
global_insert.push((entry.0.clone(), Arc::clone(&entry.1)));
}
}
}
// Apply global inserts
for (id, schema_arc) in global_insert {
self.schemas.insert(id, schema_arc);
}
// Apply local scopes
for (origin_name, id, schema_arc) in type_insert {
if let Some(t) = self.types.get_mut(&origin_name) {
@ -362,6 +322,40 @@ impl Database {
}
}
}
pub fn get_scoped_schema(&self, realm: SchemaRealm, schema_id: &str) -> Option<Arc<Schema>> {
// Punc Realm natively maps mathematically to `.request` and `.response` shapes
if realm == SchemaRealm::Punc {
if schema_id.ends_with(".request") || schema_id.ends_with(".response") {
let punc_name = schema_id
.trim_end_matches(".request")
.trim_end_matches(".response");
return self.puncs.get(punc_name).and_then(|p| p.schemas.get(schema_id).cloned());
}
}
let clean_id = schema_id.trim_end_matches(".filter");
let root_id = clean_id.split('/').next().unwrap_or(clean_id);
let base_name = root_id.split('.').next_back().unwrap_or(root_id);
// Puncs and Types can lookup Table boundaries
if realm == SchemaRealm::Type || realm == SchemaRealm::Punc {
if let Some(type_def) = self.types.get(base_name) {
if let Some(schema) = type_def.schemas.get(schema_id) {
return Some(schema.clone());
}
}
}
// All realms can intrinsically look up enumerations
if let Some(enum_def) = self.enums.get(base_name) {
if let Some(schema) = enum_def.schemas.get(schema_id) {
return Some(schema.clone());
}
}
None
}
/// Inspects the Postgres pg_constraint relations catalog to securely identify
/// the precise Foreign Key connecting a parent and child hierarchy path.

6
src/database/realm.rs Normal file
View File

@ -0,0 +1,6 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SchemaRealm {
Enum,
Type,
Punc,
}