-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnodes.js
More file actions
125 lines (108 loc) · 3.58 KB
/
Copy pathnodes.js
File metadata and controls
125 lines (108 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* @module nodes
* Node query and inspection for git-mind.
*/
import { extractPrefix, classifyPrefix } from './validators.js';
import { getProp, toPropObject } from './prop-bag.js';
/**
* @typedef {object} NodeInfo
* @property {string} id - Full node ID (prefix:identifier)
* @property {string} prefix - Extracted prefix
* @property {string} prefixClass - 'canonical' | 'system' | 'unknown'
* @property {Record<string, unknown>} properties - Node properties
*/
/**
* Get a node by ID with prefix classification and properties.
* Returns null if the node doesn't exist.
*
* @param {import('@git-stunts/git-warp').default} graph
* @param {string} id - Node ID
* @returns {Promise<NodeInfo|null>}
*/
export async function getNode(graph, id) {
const exists = await graph.hasNode(id);
if (!exists) return null;
const prefix = extractPrefix(id);
const propsMap = await graph.getNodeProps(id);
const properties = toPropObject(propsMap);
return {
id,
prefix,
prefixClass: prefix ? classifyPrefix(prefix) : 'unknown',
properties,
};
}
/**
* @typedef {object} SetPropertyResult
* @property {string} id - Node ID
* @property {string} key - Property key
* @property {unknown} value - New property value
* @property {unknown} previous - Previous value (null if unset)
* @property {boolean} changed - Whether the value actually changed
*/
/**
* Set a property on an existing node.
* Idempotent — setting the same value returns { changed: false }.
*
* Note: idempotency uses strict equality (===). The CLI always passes string
* values; callers using structured values (objects/arrays) will always see
* changed: true since === compares by reference.
*
* @param {import('@git-stunts/git-warp').default} graph
* @param {string} id - Node ID
* @param {string} key - Property key (non-empty)
* @param {unknown} value - Property value (string in CLI usage)
* @returns {Promise<SetPropertyResult>}
*/
export async function setNodeProperty(graph, id, key, value) {
if (!key || typeof key !== 'string') {
throw new Error('Property key must be a non-empty string');
}
const exists = await graph.hasNode(id);
if (!exists) {
throw new Error(`Node not found: ${id}`);
}
// Read current value
const propsMap = await graph.getNodeProps(id);
const previous = getProp(propsMap, key) ?? null;
const changed = previous !== value;
if (changed) {
const patch = await graph.createPatch();
patch.setProperty(id, key, value);
await patch.commit();
}
return { id, key, value, previous, changed };
}
/**
* @typedef {object} UnsetPropertyResult
* @property {string} id - Node ID
* @property {string} key - Property key
* @property {unknown} previous - Previous value (null if unset)
* @property {boolean} removed - Whether a value was actually removed
*/
/**
* Remove a property from an existing node.
*
* @param {import('@git-stunts/git-warp').default} graph
* @param {string} id - Node ID
* @param {string} key - Property key
* @returns {Promise<UnsetPropertyResult>}
*/
export async function unsetNodeProperty(graph, id, key) {
if (!key || typeof key !== 'string') {
throw new Error('Property key must be a non-empty string');
}
const exists = await graph.hasNode(id);
if (!exists) {
throw new Error(`Node not found: ${id}`);
}
const propsMap = await graph.getNodeProps(id);
const previous = getProp(propsMap, key) ?? null;
const removed = previous != null;
if (removed) {
const patch = await graph.createPatch();
patch.setProperty(id, key, null);
await patch.commit();
}
return { id, key, previous, removed };
}