Compare commits

...

15 Commits

11 changed files with 471 additions and 210 deletions

View File

@ -184,23 +184,27 @@ It evaluates as an **Independent Declarative Rules Engine**. Every `Case` block
* **`array`**: Homogeneous collection of items matching the `items` schema. Validation is homogeneous, so strictness checking does not apply. `"extensible"` is not applicable at the `array` level (tuple-like `prefixItems` are removed).
* **Inheritance Boundaries**: Strictness resets when crossing non-primitive `type` boundaries. A schema extending a strict parent remains strict unless it explicitly overrides with `"extensible": true`.
### Immutable Properties (`"immutable": true`)
To distinguish read-only hydrated endpoint references, computed properties, system-managed timestamps (`created_at`, `modified_at`), or audit fields from writable properties, JSPG introduces the universal `"immutable": true` schema keyword.
### Immutable Properties (`"immutable": "always" | "external"`)
To distinguish read-only hydrated endpoint references, computed properties, system-managed timestamps (`created_at`, `modified_at`), or audit fields from writable properties, JSPG supports the `"immutable"` property schema attribute, which takes string enum values (`"always"`, `"external"`, or omitted).
* **Developer Perspective**: Annotate properties in database schemas or trait definitions with `"immutable": true` when the property should be visible on reads (`jspg_query` / `.response`), but rejected or ignored on writes (`jspg_merge` / `.request`).
* **Enum Values**:
* `"always"`: Property is permanently read-only across all boundaries (e.g. system-managed audit timestamps like `created_at`, `modified_at`, or computed fields).
* `"external"`: Property is read-only from external client API requests (`.request` payloads), but can be populated or mutated by internal system operations.
* Omitted: Property is fully writable.
* **Developer Perspective**: Annotate properties in database schemas or trait definitions with `"immutable": "always"` or `"immutable": "external"` when the property should be visible on reads (`jspg_query` / `.response`), but rejected or ignored on writes (`jspg_merge` / `.request`).
```json
"properties": {
"source": {
"family": "lite.organization",
"immutable": true,
"immutable": "always",
"description": "Read-only hydrated member entity summary on a membership edge."
}
}
```
* **Behavior Across Pillars**:
* **Queryer (`jspg_query`)**: Hydrates and includes `immutable` properties in output read responses without restriction.
* **Validator (`jspg_validate`)**: Context-aware. Returns `IMMUTABLE_PROPERTY_VIOLATION` if an `immutable` property is supplied in a write/request payload (schema IDs not ending in `.response`). Response schemas (`.response`) permit `immutable` fields.
* **Merger (`jspg_merge`)**: Automatically skips `immutable` properties during object graph merging so client payloads can never mutate database columns or relationship edges.
* **Queryer (`jspg_query`)**: Hydrates and includes `"immutable"` properties in output read responses without restriction.
* **Validator (`jspg_validate`)**: Context-aware. Rejects specified `"immutable"` properties supplied in write/request payloads. Response schemas (`.response`) permit `"immutable"` fields.
* **Merger (`jspg_merge`)**: Automatically skips `"immutable"` properties during object graph merging so client payloads can never mutate database columns or relationship edges.
### Format Leniency for Empty Strings
To simplify frontend form validation, format validators specifically for `uuid`, `date-time`, and `email` explicitly allow empty strings (`""`), treating them as "present but unset".

View File

