Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9bdb767685 | |||
| bdd89fe695 | |||
| 8135d80045 | |||
| 9255439d53 | |||
| 9038607729 | |||
| 9f6c27c3b8 | |||
| 75aac41362 | |||
| dbcef42401 | |||
| b6c5561d2f | |||
| e01b778d68 | |||
| 6eb134c0d6 | |||
| 7ccc4b7cce | |||
| 77bfa4cd18 |
@ -143,7 +143,8 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"lookup_fields": [],
|
"lookup_fields": [],
|
||||||
"historical": false,
|
"historical": true,
|
||||||
|
"notify": true,
|
||||||
"relationship": false
|
"relationship": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -191,6 +192,7 @@
|
|||||||
},
|
},
|
||||||
"lookup_fields": [],
|
"lookup_fields": [],
|
||||||
"historical": true,
|
"historical": true,
|
||||||
|
"notify": true,
|
||||||
"relationship": false
|
"relationship": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -239,6 +241,7 @@
|
|||||||
},
|
},
|
||||||
"lookup_fields": [],
|
"lookup_fields": [],
|
||||||
"historical": true,
|
"historical": true,
|
||||||
|
"notify": true,
|
||||||
"relationship": false
|
"relationship": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -341,6 +344,7 @@
|
|||||||
"pronouns"
|
"pronouns"
|
||||||
],
|
],
|
||||||
"historical": true,
|
"historical": true,
|
||||||
|
"notify": true,
|
||||||
"relationship": false
|
"relationship": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -395,6 +399,7 @@
|
|||||||
"id"
|
"id"
|
||||||
],
|
],
|
||||||
"historical": true,
|
"historical": true,
|
||||||
|
"notify": true,
|
||||||
"relationship": false
|
"relationship": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -452,6 +457,7 @@
|
|||||||
},
|
},
|
||||||
"lookup_fields": [],
|
"lookup_fields": [],
|
||||||
"historical": true,
|
"historical": true,
|
||||||
|
"notify": true,
|
||||||
"relationship": false
|
"relationship": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -515,7 +521,8 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"lookup_fields": [],
|
"lookup_fields": [],
|
||||||
"historical": true
|
"historical": true,
|
||||||
|
"notify": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "contact",
|
"name": "contact",
|
||||||
@ -588,7 +595,8 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"lookup_fields": [],
|
"lookup_fields": [],
|
||||||
"historical": true
|
"historical": true,
|
||||||
|
"notify": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phone_number",
|
"name": "phone_number",
|
||||||
@ -646,6 +654,7 @@
|
|||||||
],
|
],
|
||||||
"lookup_fields": [],
|
"lookup_fields": [],
|
||||||
"historical": true,
|
"historical": true,
|
||||||
|
"notify": true,
|
||||||
"relationship": false
|
"relationship": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -704,6 +713,98 @@
|
|||||||
],
|
],
|
||||||
"lookup_fields": [],
|
"lookup_fields": [],
|
||||||
"historical": true,
|
"historical": true,
|
||||||
|
"notify": true,
|
||||||
|
"relationship": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "attachment",
|
||||||
|
"schemas": [
|
||||||
|
{
|
||||||
|
"$id": "type_metadata",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"type": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$id": "other_metadata",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"other": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$id": "attachment",
|
||||||
|
"$ref": "entity",
|
||||||
|
"properties": {
|
||||||
|
"flags": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"type_metadata": {
|
||||||
|
"$ref": "type_metadata"
|
||||||
|
},
|
||||||
|
"other_metadata": {
|
||||||
|
"$ref": "other_metadata"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"hierarchy": [
|
||||||
|
"attachment",
|
||||||
|
"entity"
|
||||||
|
],
|
||||||
|
"fields": [
|
||||||
|
"id",
|
||||||
|
"type",
|
||||||
|
"flags",
|
||||||
|
"type_metadata",
|
||||||
|
"other_metadata",
|
||||||
|
"created_at",
|
||||||
|
"created_by",
|
||||||
|
"modified_at",
|
||||||
|
"modified_by",
|
||||||
|
"archived"
|
||||||
|
],
|
||||||
|
"grouped_fields": {
|
||||||
|
"attachment": [
|
||||||
|
"id",
|
||||||
|
"type",
|
||||||
|
"flags",
|
||||||
|
"type_metadata",
|
||||||
|
"other_metadata"
|
||||||
|
],
|
||||||
|
"entity": [
|
||||||
|
"id",
|
||||||
|
"type",
|
||||||
|
"created_at",
|
||||||
|
"created_by",
|
||||||
|
"modified_at",
|
||||||
|
"modified_by",
|
||||||
|
"archived"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"field_types": {
|
||||||
|
"id": "uuid",
|
||||||
|
"type": "text",
|
||||||
|
"flags": "_text",
|
||||||
|
"type_metadata": "jsonb",
|
||||||
|
"other_metadata": "jsonb",
|
||||||
|
"created_at": "timestamptz",
|
||||||
|
"created_by": "uuid",
|
||||||
|
"modified_at": "timestamptz",
|
||||||
|
"modified_by": "uuid",
|
||||||
|
"archived": "boolean"
|
||||||
|
},
|
||||||
|
"lookup_fields": [],
|
||||||
|
"historical": true,
|
||||||
|
"notify": true,
|
||||||
"relationship": false
|
"relationship": false
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -2180,6 +2281,108 @@
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"description": "Attachment with text[] and jsonb metadata structures",
|
||||||
|
"action": "merge",
|
||||||
|
"data": {
|
||||||
|
"type": "attachment",
|
||||||
|
"flags": [
|
||||||
|
"urgent",
|
||||||
|
"reviewed"
|
||||||
|
],
|
||||||
|
"other_metadata": {
|
||||||
|
"other": "hello"
|
||||||
|
},
|
||||||
|
"type_metadata": {
|
||||||
|
"type": "type_metadata"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"expect": {
|
||||||
|
"success": true,
|
||||||
|
"sql": [
|
||||||
|
[
|
||||||
|
"INSERT INTO agreego.\"entity\" (",
|
||||||
|
" \"created_at\",",
|
||||||
|
" \"created_by\",",
|
||||||
|
" \"id\",",
|
||||||
|
" \"modified_at\",",
|
||||||
|
" \"modified_by\",",
|
||||||
|
" \"type\"",
|
||||||
|
")",
|
||||||
|
"VALUES (",
|
||||||
|
" '{{timestamp}}',",
|
||||||
|
" '00000000-0000-0000-0000-000000000000',",
|
||||||
|
" '{{uuid:attachment_id}}',",
|
||||||
|
" '{{timestamp}}',",
|
||||||
|
" '00000000-0000-0000-0000-000000000000',",
|
||||||
|
" 'attachment'",
|
||||||
|
")"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"INSERT INTO agreego.\"attachment\" (",
|
||||||
|
" \"flags\",",
|
||||||
|
" \"id\",",
|
||||||
|
" \"other_metadata\",",
|
||||||
|
" \"type\",",
|
||||||
|
" \"type_metadata\"",
|
||||||
|
")",
|
||||||
|
"VALUES (",
|
||||||
|
" '{\"urgent\",\"reviewed\"}',",
|
||||||
|
" '{{uuid:attachment_id}}',",
|
||||||
|
" '{\"other\":\"hello\"}',",
|
||||||
|
" 'attachment',",
|
||||||
|
" '{\"type\":\"type_metadata\"}'",
|
||||||
|
")"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"INSERT INTO agreego.change (",
|
||||||
|
" \"old\",",
|
||||||
|
" \"new\",",
|
||||||
|
" entity_id,",
|
||||||
|
" id,",
|
||||||
|
" kind,",
|
||||||
|
" modified_at,",
|
||||||
|
" modified_by",
|
||||||
|
")",
|
||||||
|
"VALUES (",
|
||||||
|
" NULL,",
|
||||||
|
" '{",
|
||||||
|
" \"flags\":[\"urgent\",\"reviewed\"],",
|
||||||
|
" \"other_metadata\":{\"other\":\"hello\"},",
|
||||||
|
" \"type\":\"attachment\",",
|
||||||
|
" \"type_metadata\":{\"type\":\"type_metadata\"}",
|
||||||
|
" }',",
|
||||||
|
" '{{uuid:attachment_id}}',",
|
||||||
|
" '{{uuid}}',",
|
||||||
|
" 'create',",
|
||||||
|
" '{{timestamp}}',",
|
||||||
|
" '00000000-0000-0000-0000-000000000000'",
|
||||||
|
")"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"SELECT pg_notify('entity', '{",
|
||||||
|
" \"complete\":{",
|
||||||
|
" \"created_at\":\"{{timestamp}}\",",
|
||||||
|
" \"created_by\":\"00000000-0000-0000-0000-000000000000\",",
|
||||||
|
" \"flags\":[\"urgent\",\"reviewed\"],",
|
||||||
|
" \"id\":\"{{uuid:attachment_id}}\",",
|
||||||
|
" \"modified_at\":\"{{timestamp}}\",",
|
||||||
|
" \"modified_by\":\"00000000-0000-0000-0000-000000000000\",",
|
||||||
|
" \"other_metadata\":{\"other\":\"hello\"},",
|
||||||
|
" \"type\":\"attachment\",",
|
||||||
|
" \"type_metadata\":{\"type\":\"type_metadata\"}",
|
||||||
|
" },",
|
||||||
|
" \"new\":{",
|
||||||
|
" \"flags\":[\"urgent\",\"reviewed\"],",
|
||||||
|
" \"other_metadata\":{\"other\":\"hello\"},",
|
||||||
|
" \"type\":\"attachment\",",
|
||||||
|
" \"type_metadata\":{\"type\":\"type_metadata\"}",
|
||||||
|
" }",
|
||||||
|
" }')"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,6 +23,7 @@ use relation::Relation;
|
|||||||
use schema::Schema;
|
use schema::Schema;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
use std::sync::Arc;
|
||||||
use r#type::Type;
|
use r#type::Type;
|
||||||
|
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
@ -310,12 +311,84 @@ impl Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn compile_schemas(&mut self) {
|
fn compile_schemas(&mut self) {
|
||||||
// Pass 3: compile_internals across pure structure
|
|
||||||
let schema_ids: Vec<String> = self.schemas.keys().cloned().collect();
|
let schema_ids: Vec<String> = self.schemas.keys().cloned().collect();
|
||||||
|
let mut compiled_names_map: HashMap<String, Vec<String>> = HashMap::new();
|
||||||
|
let mut compiled_props_map: HashMap<String, std::collections::BTreeMap<String, Arc<Schema>>> =
|
||||||
|
HashMap::new();
|
||||||
|
|
||||||
|
for id in &schema_ids {
|
||||||
|
if let Some(schema) = self.schemas.get(id) {
|
||||||
|
let mut visited = HashSet::new();
|
||||||
|
let merged = self.merged_properties(schema, &mut visited);
|
||||||
|
let mut names: Vec<String> = merged.keys().cloned().collect();
|
||||||
|
if !names.is_empty() {
|
||||||
|
names.sort();
|
||||||
|
compiled_names_map.insert(id.clone(), names);
|
||||||
|
compiled_props_map.insert(id.clone(), merged);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for id in schema_ids {
|
for id in schema_ids {
|
||||||
if let Some(schema) = self.schemas.get_mut(&id) {
|
if let Some(schema) = self.schemas.get_mut(&id) {
|
||||||
|
if let Some(names) = compiled_names_map.remove(&id) {
|
||||||
|
schema.obj.compiled_property_names = Some(names);
|
||||||
|
}
|
||||||
|
if let Some(props) = compiled_props_map.remove(&id) {
|
||||||
|
schema.obj.compiled_properties = Some(props);
|
||||||
|
}
|
||||||
schema.compile_internals();
|
schema.compile_internals();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn merged_properties(
|
||||||
|
&self,
|
||||||
|
schema: &Schema,
|
||||||
|
visited: &mut HashSet<String>,
|
||||||
|
) -> std::collections::BTreeMap<String, Arc<Schema>> {
|
||||||
|
if let Some(props) = &schema.obj.compiled_properties {
|
||||||
|
return props.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut props = std::collections::BTreeMap::new();
|
||||||
|
|
||||||
|
if let Some(id) = &schema.obj.id {
|
||||||
|
if !visited.insert(id.clone()) {
|
||||||
|
return props;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ref_id) = &schema.obj.r#ref {
|
||||||
|
if let Some(parent_schema) = self.schemas.get(ref_id) {
|
||||||
|
props.extend(self.merged_properties(parent_schema, visited));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(all_of) = &schema.obj.all_of {
|
||||||
|
for ao in all_of {
|
||||||
|
props.extend(self.merged_properties(ao, visited));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(then_schema) = &schema.obj.then_ {
|
||||||
|
props.extend(self.merged_properties(then_schema, visited));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(else_schema) = &schema.obj.else_ {
|
||||||
|
props.extend(self.merged_properties(else_schema, visited));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(local_props) = &schema.obj.properties {
|
||||||
|
for (k, v) in local_props {
|
||||||
|
props.insert(k.clone(), v.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(id) = &schema.obj.id {
|
||||||
|
visited.remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
props
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -167,6 +167,13 @@ pub struct SchemaObject {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub extensible: Option<bool>,
|
pub extensible: Option<bool>,
|
||||||
|
|
||||||
|
#[serde(rename = "compiledProperties")]
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub compiled_property_names: Option<Vec<String>>,
|
||||||
|
|
||||||
|
#[serde(skip)]
|
||||||
|
pub compiled_properties: Option<BTreeMap<String, Arc<Schema>>>,
|
||||||
|
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub compiled_format: Option<CompiledFormat>,
|
pub compiled_format: Option<CompiledFormat>,
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
|
|||||||
@ -15,6 +15,8 @@ pub struct Type {
|
|||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub historical: bool,
|
pub historical: bool,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
pub notify: bool,
|
||||||
|
#[serde(default)]
|
||||||
pub sensitive: bool,
|
pub sensitive: bool,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub ownable: bool,
|
pub ownable: bool,
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
pub mod cache;
|
pub mod cache;
|
||||||
|
|
||||||
|
use crate::database::r#type::Type;
|
||||||
use crate::database::Database;
|
use crate::database::Database;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -175,7 +176,7 @@ impl Merger {
|
|||||||
|
|
||||||
// Attempt to extract relative object type name
|
// Attempt to extract relative object type name
|
||||||
let relative_type_name = match relative.get("type").and_then(|v| v.as_str()) {
|
let relative_type_name = match relative.get("type").and_then(|v| v.as_str()) {
|
||||||
Some(t) => t,
|
Some(t) => t.to_string(),
|
||||||
None => continue,
|
None => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -184,7 +185,7 @@ impl Merger {
|
|||||||
// Call central Database O(1) graph logic
|
// Call central Database O(1) graph logic
|
||||||
let relative_relation = self.db.get_relation(
|
let relative_relation = self.db.get_relation(
|
||||||
&type_def.name,
|
&type_def.name,
|
||||||
relative_type_name,
|
&relative_type_name,
|
||||||
&relation_name,
|
&relation_name,
|
||||||
Some(&relative_keys),
|
Some(&relative_keys),
|
||||||
);
|
);
|
||||||
@ -199,11 +200,16 @@ impl Merger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let merged_relative = match self.merge_internal(Value::Object(relative), notifications)? {
|
let mut merged_relative = match self.merge_internal(Value::Object(relative), notifications)? {
|
||||||
Value::Object(m) => m,
|
Value::Object(m) => m,
|
||||||
_ => continue,
|
_ => continue,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
merged_relative.insert(
|
||||||
|
"type".to_string(),
|
||||||
|
Value::String(relative_type_name),
|
||||||
|
);
|
||||||
|
|
||||||
Self::apply_entity_relation(
|
Self::apply_entity_relation(
|
||||||
&mut entity_fields,
|
&mut entity_fields,
|
||||||
&relation.source_columns,
|
&relation.source_columns,
|
||||||
@ -321,8 +327,9 @@ impl Merger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7. Perform change tracking
|
// 7. Perform change tracking dynamically suppressing noise based on type bounds!
|
||||||
let notify_sql = self.merge_entity_change(
|
let notify_sql = self.merge_entity_change(
|
||||||
|
type_def,
|
||||||
&entity_fields,
|
&entity_fields,
|
||||||
entity_fetched.as_ref(),
|
entity_fetched.as_ref(),
|
||||||
entity_change_kind.as_deref(),
|
entity_change_kind.as_deref(),
|
||||||
@ -620,11 +627,7 @@ impl Merger {
|
|||||||
for key in &sorted_keys {
|
for key in &sorted_keys {
|
||||||
columns.push(format!("\"{}\"", key));
|
columns.push(format!("\"{}\"", key));
|
||||||
let val = entity_pairs.get(key).unwrap();
|
let val = entity_pairs.get(key).unwrap();
|
||||||
if val.as_str() == Some("") {
|
values.push(Self::format_sql_value(val, key, entity_type));
|
||||||
values.push("NULL".to_string());
|
|
||||||
} else {
|
|
||||||
values.push(Self::quote_literal(val));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if columns.is_empty() {
|
if columns.is_empty() {
|
||||||
@ -658,7 +661,11 @@ impl Merger {
|
|||||||
if val.as_str() == Some("") {
|
if val.as_str() == Some("") {
|
||||||
set_clauses.push(format!("\"{}\" = NULL", key));
|
set_clauses.push(format!("\"{}\" = NULL", key));
|
||||||
} else {
|
} else {
|
||||||
set_clauses.push(format!("\"{}\" = {}", key, Self::quote_literal(val)));
|
set_clauses.push(format!(
|
||||||
|
"\"{}\" = {}",
|
||||||
|
key,
|
||||||
|
Self::format_sql_value(val, key, entity_type)
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -680,6 +687,7 @@ impl Merger {
|
|||||||
|
|
||||||
fn merge_entity_change(
|
fn merge_entity_change(
|
||||||
&self,
|
&self,
|
||||||
|
type_obj: &Type,
|
||||||
entity_fields: &serde_json::Map<String, Value>,
|
entity_fields: &serde_json::Map<String, Value>,
|
||||||
entity_fetched: Option<&serde_json::Map<String, Value>>,
|
entity_fetched: Option<&serde_json::Map<String, Value>>,
|
||||||
entity_change_kind: Option<&str>,
|
entity_change_kind: Option<&str>,
|
||||||
@ -760,6 +768,8 @@ impl Merger {
|
|||||||
notification.insert("old".to_string(), old_val_obj.clone());
|
notification.insert("old".to_string(), old_val_obj.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut notify_sql = None;
|
||||||
|
if type_obj.historical {
|
||||||
let change_sql = format!(
|
let change_sql = format!(
|
||||||
"INSERT INTO agreego.change (\"old\", \"new\", entity_id, id, kind, modified_at, modified_by) VALUES ({}, {}, {}, {}, {}, {}, {})",
|
"INSERT INTO agreego.change (\"old\", \"new\", entity_id, id, kind, modified_at, modified_by) VALUES ({}, {}, {}, {}, {}, {}, {})",
|
||||||
Self::quote_literal(&old_val_obj),
|
Self::quote_literal(&old_val_obj),
|
||||||
@ -771,17 +781,20 @@ impl Merger {
|
|||||||
Self::quote_literal(&Value::String(user_id.to_string()))
|
Self::quote_literal(&Value::String(user_id.to_string()))
|
||||||
);
|
);
|
||||||
|
|
||||||
let notify_sql = format!(
|
|
||||||
"SELECT pg_notify('entity', {})",
|
|
||||||
Self::quote_literal(&Value::String(Value::Object(notification).to_string()))
|
|
||||||
);
|
|
||||||
|
|
||||||
self
|
self
|
||||||
.db
|
.db
|
||||||
.execute(&change_sql, None)
|
.execute(&change_sql, None)
|
||||||
.map_err(|e| format!("Executor Error in change: {:?}", e))?;
|
.map_err(|e| format!("Executor Error in change: {:?}", e))?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Some(notify_sql))
|
if type_obj.notify {
|
||||||
|
notify_sql = Some(format!(
|
||||||
|
"SELECT pg_notify('entity', {})",
|
||||||
|
Self::quote_literal(&Value::String(Value::Object(notification).to_string()))
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(notify_sql)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compare_entities(
|
fn compare_entities(
|
||||||
@ -833,6 +846,34 @@ impl Merger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn format_sql_value(val: &Value, key: &str, entity_type: &Type) -> String {
|
||||||
|
if val.as_str() == Some("") {
|
||||||
|
return "NULL".to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut is_pg_array = false;
|
||||||
|
if let Some(field_types_map) = entity_type.field_types.as_ref().and_then(|v| v.as_object()) {
|
||||||
|
if let Some(t_val) = field_types_map.get(key) {
|
||||||
|
if let Some(t_str) = t_val.as_str() {
|
||||||
|
if t_str.starts_with('_') {
|
||||||
|
is_pg_array = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_pg_array && val.is_array() {
|
||||||
|
let mut s = val.to_string();
|
||||||
|
if s.starts_with('[') && s.ends_with(']') {
|
||||||
|
s.replace_range(0..1, "{");
|
||||||
|
s.replace_range(s.len() - 1..s.len(), "}");
|
||||||
|
}
|
||||||
|
Self::quote_literal(&Value::String(s))
|
||||||
|
} else {
|
||||||
|
Self::quote_literal(val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn quote_literal(val: &Value) -> String {
|
fn quote_literal(val: &Value) -> String {
|
||||||
match val {
|
match val {
|
||||||
Value::Null => "NULL".to_string(),
|
Value::Null => "NULL".to_string(),
|
||||||
|
|||||||
@ -19,11 +19,7 @@ pub struct Node<'a> {
|
|||||||
|
|
||||||
impl<'a> Compiler<'a> {
|
impl<'a> Compiler<'a> {
|
||||||
/// Compiles a JSON schema into a nested PostgreSQL query returning JSONB
|
/// Compiles a JSON schema into a nested PostgreSQL query returning JSONB
|
||||||
pub fn compile(
|
pub fn compile(&self, schema_id: &str, filter_keys: &[String]) -> Result<String, String> {
|
||||||
&self,
|
|
||||||
schema_id: &str,
|
|
||||||
filter_keys: &[String],
|
|
||||||
) -> Result<String, String> {
|
|
||||||
let schema = self
|
let schema = self
|
||||||
.db
|
.db
|
||||||
.schemas
|
.schemas
|
||||||
@ -251,8 +247,7 @@ impl<'a> Compiler<'a> {
|
|||||||
let mut bypass_node = node.clone();
|
let mut bypass_node = node.clone();
|
||||||
bypass_node.schema = std::sync::Arc::new(bypass_schema);
|
bypass_node.schema = std::sync::Arc::new(bypass_schema);
|
||||||
|
|
||||||
let mut bypassed_args =
|
let mut bypassed_args = self.compile_select_clause(r#type, table_aliases, bypass_node)?;
|
||||||
self.compile_select_clause(r#type, table_aliases, bypass_node)?;
|
|
||||||
select_args.append(&mut bypassed_args);
|
select_args.append(&mut bypassed_args);
|
||||||
} else {
|
} else {
|
||||||
let mut family_schemas = Vec::new();
|
let mut family_schemas = Vec::new();
|
||||||
@ -400,7 +395,9 @@ impl<'a> Compiler<'a> {
|
|||||||
) -> Result<Vec<String>, String> {
|
) -> Result<Vec<String>, String> {
|
||||||
let mut select_args = Vec::new();
|
let mut select_args = Vec::new();
|
||||||
let grouped_fields = r#type.grouped_fields.as_ref().and_then(|v| v.as_object());
|
let grouped_fields = r#type.grouped_fields.as_ref().and_then(|v| v.as_object());
|
||||||
let merged_props = self.get_merged_properties(node.schema.as_ref());
|
let merged_props = self
|
||||||
|
.db
|
||||||
|
.merged_properties(node.schema.as_ref(), &mut std::collections::HashSet::new());
|
||||||
let mut sorted_keys: Vec<&String> = merged_props.keys().collect();
|
let mut sorted_keys: Vec<&String> = merged_props.keys().collect();
|
||||||
sorted_keys.sort();
|
sorted_keys.sort();
|
||||||
|
|
||||||
@ -494,7 +491,13 @@ impl<'a> Compiler<'a> {
|
|||||||
where_clauses.push(format!("NOT {}.archived", entity_alias));
|
where_clauses.push(format!("NOT {}.archived", entity_alias));
|
||||||
|
|
||||||
self.compile_filter_conditions(r#type, type_aliases, &node, &base_alias, &mut where_clauses);
|
self.compile_filter_conditions(r#type, type_aliases, &node, &base_alias, &mut where_clauses);
|
||||||
self.compile_relation_conditions(r#type, type_aliases, &node, &base_alias, &mut where_clauses)?;
|
self.compile_relation_conditions(
|
||||||
|
r#type,
|
||||||
|
type_aliases,
|
||||||
|
&node,
|
||||||
|
&base_alias,
|
||||||
|
&mut where_clauses,
|
||||||
|
)?;
|
||||||
|
|
||||||
Ok(where_clauses)
|
Ok(where_clauses)
|
||||||
}
|
}
|
||||||
@ -509,7 +512,10 @@ impl<'a> Compiler<'a> {
|
|||||||
for (t_name, fields_val) in gf {
|
for (t_name, fields_val) in gf {
|
||||||
if let Some(fields_arr) = fields_val.as_array() {
|
if let Some(fields_arr) = fields_val.as_array() {
|
||||||
if fields_arr.iter().any(|v| v.as_str() == Some(field_name)) {
|
if fields_arr.iter().any(|v| v.as_str() == Some(field_name)) {
|
||||||
return type_aliases.get(t_name).cloned().unwrap_or_else(|| base_alias.to_string());
|
return type_aliases
|
||||||
|
.get(t_name)
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_else(|| base_alias.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -606,13 +612,31 @@ impl<'a> Compiler<'a> {
|
|||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
let sql_op = match op {
|
let sql_op = match op {
|
||||||
"$eq" => if is_ilike { "ILIKE" } else { "=" },
|
"$eq" => {
|
||||||
"$ne" => if is_ilike { "NOT ILIKE" } else { "!=" },
|
if is_ilike {
|
||||||
|
"ILIKE"
|
||||||
|
} else {
|
||||||
|
"="
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"$ne" => {
|
||||||
|
if is_ilike {
|
||||||
|
"NOT ILIKE"
|
||||||
|
} else {
|
||||||
|
"!="
|
||||||
|
}
|
||||||
|
}
|
||||||
"$gt" => ">",
|
"$gt" => ">",
|
||||||
"$gte" => ">=",
|
"$gte" => ">=",
|
||||||
"$lt" => "<",
|
"$lt" => "<",
|
||||||
"$lte" => "<=",
|
"$lte" => "<=",
|
||||||
_ => if is_ilike { "ILIKE" } else { "=" },
|
_ => {
|
||||||
|
if is_ilike {
|
||||||
|
"ILIKE"
|
||||||
|
} else {
|
||||||
|
"="
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let param_sql = if is_ilike && (op == "$eq" || op == "$ne") {
|
let param_sql = if is_ilike && (op == "$eq" || op == "$ne") {
|
||||||
@ -643,7 +667,9 @@ impl<'a> Compiler<'a> {
|
|||||||
let mut child_relation_alias = base_alias.to_string();
|
let mut child_relation_alias = base_alias.to_string();
|
||||||
|
|
||||||
if let Some(parent_type) = node.parent_type {
|
if let Some(parent_type) = node.parent_type {
|
||||||
let merged_props = self.get_merged_properties(node.schema.as_ref());
|
let merged_props = self
|
||||||
|
.db
|
||||||
|
.merged_properties(node.schema.as_ref(), &mut std::collections::HashSet::new());
|
||||||
let relative_keys: Vec<String> = merged_props.keys().cloned().collect();
|
let relative_keys: Vec<String> = merged_props.keys().cloned().collect();
|
||||||
|
|
||||||
let (relation, is_parent_source) = self
|
let (relation, is_parent_source) = self
|
||||||
@ -695,25 +721,4 @@ impl<'a> Compiler<'a> {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_merged_properties(
|
|
||||||
&self,
|
|
||||||
schema: &crate::database::schema::Schema,
|
|
||||||
) -> std::collections::BTreeMap<String, Arc<crate::database::schema::Schema>> {
|
|
||||||
let mut props = std::collections::BTreeMap::new();
|
|
||||||
|
|
||||||
if let Some(ref_id) = &schema.obj.r#ref {
|
|
||||||
if let Some(parent_schema) = self.db.schemas.get(ref_id) {
|
|
||||||
props.extend(self.get_merged_properties(parent_schema));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(local_props) = &schema.obj.properties {
|
|
||||||
for (k, v) in local_props {
|
|
||||||
props.insert(k.clone(), v.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
props
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8536,3 +8536,9 @@ fn test_merger_0_7() {
|
|||||||
let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR"));
|
let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR"));
|
||||||
crate::tests::runner::run_test_case(&path, 0, 7).unwrap();
|
crate::tests::runner::run_test_case(&path, 0, 7).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_merger_0_8() {
|
||||||
|
let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR"));
|
||||||
|
crate::tests::runner::run_test_case(&path, 0, 8).unwrap();
|
||||||
|
}
|
||||||
|
|||||||
@ -62,7 +62,8 @@ fn test_library_api() {
|
|||||||
"properties": {
|
"properties": {
|
||||||
"name": { "type": "string" }
|
"name": { "type": "string" }
|
||||||
},
|
},
|
||||||
"required": ["name"]
|
"required": ["name"],
|
||||||
|
"compiledProperties": ["name"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@ -13,13 +13,18 @@ impl<'a> ValidationContext<'a> {
|
|||||||
) -> Result<bool, ValidationError> {
|
) -> Result<bool, ValidationError> {
|
||||||
let current = self.instance;
|
let current = self.instance;
|
||||||
if let Some(obj) = current.as_object() {
|
if let Some(obj) = current.as_object() {
|
||||||
// Entity Bound Implicit Type Validation
|
// Entity implicit type validation
|
||||||
if let Some(lookup_key) = self.schema.id.as_ref().or(self.schema.r#ref.as_ref()) {
|
// Use the specific schema id or ref as a fallback
|
||||||
let base_type_name = lookup_key.split('.').next_back().unwrap_or("").to_string();
|
if let Some(identifier) = self.schema.id.as_ref().or(self.schema.r#ref.as_ref()) {
|
||||||
if let Some(type_def) = self.db.types.get(&base_type_name)
|
// Kick in if the data object has a type field
|
||||||
&& let Some(type_val) = obj.get("type")
|
if let Some(type_val) = obj.get("type")
|
||||||
&& let Some(type_str) = type_val.as_str()
|
&& let Some(type_str) = type_val.as_str()
|
||||||
{
|
{
|
||||||
|
// Get the string or the final segment as the base
|
||||||
|
let base = identifier.split('.').next_back().unwrap_or("").to_string();
|
||||||
|
// Check if the base is a global type name
|
||||||
|
if let Some(type_def) = self.db.types.get(&base) {
|
||||||
|
// Ensure the instance type is a variation of the global type
|
||||||
if type_def.variations.contains(type_str) {
|
if type_def.variations.contains(type_str) {
|
||||||
// Ensure it passes strict mode
|
// Ensure it passes strict mode
|
||||||
result.evaluated_keys.insert("type".to_string());
|
result.evaluated_keys.insert("type".to_string());
|
||||||
@ -33,8 +38,15 @@ impl<'a> ValidationContext<'a> {
|
|||||||
path: format!("{}/type", self.path),
|
path: format!("{}/type", self.path),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Ad-Hoc schemas natively use strict schema discriminator strings instead of variation inheritance
|
||||||
|
if type_str == identifier {
|
||||||
|
result.evaluated_keys.insert("type".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(min) = self.schema.min_properties
|
if let Some(min) = self.schema.min_properties
|
||||||
&& (obj.len() as f64) < min
|
&& (obj.len() as f64) < min
|
||||||
{
|
{
|
||||||
@ -44,6 +56,7 @@ impl<'a> ValidationContext<'a> {
|
|||||||
path: self.path.to_string(),
|
path: self.path.to_string(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(max) = self.schema.max_properties
|
if let Some(max) = self.schema.max_properties
|
||||||
&& (obj.len() as f64) > max
|
&& (obj.len() as f64) > max
|
||||||
{
|
{
|
||||||
@ -53,6 +66,7 @@ impl<'a> ValidationContext<'a> {
|
|||||||
path: self.path.to_string(),
|
path: self.path.to_string(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref req) = self.schema.required {
|
if let Some(ref req) = self.schema.required {
|
||||||
for field in req {
|
for field in req {
|
||||||
if !obj.contains_key(field) {
|
if !obj.contains_key(field) {
|
||||||
@ -114,10 +128,14 @@ impl<'a> ValidationContext<'a> {
|
|||||||
|
|
||||||
// Entity Bound Implicit Type Interception
|
// Entity Bound Implicit Type Interception
|
||||||
if key == "type"
|
if key == "type"
|
||||||
&& let Some(lookup_key) = sub_schema.id.as_ref().or(sub_schema.r#ref.as_ref())
|
&& let Some(schema_bound) = sub_schema.id.as_ref().or(sub_schema.r#ref.as_ref())
|
||||||
{
|
{
|
||||||
let base_type_name = lookup_key.split('.').next_back().unwrap_or("").to_string();
|
let physical_type_name = schema_bound
|
||||||
if let Some(type_def) = self.db.types.get(&base_type_name)
|
.split('.')
|
||||||
|
.next_back()
|
||||||
|
.unwrap_or("")
|
||||||
|
.to_string();
|
||||||
|
if let Some(type_def) = self.db.types.get(&physical_type_name)
|
||||||
&& let Some(instance_type) = child_instance.as_str()
|
&& let Some(instance_type) = child_instance.as_str()
|
||||||
&& type_def.variations.contains(instance_type)
|
&& type_def.variations.contains(instance_type)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user