This page predates the current package split. Use the current package guides for checked installation, public imports, and onboarding examples. The detailed examples below have not all been revalidated.
DataStore Operations API
DataStore operations are low-level operations that directly manipulate the DataStore with overlay and lock support. They are organized into logical classes.
Overview
DataStore operations are grouped into the following classes:
- CoreOperations: Basic CRUD operations (setNode, getNode, deleteNode, etc.)
- ContentOperations: Parent-child relationship management (addChild, removeChild, moveNode, etc.)
- RangeOperations: Text range operations (insertText, deleteText, replaceText, etc.)
- MarkOperations: Mark management (normalizeMarks, addMark, removeMark, etc.)
- QueryOperations: Node querying and searching
- SplitMergeOperations: Node splitting and merging
- DecoratorOperations: Decorator management
- UtilityOperations: Utility functions (editable node detection, traversal, etc.)
- SerializationOperations: Serialization and deserialization
Usage Pattern
All DataStore operations are accessed through the DataStore instance:
import { DataStore } from '@barocss/datastore';
const dataStore = new DataStore();
// Operations are accessed via dataStore properties
dataStore.core.setNode(node);
dataStore.content.addChild(parentId, child);
dataStore.range.insertText(range, 'Hello');
dataStore.mark.normalizeMarks(nodeId);
dataStore.query.findNodesByType('paragraph');
dataStore.splitMerge.splitTextNode('text-1', 5);
dataStore.utility.getPreviousEditableNode('text-1');
Note: DataStore operations automatically use overlay when a transaction is active (via dataStore.begin()).
Operation Categories
When to Use Each Class
| Task | Use This Class | Example |
|---|---|---|
| Create/Read/Update/Delete nodes | CoreOperations | dataStore.core.setNode(), dataStore.core.getNode() |
| Manage parent-child relationships | ContentOperations | dataStore.content.addChild(), dataStore.content.moveNode() |
| Manipulate text ranges | RangeOperations | dataStore.range.insertText(), dataStore.range.deleteText() |
| Manage marks | MarkOperations | dataStore.mark.normalizeMarks(), dataStore.mark.toggleMark() |
| Query/search nodes | QueryOperations | dataStore.query.findNodesByType(), dataStore.query.searchText() |
| Split/merge nodes | SplitMergeOperations | dataStore.splitMerge.splitTextNode(), dataStore.splitMerge.mergeTextNodes() |
| Utility functions | UtilityOperations | dataStore.utility.getParent(), dataStore.utility.isLeafNode() |
| Decorator management | DecoratorOperations | See Decorators Guide |
| Serialization | SerializationOperations | See DataStore README |
CoreOperations
Basic CRUD operations for nodes.
setNode(node: INode, validate?: boolean): void
Creates or updates a node in DataStore.
Parameters:
node: Node to set (assignssidif missing)validate: Whether to validate against schema (default:true)
Behavior:
- Assigns
sidif missing usingDataStore.generateId() - Validates against schema if
validate=trueand schema exists - Converts object children in content to IDs recursively
- Overlay-aware: writes go to overlay if transaction active
- Emits
'create'or'update'operation event
Example:
dataStore.core.setNode({
sid: 'p1',
stype: 'paragraph',
text: 'Hello',
content: []
});
getNode(nodeId: string): INode | undefined
Retrieves a node by ID.
Parameters:
nodeId: Node ID (SID)
Returns:
INode | undefined: Node if found,undefinedotherwise
Read Path:
- Check
deletedNodeIds→ returnundefinedif deleted - Check
overlayNodes→ return overlay version if exists - Fallback to
baseNodes→ return base version
Example:
const node = dataStore.core.getNode('text-1');
deleteNode(nodeId: string): boolean
Deletes a node from DataStore.
Parameters:
nodeId: Node ID to delete
Returns:
boolean:trueif deleted,falseif node not found
Behavior:
- Cannot delete root node (throws error)
- Removes node from parent's content array
- Emits
'delete'operation event - Overlay-aware
Example:
const deleted = dataStore.core.deleteNode('node-1');
updateNode(nodeId: string, updates: Partial<INode>, validate?: boolean): { valid: boolean; errors: string[] } | null
Updates a node with partial changes.
Parameters:
nodeId: Node ID to updateupdates: Partial node data to applyvalidate: Whether to validate (default:true)
Returns:
- Validation result:
{ valid: boolean; errors: string[] }ornull
Behavior:
- Merges fields (attributes shallow-merge)
- Validates against schema if
validate=true - Overlay-aware: writes go to overlay if transaction active
- Emits
'update'operation event
Example:
const result = dataStore.core.updateNode('text-1', {
text: 'Updated text'
});
createNodeWithChildren(node: INode, schema?: Schema): INode
Creates a node with all its children recursively.
Parameters:
node: Node with nested children (objects)schema: Optional schema for validation
Returns:
INode: Created node with assigned IDs
Behavior:
- Recursively creates all child nodes
- Assigns IDs to all nodes
- Converts object children to ID arrays
- Overlay-aware
Example:
const root = dataStore.core.createNodeWithChildren({
stype: 'document',
content: [
{
stype: 'paragraph',
content: [
{ stype: 'inline-text', text: 'Hello' }
]
}
]
});
transformNode(nodeId: string, newType: string, newAttrs?: Record<string, any>): { valid: boolean; errors: string[]; newNodeId?: string }
Transforms a node to a different type.
Parameters:
nodeId: Node ID to transformnewType: New schema type (stype)newAttrs: Optional new attributes
Returns:
- Validation result with optional
newNodeId
Example:
const result = dataStore.core.transformNode('p1', 'heading', { level: 1 });
ContentOperations
Manages parent-child relationships and content ordering.
addChild(parentId: string, child: INode | string, position?: number): string
Adds a child node to a parent's content array.
Parameters:
parentId: Parent node IDchild: Child node (object) or child ID (string)position: Insert position (default: end)
Returns:
string: Child node ID
Behavior:
- Creates child if object provided (assigns ID if missing)
- Inserts child ID at position in parent's content array
- Updates child's
parentId - Emits
'update'for parent - Overlay-aware
Example:
// Add existing node
const childId = dataStore.content.addChild('parent-1', 'child-1', 0);
// Create and add new node
const newChildId = dataStore.content.addChild('parent-1', {
stype: 'paragraph',
text: 'New paragraph'
}, 0);