@ -1027,16 +1027,31 @@
"name": "user",
"module": "test",
"source": "test",
"hierarchy": ["user"],
"variations": ["user"],
"fields": ["id", "email"],
"lookup_fields": ["email"],
"hierarchy": [
"user"
],
"variations": [
"user"
],
"fields": [
"id",
"email"
],
"lookup_fields": [
"email"
],
"schemas": {
"user": {
"type": "object",
"properties": {
"id": { "type": "string", "format": "uuid" },
"email": { "type": "string", "format": "email" }
"id": {
"type": "string",
"format": "uuid"
},
"email": {
"type": "string",
"format": "email"
}
}
}
}
@ -1072,7 +1087,9 @@
"indexes": [
{
"table": "user",
"columns": ["email"]
"columns": [
"email"
]
}
],
"types": [
@ -1082,16 +1099,31 @@
"name": "user",
"module": "test",
"source": "test",
"hierarchy": ["user"],
"variations": ["user"],
"fields": ["id", "email"],
"lookup_fields": ["email"],
"hierarchy": [
"user"
],
"variations": [
"user"
],
"fields": [
"id",
"email"
],
"lookup_fields": [
"email"
],
"schemas": {
"user": {
"type": "object",
"properties": {
"id": { "type": "string", "format": "uuid" },
"email": { "type": "string", "format": "email" }
"id": {
"type": "string",
"format": "uuid"
},
"email": {
"type": "string",
"format": "email"
}
}
}
}

View File

@ -120,11 +120,27 @@
"indexes": [
{
"table": "person",
"columns": ["first_name", "last_name", "date_of_birth", "pronouns"]
"columns": [
"first_name",
"last_name",
"date_of_birth",
"pronouns"
]
},
{
"table": "user",
"columns": ["name"]
"columns": [
"name"
]
},
{
"table": "relationship",
"columns": [
"type",
"source_id",
"target_id",
"start_date"
]
}
],
"types": [
@ -566,6 +582,8 @@
"source_type",
"target_id",
"target_type",
"start_date",
"end_date",
"id",
"type",
"name",
@ -590,7 +608,9 @@
"source_id",
"source_type",
"target_id",
"target_type"
"target_type",
"start_date",
"end_date"
]
},
"field_types": {
@ -601,6 +621,8 @@
"source_type": "text",
"target_id": "uuid",
"target_type": "text",
"start_date": "timestamptz",
"end_date": "timestamptz",
"name": "text",
"created_at": "timestamptz",
"created_by": "uuid",
@ -613,7 +635,16 @@
"properties": {}
}
},
"lookup_fields": [],
"lookup_fields": [
"type",
"source_id",
"target_id",
"start_date"
],
"field_defaults": {
"start_date": "0001-01-01T00:00:00Z",
"end_date": "9999-12-31T23:59:59Z"
},
"historical": true,
"notify": true
},
@ -1206,7 +1237,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"organization\" (",
@ -1216,7 +1248,8 @@
"VALUES (",
" '{{uuid:generated_0}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"organization\".*)"
],
[
"INSERT INTO agreego.\"user\" (",
@ -1226,7 +1259,8 @@
"VALUES (",
" '{{uuid:generated_0}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"user\".*)"
],
[
"INSERT INTO agreego.\"person\" (",
@ -1240,7 +1274,8 @@
" '{{uuid:generated_0}}',",
" 'IncompleteLast',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"person\".*)"
],
[
"INSERT INTO agreego.change (",
@ -1316,7 +1351,7 @@
"success": true,
"sql": [
[
"(SELECT to_jsonb(t1.*) || to_jsonb(t2.*) || to_jsonb(t3.*) || to_jsonb(t4.*)",
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}') || COALESCE(to_jsonb(t4.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"organization\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"user\" t3 ON t3.id = t1.id",
@ -1343,7 +1378,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"organization\" (",
@ -1353,7 +1389,8 @@
"VALUES (",
" '{{uuid:generated_0}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"organization\".*)"
],
[
"INSERT INTO agreego.\"user\" (",
@ -1363,7 +1400,8 @@
"VALUES (",
" '{{uuid:generated_0}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"user\".*)"
],
[
"INSERT INTO agreego.\"person\" (",
@ -1383,7 +1421,8 @@
" 'LookupLast',",
" 'they/them',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"person\".*)"
],
[
"INSERT INTO agreego.change (",
@ -1469,14 +1508,14 @@
"success": true,
"sql": [
[
"(SELECT to_jsonb(t1.*) || to_jsonb(t2.*) || to_jsonb(t3.*) || to_jsonb(t4.*)",
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}') || COALESCE(to_jsonb(t4.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"organization\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"user\" t3 ON t3.id = t1.id",
"LEFT JOIN agreego.\"person\" t4 ON t4.id = t1.id",
"WHERE",
" t1.id = '{{uuid:data.id}}'",
"UNION SELECT to_jsonb(t1.*) || to_jsonb(t2.*) || to_jsonb(t3.*) || to_jsonb(t4.*)",
"UNION SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}') || COALESCE(to_jsonb(t4.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"organization\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"user\" t3 ON t3.id = t1.id",
@ -1503,7 +1542,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"organization\" (",
@ -1513,7 +1553,8 @@
"VALUES (",
" '{{uuid:data.id}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"organization\".*)"
],
[
"INSERT INTO agreego.\"user\" (",
@ -1523,7 +1564,8 @@
"VALUES (",
" '{{uuid:data.id}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"user\".*)"
],
[
"INSERT INTO agreego.\"person\" (",
@ -1543,7 +1585,8 @@
" 'LookupLast',",
" 'they/them',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"person\".*)"
],
[
"INSERT INTO agreego.change (",
@ -1631,14 +1674,14 @@
"success": true,
"sql": [
[
"(SELECT to_jsonb(t1.*) || to_jsonb(t2.*) || to_jsonb(t3.*) || to_jsonb(t4.*)",
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}') || COALESCE(to_jsonb(t4.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"organization\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"user\" t3 ON t3.id = t1.id",
"LEFT JOIN agreego.\"person\" t4 ON t4.id = t1.id",
"WHERE",
" t1.id = '{{uuid:data.id}}'",
"UNION SELECT to_jsonb(t1.*) || to_jsonb(t2.*) || to_jsonb(t3.*) || to_jsonb(t4.*)",
"UNION SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}') || COALESCE(to_jsonb(t4.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"organization\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"user\" t3 ON t3.id = t1.id",
@ -1648,7 +1691,7 @@
" AND \"last_name\" = 'LookupLast'",
" AND \"date_of_birth\" = '{{timestamp}}'",
" AND \"pronouns\" = 'they/them')",
"UNION SELECT to_jsonb(t1.*) || to_jsonb(t2.*) || to_jsonb(t3.*) || to_jsonb(t4.*)",
"UNION SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}') || COALESCE(to_jsonb(t4.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"organization\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"user\" t3 ON t3.id = t1.id",
@ -1672,7 +1715,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"organization\" (",
@ -1684,7 +1728,8 @@
" '{{uuid:data.id}}',",
" 'LookupName',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"organization\".*)"
],
[
"INSERT INTO agreego.\"user\" (",
@ -1694,7 +1739,8 @@
"VALUES (",
" '{{uuid:data.id}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"user\".*)"
],
[
"INSERT INTO agreego.\"person\" (",
@ -1714,7 +1760,8 @@
" 'LookupLast',",
" 'they/them',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"person\".*)"
],
[
"INSERT INTO agreego.change (",
@ -1802,14 +1849,14 @@
"success": true,
"sql": [
[
"(SELECT to_jsonb(t1.*) || to_jsonb(t2.*) || to_jsonb(t3.*) || to_jsonb(t4.*)",
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}') || COALESCE(to_jsonb(t4.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"organization\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"user\" t3 ON t3.id = t1.id",
"LEFT JOIN agreego.\"person\" t4 ON t4.id = t1.id",
"WHERE",
" t1.id = '{{uuid:data.id}}'",
"UNION SELECT to_jsonb(t1.*) || to_jsonb(t2.*) || to_jsonb(t3.*) || to_jsonb(t4.*)",
"UNION SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}') || COALESCE(to_jsonb(t4.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"organization\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"user\" t3 ON t3.id = t1.id",
@ -1836,7 +1883,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"organization\" (",
@ -1846,7 +1894,8 @@
"VALUES (",
" '{{uuid:data.id}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"organization\".*)"
],
[
"INSERT INTO agreego.\"user\" (",
@ -1856,7 +1905,8 @@
"VALUES (",
" '{{uuid:data.id}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"user\".*)"
],
[
"INSERT INTO agreego.\"person\" (",
@ -1874,7 +1924,8 @@
" 'LookupLast',",
" 'they/them',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"person\".*)"
],
[
"INSERT INTO agreego.change (",
@ -1951,7 +2002,7 @@
"success": true,
"sql": [
[
"(SELECT to_jsonb(t1.*) || to_jsonb(t2.*) || to_jsonb(t3.*) || to_jsonb(t4.*)",
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}') || COALESCE(to_jsonb(t4.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"organization\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"user\" t3 ON t3.id = t1.id",
@ -1975,7 +2026,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"organization\" (",
@ -1985,7 +2037,8 @@
"VALUES (",
" '{{uuid:mocks.0.id}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"organization\".*)"
],
[
"INSERT INTO agreego.\"user\" (",
@ -1995,7 +2048,8 @@
"VALUES (",
" '{{uuid:mocks.0.id}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"user\".*)"
],
[
"INSERT INTO agreego.\"person\" (",
@ -2009,7 +2063,8 @@
" '{{uuid:mocks.0.id}}',",
" 'NewLast',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"person\".*)"
],
[
"INSERT INTO agreego.change (",
@ -2074,7 +2129,7 @@
"success": true,
"sql": [
[
"(SELECT to_jsonb(t1.*) || to_jsonb(t2.*) || to_jsonb(t3.*) || to_jsonb(t4.*)",
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}') || COALESCE(to_jsonb(t4.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"organization\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"user\" t3 ON t3.id = t1.id",
@ -2098,7 +2153,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"organization\" (",
@ -2108,7 +2164,8 @@
"VALUES (",
" '123',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"organization\".*)"
],
[
"INSERT INTO agreego.\"user\" (",
@ -2118,7 +2175,8 @@
"VALUES (",
" '123',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"user\".*)"
],
[
"INSERT INTO agreego.\"person\" (",
@ -2136,7 +2194,8 @@
" 'Doe',",
" NULL,",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"person\".*)"
],
[
"INSERT INTO agreego.change (",
@ -2226,7 +2285,8 @@
" '00000000-0000-0000-0000-000000000000',",
" '{{uuid:generated_1}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"organization\" (",
@ -2236,7 +2296,8 @@
"VALUES (",
" '{{uuid:generated_0}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"organization\".*)"
],
[
"INSERT INTO agreego.\"user\" (",
@ -2246,7 +2307,8 @@
"VALUES (",
" '{{uuid:generated_0}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"user\".*)"
],
[
"INSERT INTO agreego.\"person\" (",
@ -2262,7 +2324,8 @@
" '{{uuid:generated_0}}',",
" 'Smith',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"person\".*)"
],
[
"INSERT INTO agreego.change (",
@ -2306,7 +2369,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'order'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"order\" (",
@ -2320,7 +2384,8 @@
" '{{uuid:generated_3}}',",
" 100,",
" 'order'",
")"
")",
"RETURNING to_jsonb(\"order\".*)"
],
[
"INSERT INTO agreego.change (",
@ -2413,7 +2478,7 @@
"success": true,
"sql": [
[
"(SELECT to_jsonb(t1.*) || to_jsonb(t2.*)",
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"order\" t2 ON t2.id = t1.id",
"WHERE",
@ -2435,7 +2500,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'order'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"order\" (",
@ -2447,7 +2513,8 @@
" 'abc',",
" 99,",
" 'order'",
")"
")",
"RETURNING to_jsonb(\"order\".*)"
],
[
"INSERT INTO agreego.\"entity\" (",
@ -2465,7 +2532,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'order_line'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"order_line\" (",
@ -2481,7 +2549,8 @@
" 99,",
" 'Widget',",
" 'order_line'",
")"
")",
"RETURNING to_jsonb(\"order_line\".*)"
],
[
"INSERT INTO agreego.change (",
@ -2630,7 +2699,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"organization\" (",
@ -2640,7 +2710,8 @@
"VALUES (",
" '{{uuid:generated_0}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"organization\".*)"
],
[
"INSERT INTO agreego.\"user\" (",
@ -2650,7 +2721,8 @@
"VALUES (",
" '{{uuid:generated_0}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"user\".*)"
],
[
"INSERT INTO agreego.\"person\" (",
@ -2664,7 +2736,8 @@
" '{{uuid:generated_0}}',",
" 'Test',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"person\".*)"
],
[
"INSERT INTO agreego.\"entity\" (",
@ -2682,7 +2755,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'phone_number'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"phone_number\" (",
@ -2690,7 +2764,8 @@
")",
"VALUES (",
" '555-0001'",
")"
")",
"RETURNING to_jsonb(\"phone_number\".*)"
],
[
"INSERT INTO agreego.change (",
@ -2715,6 +2790,17 @@
" '00000000-0000-0000-0000-000000000000'",
")"
],
[
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"relationship\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"contact\" t3 ON t3.id = t1.id",
"WHERE",
" (t1.\"type\" = 'contact'",
" AND \"source_id\" = '{{uuid:generated_0}}'",
" AND \"target_id\" = '{{uuid:generated_1}}'",
" AND \"start_date\" = '{{timestamp}}'))"
],
[
"INSERT INTO agreego.\"entity\" (",
" \"created_at\",",
@ -2731,7 +2817,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'contact'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"relationship\" (",
@ -2745,7 +2832,8 @@
" 'person',",
" '{{uuid:generated_1}}',",
" 'phone_number'",
")"
")",
"RETURNING to_jsonb(\"relationship\".*)"
],
[
"INSERT INTO agreego.\"contact\" (",
@ -2753,7 +2841,8 @@
")",
"VALUES (",
" true",
")"
")",
"RETURNING to_jsonb(\"contact\".*)"
],
[
"INSERT INTO agreego.change (",
@ -2798,7 +2887,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'email_address'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"email_address\" (",
@ -2806,7 +2896,8 @@
")",
"VALUES (",
" 'test@example.com'",
")"
")",
"RETURNING to_jsonb(\"email_address\".*)"
],
[
"INSERT INTO agreego.change (",
@ -2831,6 +2922,17 @@
" '00000000-0000-0000-0000-000000000000'",
")"
],
[
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"relationship\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"contact\" t3 ON t3.id = t1.id",
"WHERE",
" (t1.\"type\" = 'contact'",
" AND \"source_id\" = '{{uuid:generated_0}}'",
" AND \"target_id\" = '{{uuid:generated_5}}'",
" AND \"start_date\" = '{{timestamp}}'))"
],
[
"INSERT INTO agreego.\"entity\" (",
" \"created_at\",",
@ -2847,7 +2949,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'contact'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"relationship\" (",
@ -2861,7 +2964,8 @@
" 'person',",
" '{{uuid:generated_5}}',",
" 'email_address'",
")"
")",
"RETURNING to_jsonb(\"relationship\".*)"
],
[
"INSERT INTO agreego.\"contact\" (",
@ -2869,7 +2973,8 @@
")",
"VALUES (",
" false",
")"
")",
"RETURNING to_jsonb(\"contact\".*)"
],
[
"INSERT INTO agreego.change (",
@ -2914,7 +3019,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'email_address'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"email_address\" (",
@ -2922,7 +3028,8 @@
")",
"VALUES (",
" 'test2@example.com'",
")"
")",
"RETURNING to_jsonb(\"email_address\".*)"
],
[
"INSERT INTO agreego.change (",
@ -2947,6 +3054,17 @@
" '00000000-0000-0000-0000-000000000000'",
")"
],
[
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"relationship\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"contact\" t3 ON t3.id = t1.id",
"WHERE",
" (t1.\"type\" = 'contact'",
" AND \"source_id\" = '{{uuid:generated_0}}'",
" AND \"target_id\" = '{{uuid:generated_9}}'",
" AND \"start_date\" = '{{timestamp}}'))"
],
[
"INSERT INTO agreego.\"entity\" (",
" \"created_at\",",
@ -2963,7 +3081,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'contact'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"relationship\" (",
@ -2977,7 +3096,8 @@
" 'person',",
" '{{uuid:generated_9}}',",
" 'email_address'",
")"
")",
"RETURNING to_jsonb(\"relationship\".*)"
],
[
"INSERT INTO agreego.\"contact\" (",
@ -2985,7 +3105,8 @@
")",
"VALUES (",
" false",
")"
")",
"RETURNING to_jsonb(\"contact\".*)"
],
[
"INSERT INTO agreego.change (",
@ -3215,7 +3336,7 @@
"success": true,
"sql": [
[
"(SELECT to_jsonb(t1.*) || to_jsonb(t2.*) || to_jsonb(t3.*) || to_jsonb(t4.*)",
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}') || COALESCE(to_jsonb(t3.*), '{}') || COALESCE(to_jsonb(t4.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"organization\" t2 ON t2.id = t1.id",
"LEFT JOIN agreego.\"user\" t3 ON t3.id = t1.id",
@ -3241,7 +3362,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"organization\" (",
@ -3251,7 +3373,8 @@
"VALUES (",
" 'abc-archived',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"organization\".*)"
],
[
"INSERT INTO agreego.\"user\" (",
@ -3261,7 +3384,8 @@
"VALUES (",
" 'abc-archived',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"user\".*)"
],
[
"INSERT INTO agreego.\"person\" (",
@ -3271,7 +3395,8 @@
"VALUES (",
" 'abc-archived',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"person\".*)"
],
[
"INSERT INTO agreego.change (",
@ -3353,7 +3478,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'attachment'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"attachment\" (",
@ -3373,7 +3499,8 @@
" '{",
" \"type\": \"type_metadata\"",
" }'",
")"
")",
"RETURNING to_jsonb(\"attachment\".*)"
],
[
"INSERT INTO agreego.change (",
@ -3480,7 +3607,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'order_line'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"order_line\" (",
@ -3496,7 +3624,8 @@
" 99,",
" 'Widget',",
" 'order_line'",
")"
")",
"RETURNING to_jsonb(\"order_line\".*)"
],
[
"INSERT INTO agreego.change (",
@ -3568,7 +3697,7 @@
"success": true,
"sql": [
[
"(SELECT to_jsonb(t1.*) || to_jsonb(t2.*)",
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"order_line\" t2 ON t2.id = t1.id",
"WHERE",
@ -3590,7 +3719,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'order_line'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"order_line\" (",
@ -3606,7 +3736,8 @@
" 99,",
" 'Widget',",
" 'order_line'",
")"
")",
"RETURNING to_jsonb(\"order_line\".*)"
],
[
"INSERT INTO agreego.change (",
@ -3692,7 +3823,7 @@
"success": true,
"sql": [
[
"(SELECT to_jsonb(t1.*) || to_jsonb(t2.*)",
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"invoice\" t2 ON t2.id = t1.id",
"WHERE",
@ -3714,7 +3845,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'invoice'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"invoice\" (",
@ -3746,7 +3878,8 @@
" }',",
" 200,",
" 'invoice'",
")"
")",
"RETURNING to_jsonb(\"invoice\".*)"
],
[
"INSERT INTO agreego.change (",
@ -3808,7 +3941,7 @@
"success": true,
"sql": [
[
"(SELECT to_jsonb(t1.*) || to_jsonb(t2.*)",
"(SELECT COALESCE(to_jsonb(t1.*), '{}') || COALESCE(to_jsonb(t2.*), '{}')",
"FROM agreego.\"entity\" t1",
"LEFT JOIN agreego.\"account\" t2 ON t2.id = t1.id",
"WHERE",
@ -3830,7 +3963,8 @@
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'account'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"account\" (",
@ -3844,7 +3978,8 @@
" 'checking',",
" '123456789',",
" 'account'",
")"
")",
"RETURNING to_jsonb(\"account\".*)"
],
[
"INSERT INTO agreego.change (",
@ -3936,7 +4071,8 @@
" '00000000-0000-0000-0000-000000000000',",
" '{{uuid:generated_1}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"organization\" (",
@ -3946,7 +4082,8 @@
"VALUES (",
" '{{uuid:generated_0}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"organization\".*)"
],
[
"INSERT INTO agreego.\"user\" (",
@ -3956,7 +4093,8 @@
"VALUES (",
" '{{uuid:generated_0}}',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"user\".*)"
],
[
"INSERT INTO agreego.\"person\" (",
@ -3970,7 +4108,8 @@
" '{{uuid:generated_0}}',",
" 'Person',",
" 'person'",
")"
")",
"RETURNING to_jsonb(\"person\".*)"
],
[
"INSERT INTO agreego.change (",
@ -4015,7 +4154,8 @@
" '00000000-0000-0000-0000-000000000000',",
" 'parent-org-id',",
" 'order'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"order\" (",
@ -4027,7 +4167,8 @@
" '{{uuid:generated_0}}',",
" '{{uuid:generated_3}}',",
" 'order'",
")"
")",
"RETURNING to_jsonb(\"order\".*)"
],
[
"INSERT INTO agreego.\"entity\" (",
@ -4047,7 +4188,8 @@
" '00000000-0000-0000-0000-000000000000',",
" 'parent-org-id',",
" 'order_line'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"order_line\" (",
@ -4059,7 +4201,8 @@
" '{{uuid:generated_4}}',",
" '{{uuid:generated_3}}',",
" 'order_line'",
")"
")",
"RETURNING to_jsonb(\"order_line\".*)"
],
[
"INSERT INTO agreego.change (",
@ -4103,7 +4246,8 @@
" '00000000-0000-0000-0000-000000000000',",
" 'explicit-org-id',",
" 'order_line'",
")"
")",
"RETURNING to_jsonb(\"entity\".*)"
],
[
"INSERT INTO agreego.\"order_line\" (",
@ -4115,7 +4259,8 @@
" '{{uuid:generated_6}}',",
" '{{uuid:generated_3}}',",
" 'order_line'",
")"
")",
"RETURNING to_jsonb(\"order_line\".*)"
],
[
"INSERT INTO agreego.change (",

View File

@ -260,12 +260,9 @@
},
"lookup_fields": [],
"null_fields": [],
"default_fields": [
"id",
"type",
"created_at",
"archived"
],
"field_defaults": {
"archived": false
},
"variations": [
"bot",
"organization",
@ -1739,20 +1736,20 @@
" AND entity_1.created_at <= ($16 #>> '{}')::TIMESTAMPTZ",
" AND entity_1.created_at <> ($17 #>> '{}')::TIMESTAMPTZ",
" AND person_3.first_name ILIKE $18 #>> '{}'",
" AND person_3.first_name > ($19 #>> '{)",
" AND person_3.first_name >= ($20 #>> '{)",
" AND person_3.first_name < ($21 #>> '{)",
" AND person_3.first_name <= ($22 #>> '{)",
" AND person_3.first_name NOT ILIKE $23 #>> '{}'",
" AND person_3.first_name NOT IN (SELECT value FROM jsonb_array_elements_text(($24 #>> '{}')::jsonb))",
" AND person_3.first_name IN (SELECT value FROM jsonb_array_elements_text(($25 #>> '{}')::jsonb))",
" AND entity_1.id = ($26 #>> '{}')::UUID",
" AND entity_1.id <> ($27 #>> '{}')::UUID",
" AND entity_1.id NOT IN (SELECT value::UUID FROM jsonb_array_elements_text(($28 #>> '{}')::jsonb))",
" AND entity_1.id IN (SELECT value::UUID FROM jsonb_array_elements_text(($29 #>> '{}')::jsonb))",
" AND person_3.last_name ILIKE $30 #>> '{}'",
" AND person_3.last_name NOT ILIKE $31 #>> '{}'",
" ))))"
" AND person_3.first_name > ($19 #>> '{}')",
" AND person_3.first_name >= ($20 #>> '{}')",
" AND person_3.first_name < ($21 #>> '{}')",
" AND person_3.first_name <= ($22 #>> '{}')",
" AND person_3.first_name NOT ILIKE $23 #>> '{}'",
" AND person_3.first_name NOT IN (SELECT value FROM jsonb_array_elements_text(($24 #>> '{}')::jsonb))",
" AND person_3.first_name IN (SELECT value FROM jsonb_array_elements_text(($25 #>> '{}')::jsonb))",
" AND entity_1.id = ($26 #>> '{}')::UUID",
" AND entity_1.id <> ($27 #>> '{}')::UUID",
" AND entity_1.id NOT IN (SELECT value::UUID FROM jsonb_array_elements_text(($28 #>> '{}')::jsonb))",
" AND entity_1.id IN (SELECT value::UUID FROM jsonb_array_elements_text(($29 #>> '{}')::jsonb))",
" AND person_3.last_name ILIKE $30 #>> '{}'",
" AND person_3.last_name NOT ILIKE $31 #>> '{}'",
"))))"
]
]
}

View File

@ -77,8 +77,8 @@ impl DatabaseExecutor for SpiExecutor {
pgrx::debug1!("JSPG_SQL: {}", sql);
self.transact(|| {
Spi::connect(|client| {
match client.select(sql, Some(args_with_oid.len() as i64), &args_with_oid) {
Spi::connect_mut(|client| {
match client.update(sql, Some(args_with_oid.len() as i64), &args_with_oid) {
Ok(tup_table) => {
let mut results = Vec::new();
for row in tup_table {

View File

@ -36,7 +36,7 @@ pub struct Type {
#[serde(default)]
pub null_fields: Vec<String>,
#[serde(default)]
pub default_fields: Vec<String>,
pub field_defaults: IndexMap<String, Value>,
pub field_types: Option<Value>,
#[serde(default)]
pub schemas: IndexMap<String, Arc<Schema>>,

View File

@ -57,7 +57,7 @@ pub fn jspg_setup(database: Json) -> Json {
Json(serde_json::to_value(drop).unwrap())
}
#[cfg_attr(not(test), pg_extern)]
#[cfg_attr(not(test), pg_extern(volatile))]
pub fn jspg_merge(schema_id: &str, data: JsonB) -> JsonB {
// Try to acquire a read lock to get a clone of the Engine Arc
let engine_opt = {
@ -74,7 +74,7 @@ pub fn jspg_merge(schema_id: &str, data: JsonB) -> JsonB {
}
}
#[cfg_attr(not(test), pg_extern)]
#[cfg_attr(not(test), pg_extern(volatile))]
pub fn jspg_merge_ordered(schema_id: &str, data: Json) -> Json {
let engine_opt = {
let lock = GLOBAL_JSPG.read().unwrap();

View File

@ -6,8 +6,8 @@ pub mod cache;
use crate::database::Database;
use crate::database::r#type::Type;
use crate::drop::{Drop, Error, ErrorDetails};
use serde_json::Value;
use indexmap::IndexMap;
use serde_json::Value;
use std::sync::Arc;
pub struct Merger {
@ -31,9 +31,10 @@ impl Merger {
None => {
return Drop::with_errors(vec![Error {
code: "SCHEMA_NOT_FOUND".to_string(),
values: Some(IndexMap::from([
("schema".to_string(), schema_id.to_string()),
])),
values: Some(IndexMap::from([(
"schema".to_string(),
schema_id.to_string(),
)])),
details: ErrorDetails {
path: None,
cause: None,
@ -56,9 +57,7 @@ impl Merger {
if let Err(e) = self.db.execute(&notify_sql, None) {
return Drop::with_errors(vec![Error {
code: "MERGE_FAILED".to_string(),
values: Some(IndexMap::from([
("error".to_string(), e.clone()),
])),
values: Some(IndexMap::from([("error".to_string(), e.clone())])),
details: ErrorDetails {
path: None,
cause: Some(e),
@ -121,9 +120,15 @@ impl Merger {
} else {
return Err(Error {
code: "TARGET_SCHEMA_NOT_FOUND".to_string(),
values: Some(IndexMap::from([("target_id".to_string(), target_id.clone())])),
values: Some(IndexMap::from([(
"target_id".to_string(),
target_id.clone(),
)])),
details: ErrorDetails {
cause: Some(format!("Polymorphic mapped target '{}' not found in database registry", target_id)),
cause: Some(format!(
"Polymorphic mapped target '{}' not found in database registry",
target_id
)),
..Default::default()
},
});
@ -141,7 +146,10 @@ impl Merger {
code: "ONE_OF_INDEX_NOT_FOUND".to_string(),
values: Some(IndexMap::from([("index".to_string(), idx.to_string())])),
details: ErrorDetails {
cause: Some(format!("Polymorphic index target '{}' not found in local oneOf array", idx)),
cause: Some(format!(
"Polymorphic index target '{}' not found in local oneOf array",
idx
)),
..Default::default()
},
});
@ -164,7 +172,10 @@ impl Merger {
("value".to_string(), v.to_string()),
])),
details: ErrorDetails {
cause: Some(format!("Polymorphic discriminator {}='{}' matched no compiled options", disc, v)),
cause: Some(format!(
"Polymorphic discriminator {}='{}' matched no compiled options",
disc, v
)),
..Default::default()
},
});
@ -172,9 +183,15 @@ impl Merger {
} else {
return Err(Error {
code: "MISSING_DISCRIMINATOR".to_string(),
values: Some(IndexMap::from([("discriminator".to_string(), disc.to_string())])),
values: Some(IndexMap::from([(
"discriminator".to_string(),
disc.to_string(),
)])),
details: ErrorDetails {
cause: Some(format!("Polymorphic merging failed: missing required discriminator '{}'", disc)),
cause: Some(format!(
"Polymorphic merging failed: missing required discriminator '{}'",
disc
)),
..Default::default()
},
});
@ -281,8 +298,6 @@ impl Merger {
let mut entity_objects = std::collections::BTreeMap::new();
let mut entity_arrays = std::collections::BTreeMap::new();
let is_external = self.db.executor.punc_external().unwrap_or(false);
for (k, v) in obj {
// Always retain system and unmapped core fields natively implicitly mapped to the Postgres tables
if k == "id" || k == "type" || k == "created" {
@ -291,12 +306,6 @@ impl Merger {
}
if let Some(prop_schema) = compiled_props.get(&k) {
if prop_schema.is_immutable(is_external) {
continue;
}
let mut is_edge = false;
if let Some(edges) = schema.obj.compiled_edges.get() {
if edges.contains_key(&k) {
@ -326,6 +335,17 @@ impl Merger {
}
}
// Hydrate missing fields from field_defaults for entity_type and its hierarchy
for parent_type_name in &type_def.hierarchy {
if let Some(parent_type) = self.db.types.get(parent_type_name) {
for (k, v) in &parent_type.field_defaults {
if !entity_fields.contains_key(k) {
entity_fields.insert(k.clone(), v.clone());
}
}
}
}
let mut current_org_id = None;
if let Some(compiled_props) = schema.obj.compiled_properties.get() {
if let Some(org_schema) = compiled_props.get("organization_id") {
@ -373,7 +393,10 @@ impl Merger {
if let Some(deps) = &schema.obj.dependencies {
if let Some(crate::database::object::Dependency::Props(req_props)) = deps.get("created") {
for req in req_props {
if !entity_fields.contains_key(req) && !entity_objects.contains_key(req) && !entity_arrays.contains_key(req) {
if !entity_fields.contains_key(req)
&& !entity_objects.contains_key(req)
&& !entity_arrays.contains_key(req)
{
return Err(Error {
code: "REQUIRED_FIELD_MISSING".to_string(),
values: Some(IndexMap::from([
@ -382,7 +405,10 @@ impl Merger {
])),
details: ErrorDetails {
path: Some(req.to_string()),
cause: Some(format!("Missing required creation field '{}' for entity {}", req, type_name)),
cause: Some(format!(
"Missing required creation field '{}' for entity {}",
req, type_name
)),
..Default::default()
},
});
@ -493,7 +519,7 @@ impl Merger {
entity_change_kind.as_deref().unwrap_or(""),
&type_name,
type_def,
&entity_fields,
&mut entity_fields,
entity_fetched.as_ref(),
)?;
@ -772,7 +798,7 @@ impl Merger {
let fetch_sql_template = if let Some(cached) = self.cache.get(entity_type_name) {
cached
} else {
let mut select_list = String::from("to_jsonb(t1.*)");
let mut select_list = String::from("COALESCE(to_jsonb(t1.*), '{}')");
let mut join_clauses = format!("FROM agreego.\"{}\" t1", entity_type.hierarchy[0]);
for (i, table_name) in entity_type.hierarchy.iter().enumerate().skip(1) {
@ -781,7 +807,7 @@ impl Merger {
" LEFT JOIN agreego.\"{}\" {} ON {}.id = t1.id",
table_name, t_alias, t_alias
));
select_list.push_str(&format!(" || to_jsonb({}.*)", t_alias));
select_list.push_str(&format!(" || COALESCE(to_jsonb({}.*), '{{}}')", t_alias));
}
let template = format!("SELECT {} {}", select_list, join_clauses);
@ -829,9 +855,15 @@ impl Merger {
if table.len() > 1 {
Err(Error {
code: "TOO_MANY_LOOKUP_ROWS".to_string(),
values: Some(IndexMap::from([("entity_type".to_string(), entity_type_name.to_string())])),
values: Some(IndexMap::from([(
"entity_type".to_string(),
entity_type_name.to_string(),
)])),
details: ErrorDetails {
cause: Some(format!("Lookup for {} found too many existing rows", entity_type_name)),
cause: Some(format!(
"Lookup for {} found too many existing rows",
entity_type_name
)),
..Default::default()
},
})
@ -878,7 +910,7 @@ impl Merger {
change_kind: &str,
entity_type_name: &str,
entity_type: &crate::database::r#type::Type,
entity_fields: &serde_json::Map<String, Value>,
entity_fields: &mut serde_json::Map<String, Value>,
_entity_fetched: Option<&serde_json::Map<String, Value>>,
) -> Result<(), Error> {
if change_kind.is_empty() {
@ -886,7 +918,7 @@ impl Merger {
}
let id_str = match entity_fields.get("id").and_then(|v| v.as_str()) {
Some(id) => id,
Some(id) => id.to_string(),
None => {
return Err(Error {
code: "MISSING_ENTITY_ID".to_string(),
@ -904,9 +936,15 @@ impl Merger {
_ => {
return Err(Error {
code: "MISSING_GROUPED_FIELDS".to_string(),
values: Some(IndexMap::from([("type".to_string(), entity_type_name.to_string())])),
values: Some(IndexMap::from([(
"type".to_string(),
entity_type_name.to_string(),
)])),
details: ErrorDetails {
cause: Some(format!("Grouped fields missing for type {}", entity_type_name)),
cause: Some(format!(
"Grouped fields missing for type {}",
entity_type_name
)),
..Default::default()
},
});
@ -923,7 +961,7 @@ impl Merger {
};
let mut entity_pairs = serde_json::Map::new();
for (k, v) in entity_fields {
for (k, v) in entity_fields.iter() {
if table_fields.contains(k) {
entity_pairs.insert(k.clone(), v.clone());
}
@ -957,20 +995,33 @@ impl Merger {
}
let sql = format!(
"INSERT INTO agreego.\"{}\" ({}) VALUES ({})",
"INSERT INTO agreego.\"{}\" ({}) VALUES ({}) RETURNING to_jsonb(\"{}\".*)",
table_name,
columns.join(", "),
values.join(", ")
values.join(", "),
table_name
);
if let Err(e) = self.db.execute(&sql, None) {
return Err(Error {
code: "DATABASE_SPI_ERROR".to_string(),
values: Some(IndexMap::from([("error".to_string(), e.clone())])),
details: ErrorDetails {
cause: Some(e),
..Default::default()
},
});
match self.db.query(&sql, None) {
Ok(Value::Array(rows)) => {
if let Some(Value::Object(row_map)) = rows.into_iter().next() {
for (k, v) in row_map {
if !v.is_null() {
entity_fields.insert(k, v);
}
}
}
}
Err(e) => {
return Err(Error {
code: "DATABASE_SPI_ERROR".to_string(),
values: Some(IndexMap::from([("error".to_string(), e.clone())])),
details: ErrorDetails {
cause: Some(e),
..Default::default()
},
});
}
_ => {}
}
} else if change_kind == "update" || change_kind == "delete" {
entity_pairs.remove("id");
@ -998,20 +1049,33 @@ impl Merger {
}
let sql = format!(
"UPDATE agreego.\"{}\" SET {} WHERE id = {}",
"UPDATE agreego.\"{}\" SET {} WHERE id = {} RETURNING to_jsonb(\"{}\".*)",
table_name,
set_clauses.join(", "),
Self::quote_literal(&Value::String(id_str.to_string()))
Self::quote_literal(&Value::String(id_str.to_string())),
table_name
);
if let Err(e) = self.db.execute(&sql, None) {
return Err(Error {
code: "DATABASE_SPI_ERROR".to_string(),
values: Some(IndexMap::from([("error".to_string(), e.clone())])),
details: ErrorDetails {
cause: Some(e),
..Default::default()
},
});
match self.db.query(&sql, None) {
Ok(Value::Array(rows)) => {
if let Some(Value::Object(row_map)) = rows.into_iter().next() {
for (k, v) in row_map {
if !v.is_null() {
entity_fields.insert(k, v);
}
}
}
}
Err(e) => {
return Err(Error {
code: "DATABASE_SPI_ERROR".to_string(),
values: Some(IndexMap::from([("error".to_string(), e.clone())])),
details: ErrorDetails {
cause: Some(e),
..Default::default()
},
});
}
_ => {}
}
}
}

View File

@ -97,7 +97,14 @@ impl SqlFormatter {
self.push_line("VALUES (");
self.indent += 2;
let vals = if suffix.ends_with(")") { &suffix[..suffix.len() - 1] } else { suffix };
let (vals, returning_clause) = if let Some(ret_idx) = suffix.rfind(") RETURNING ") {
(&suffix[..ret_idx], Some(&suffix[ret_idx + 2..]))
} else if suffix.ends_with(")") {
(&suffix[..suffix.len() - 1], None)
} else {
(suffix, None)
};
let mut val_tokens = Vec::new();
let mut curr = String::new();
let mut in_str = false;
@ -119,7 +126,7 @@ impl SqlFormatter {
for (i, val) in val_tokens.iter().enumerate() {
let comma = if i < val_tokens.len() - 1 { "," } else { "" };
if val.starts_with("'{") && val.ends_with("}'") {
if val.starts_with("'{") && val.ends_with("}'") && val.len() > 4 {
let inner = &val[1..val.len() - 1];
// Unescape single quotes from SQL strings
let unescaped = inner.replace("''", "'");
@ -146,6 +153,9 @@ impl SqlFormatter {
}
self.indent -= 2;
self.push_line(")");
if let Some(ret) = returning_clause {
self.push_line(ret);
}
} else {
self.push_line(&s);
}
@ -168,10 +178,19 @@ impl SqlFormatter {
self.indent -= 2;
if let Some(w) = where_idx {
self.push_line("WHERE");
self.indent += 2;
self.push_line(&after_set[w + 7..]);
self.indent -= 2;
let where_clause = &after_set[w + 7..];
if let Some(ret_idx) = where_clause.find(" RETURNING ") {
self.push_line("WHERE");
self.indent += 2;
self.push_line(&where_clause[..ret_idx]);
self.indent -= 2;
self.push_line(&where_clause[ret_idx + 1..]);
} else {
self.push_line("WHERE");
self.indent += 2;
self.push_line(where_clause);
self.indent -= 2;
}
}
} else {
self.push_line(&s);
@ -341,7 +360,7 @@ impl SqlFormatter {
}
Expr::Value(sqlparser::ast::ValueWithSpan { value: Value::SingleQuotedString(s), .. }) | Expr::Value(sqlparser::ast::ValueWithSpan { value: Value::EscapedStringLiteral(s), .. }) => {
if s.starts_with('{') && s.ends_with('}') {
if s.starts_with('{') && s.ends_with('}') && s.len() > 2 {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(s) {
if let Ok(pretty) = serde_json::to_string_pretty(&json) {
let lines: Vec<&str> = pretty.split('\n').collect();

View File

@ -104,7 +104,7 @@ fn test_library_api() {
},
"types": {
"source_schema": {
"default_fields": [],
"field_defaults": {},
"field_types": null,
"fields": [],
"grouped_fields": null,
@ -169,7 +169,7 @@ fn test_library_api() {
"variations": ["source_schema"]
},
"target_schema": {
"default_fields": [],
"field_defaults": {},
"field_types": null,
"fields": [],
"grouped_fields": null,

View File

@ -1 +1 @@
1.0.187
1.0.195