Use a snapshot() to keep an editable copy of the whole workspace before a change. Then use compareTo() to get an EvolutionReport for the before and after states. Both states retain their model levels.
Create Filing and a Website level that refines it:
import { DataTypes, Modeller } from '@ramen-suite/core/model';
import type { Attribute, AttributeInfo, DataType, EvolutionChange, EvolutionReport, Node } from '@ramen-suite/core/model';
const filing: Modeller = Modeller.create('Filing');
const document: Node = filing.createNode('Document').element;
const status: DataType = filing.createDataType({ name: 'Status', baseType: DataTypes.String }).element;
const website: Modeller = filing.createNextLevel('Website');
const page: Node = website.createNode({ name: 'Page', refines: document }).element;
const before: Modeller = website.snapshot();
The snapshot includes Filing and Website. Edits to either original level leave before unchanged.
Add a property to Filing, rename Page and remove the unused Status type. The renamed node in Website inherits the new property, while the Page in the snapshot keeps its earlier definition:
const title: Attribute = filing.createAttribute(document, {
name: 'title',
typeId: DataTypes.String,
}).element;
website.updateNode(page.id, { name: 'Article' });
filing.removeDataType(status.id);
console.log(website.getAttributes(page).map((item: AttributeInfo): string => item.name)); // ['title']
console.log(before.getAttributes(page)); // []
const report: EvolutionReport = before.compareTo(website);
const titleChange: EvolutionChange | undefined = report.changes.find(
(change: EvolutionChange): boolean => change.element === 'attribute' && change.elementId === title.id,
);
console.log(titleChange?.action); // 'added'
The report uses stable IDs to identify added, removed and changed models, nodes, attributes, data types, literals and relations. An EvolutionChange gives the declaring modelId, elementId and, where relevant, ownerId, plus before and after values.
beforeDiagnostics and afterDiagnostics contain validation findings for each complete workspace. This includes disconnected branches and isolated import cycles. The findings show whether an edit left a model invalid, as explained in Validate Models and Data. The report describes differences without changing either state or migrating recorded data. For example, renaming title to heading preserves the attribute's identity in the report, but recorded properties.title values still need to be moved to properties.heading before validation against the new model.
Here is a shortened view of report. The API supplies generated IDs and complete definitions in before and after:
{
"beforeModelId": "<website-id>",
"afterModelId": "<website-id>",
"beforeDiagnostics": [],
"afterDiagnostics": [],
"changes": [
{
"action": "changed",
"element": "node",
"modelId": "<website-id>",
"elementId": "<page-id>",
"before": { "name": "Page" },
"after": { "name": "Article" },
},
{
"action": "added",
"element": "attribute",
"modelId": "<filing-id>",
"elementId": "<title-id>",
"ownerId": "<document-id>",
"after": { "name": "title", "typeId": "String" },
},
{
"action": "removed",
"element": "dataType",
"modelId": "<filing-id>",
"elementId": "<status-id>",
"before": { "name": "Status", "baseType": "String" },
},
],
}