Key-Value Attributes
Working with JSON document storage using CRDT
Overview
Key-Value Attributes are the primary data storage type in LinkedRecords. They store JSON documents and use CRDT (Conflict-free Replicated Data Types) for concurrent editing support.
Creating Key-Value Attributes
Simple Creation
Using create() Method
With Multiple Facts
Reading Values
Get Current Value
Find by Query
Updating Values
set() - Replace Entire Value
The set() method replaces the entire document:
set() replaces the entire document. Any fields not included in the new value
will be removed.
patch() - Merge Changes
The patch() method merges changes with the existing document:
change() - Fine-Grained Updates
The change() method allows precise control over individual fields using
KeyValueChange:
Nested Path Updates
Use dot notation for nested updates:
Delete Fields
Set a field to null to delete it:
Concurrency Handling
Key-Value Attributes handle concurrent edits. When multiple users edit different fields simultaneously, changes are automatically merged:
Array Gotcha: Arrays are completely overwritten on concurrent updates (last write wins). CRDT merging does NOT work for array elements. If you need collaborative array editing, consider using separate attributes for array items or using facts to represent relationships.
Working with Arrays Safely
Instead of storing items in an array:
Use nested objects with IDs:
Or use separate attributes connected by facts:
Common Patterns
Task Manager
User Profile
Document with Metadata
Best Practices
-
Use objects instead of arrays for collections that need concurrent editing
-
Use change() for partial updates instead of set() to preserve other fields
-
Flatten when possible - Deeply nested structures are harder to update