Skip to content

JSON Patch

All entity updates in Formation use JSON Patch (RFC 6902). The frontend’s buildPatch() function generates patch operations by comparing original and updated entity states.

A patch document is an array of operations:

[
{ "op": "replace", "path": "/Name", "value": "New Name" },
{ "op": "replace", "path": "/Address/Id", "value": "AD03KwA" }
]

The backend PatchRewriter automatically transforms nested object references to foreign key updates:

Patch PathTransformed To
/Address/Id/AddressId
/CompanyType/CompanyTypeCode/CompanyTypeId
/DevelopmentStatus/Id/DevelopmentStatusId

This means the frontend always works with encoded IDs in nested objects, while the backend handles the FK mapping.

import { calculateChanges } from '$lib/services/entity-operations.svelte'
const changes = calculateChanges(original, edited, {
ignore: ['/NavigationProps', '/Collections']
})
// Send to API
await fetch(`/api/Schemes('${id}')`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json-patch+json' },
body: JSON.stringify(changes)
})

The calculateChanges() function compares two entity snapshots and returns only the fields that differ. Use the ignore option to exclude navigation properties or collections from comparison.