@dxos/graph 0.7.5-feature-compute.4d9d99a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +8 -0
- package/README.md +12 -0
- package/dist/lib/browser/index.mjs +379 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node/index.cjs +404 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +381 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/model.d.ts +107 -0
- package/dist/types/src/model.d.ts.map +1 -0
- package/dist/types/src/model.test.d.ts +2 -0
- package/dist/types/src/model.test.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +59 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/src/util.d.ts +17 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +58 -0
- package/src/index.ts +7 -0
- package/src/model.test.ts +120 -0
- package/src/model.ts +266 -0
- package/src/types.ts +61 -0
- package/src/util.ts +69 -0
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dxos/graph",
|
|
3
|
+
"version": "0.7.5-feature-compute.4d9d99a",
|
|
4
|
+
"description": "Low-level graph API",
|
|
5
|
+
"homepage": "https://dxos.org",
|
|
6
|
+
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "DXOS.org",
|
|
9
|
+
"sideEffects": true,
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/types/src/index.d.ts",
|
|
13
|
+
"browser": "./dist/lib/browser/index.mjs",
|
|
14
|
+
"node": "./dist/lib/node-esm/index.mjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"types": "dist/types/src/index.d.ts",
|
|
18
|
+
"typesVersions": {
|
|
19
|
+
"*": {
|
|
20
|
+
"meta": [
|
|
21
|
+
"dist/types/src/meta.d.ts"
|
|
22
|
+
],
|
|
23
|
+
"types": [
|
|
24
|
+
"dist/types/src/types/index.d.ts"
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"src"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@effect/schema": "^0.75.5",
|
|
34
|
+
"@preact/signals-core": "^1.6.0",
|
|
35
|
+
"lodash.defaultsdeep": "^4.6.1",
|
|
36
|
+
"@dxos/async": "0.7.5-feature-compute.4d9d99a",
|
|
37
|
+
"@dxos/echo-schema": "0.7.5-feature-compute.4d9d99a",
|
|
38
|
+
"@dxos/debug": "0.7.5-feature-compute.4d9d99a",
|
|
39
|
+
"@dxos/live-object": "0.7.5-feature-compute.4d9d99a",
|
|
40
|
+
"@dxos/echo-db": "0.7.5-feature-compute.4d9d99a",
|
|
41
|
+
"@dxos/node-std": "0.7.5-feature-compute.4d9d99a",
|
|
42
|
+
"@dxos/log": "0.7.5-feature-compute.4d9d99a",
|
|
43
|
+
"@dxos/schema": "0.7.5-feature-compute.4d9d99a",
|
|
44
|
+
"@dxos/invariant": "0.7.5-feature-compute.4d9d99a",
|
|
45
|
+
"@dxos/util": "0.7.5-feature-compute.4d9d99a"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@antv/graphlib": "^2.0.4",
|
|
49
|
+
"@antv/layout": "^1.2.13",
|
|
50
|
+
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
51
|
+
"@dxos/echo-db": "0.7.5-feature-compute.4d9d99a",
|
|
52
|
+
"@dxos/random": "0.7.5-feature-compute.4d9d99a",
|
|
53
|
+
"@dxos/echo-schema": "0.7.5-feature-compute.4d9d99a"
|
|
54
|
+
},
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { S } from '@dxos/echo-schema';
|
|
8
|
+
|
|
9
|
+
import { GraphModel } from './model';
|
|
10
|
+
import { BaseGraphNode, type GraphNode } from './types';
|
|
11
|
+
|
|
12
|
+
const TestNode = S.extend(
|
|
13
|
+
BaseGraphNode,
|
|
14
|
+
S.Struct({
|
|
15
|
+
value: S.String,
|
|
16
|
+
}),
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
type TestNode = S.Schema.Type<typeof TestNode>;
|
|
20
|
+
|
|
21
|
+
type TestData = { value: string };
|
|
22
|
+
|
|
23
|
+
describe('Graph', () => {
|
|
24
|
+
test('empty', ({ expect }) => {
|
|
25
|
+
const graph = new GraphModel();
|
|
26
|
+
expect(graph.nodes).to.have.length(0);
|
|
27
|
+
expect(graph.edges).to.have.length(0);
|
|
28
|
+
expect(graph.toJSON()).to.deep.eq({ nodes: [], edges: [] });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('extended', ({ expect }) => {
|
|
32
|
+
const graph = new GraphModel<TestNode>();
|
|
33
|
+
const node = graph.addNode({ id: 'test', value: 'test' });
|
|
34
|
+
expect(node.value.length).to.eq(4);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('optional', ({ expect }) => {
|
|
38
|
+
{
|
|
39
|
+
const graph = new GraphModel<GraphNode<string>>();
|
|
40
|
+
const node = graph.addNode({ id: 'test', data: 'test' });
|
|
41
|
+
expect(node.data.length).to.eq(4);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
{
|
|
45
|
+
const graph = new GraphModel<GraphNode.Optional<string>>();
|
|
46
|
+
const node = graph.addNode({ id: 'test' });
|
|
47
|
+
expect(node.data?.length).to.be.undefined;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('add and remove subgraphs', ({ expect }) => {
|
|
52
|
+
const graph = new GraphModel<GraphNode<TestData>>();
|
|
53
|
+
graph.builder
|
|
54
|
+
.addNode({ id: 'node1', data: { value: 'test' } })
|
|
55
|
+
.addNode({ id: 'node2', data: { value: 'test' } })
|
|
56
|
+
.addNode({ id: 'node3', data: { value: 'test' } })
|
|
57
|
+
.addEdge({ source: 'node1', target: 'node2' })
|
|
58
|
+
.addEdge({ source: 'node2', target: 'node3' });
|
|
59
|
+
expect(graph.nodes).to.have.length(3);
|
|
60
|
+
expect(graph.edges).to.have.length(2);
|
|
61
|
+
const pre = graph.toJSON();
|
|
62
|
+
|
|
63
|
+
const node = graph.findNode('node2');
|
|
64
|
+
expect(node).to.exist;
|
|
65
|
+
|
|
66
|
+
const removed = graph.removeNode('node2');
|
|
67
|
+
expect(removed.nodes).to.have.length(1);
|
|
68
|
+
expect(removed.edges).to.have.length(2);
|
|
69
|
+
expect(graph.nodes).to.have.length(2);
|
|
70
|
+
expect(graph.edges).to.have.length(0);
|
|
71
|
+
|
|
72
|
+
graph.addGraph(removed);
|
|
73
|
+
const post = graph.toJSON();
|
|
74
|
+
expect(pre).to.deep.eq(post);
|
|
75
|
+
|
|
76
|
+
graph.clear();
|
|
77
|
+
expect(graph.nodes).to.have.length(0);
|
|
78
|
+
expect(graph.edges).to.have.length(0);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('traverse', ({ expect }) => {
|
|
82
|
+
const graph = new GraphModel();
|
|
83
|
+
graph.builder
|
|
84
|
+
.addNode({ id: 'a' })
|
|
85
|
+
.addNode({ id: 'b' })
|
|
86
|
+
.addNode({ id: 'c' })
|
|
87
|
+
.addNode({ id: 'd' })
|
|
88
|
+
.addNode({ id: 'e' })
|
|
89
|
+
.addNode({ id: 'f' })
|
|
90
|
+
.addNode({ id: 'g' })
|
|
91
|
+
.addNode({ id: 'h' })
|
|
92
|
+
// Sub-graph 1.
|
|
93
|
+
.addEdge({ source: 'a', target: 'b' })
|
|
94
|
+
.addEdge({ source: 'a', target: 'c' })
|
|
95
|
+
.addEdge({ source: 'c', target: 'd' })
|
|
96
|
+
.addEdge({ source: 'd', target: 'e' })
|
|
97
|
+
.addEdge({ source: 'd', target: 'a' })
|
|
98
|
+
// Sub-graph 2.
|
|
99
|
+
.addEdge({ source: 'f', target: 'g' })
|
|
100
|
+
.addEdge({ source: 'g', target: 'h' });
|
|
101
|
+
|
|
102
|
+
const count = graph.nodes.length;
|
|
103
|
+
|
|
104
|
+
{
|
|
105
|
+
// Sub-graph 1.
|
|
106
|
+
const nodes = graph.traverse(graph.getNode('a'));
|
|
107
|
+
expect(nodes).to.have.length(5);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
{
|
|
111
|
+
// Sub-graph 2.
|
|
112
|
+
const nodes = graph.traverse(graph.getNode('f'));
|
|
113
|
+
expect(nodes).to.have.length(3);
|
|
114
|
+
|
|
115
|
+
// Remove sub-graph.
|
|
116
|
+
graph.removeNodes(nodes.map((node) => node.id));
|
|
117
|
+
expect(graph.nodes).to.have.length(count - 3);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
});
|
package/src/model.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { inspectCustom } from '@dxos/debug';
|
|
6
|
+
import { failedInvariant, invariant } from '@dxos/invariant';
|
|
7
|
+
import { getSnapshot } from '@dxos/live-object';
|
|
8
|
+
import { type MakeOptional, isNotFalsy, removeBy, stripUndefined } from '@dxos/util';
|
|
9
|
+
|
|
10
|
+
import { type Graph, type GraphNode, type GraphEdge, type BaseGraphNode, type BaseGraphEdge } from './types';
|
|
11
|
+
import { createEdgeId } from './util';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Wrapper class contains reactive nodes and edges.
|
|
15
|
+
*/
|
|
16
|
+
export class ReadonlyGraphModel<
|
|
17
|
+
Node extends BaseGraphNode = BaseGraphNode,
|
|
18
|
+
Edge extends BaseGraphEdge = BaseGraphEdge,
|
|
19
|
+
> {
|
|
20
|
+
protected readonly _graph: Graph;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* NOTE: Pass in simple Graph or ReactiveObject.
|
|
24
|
+
*/
|
|
25
|
+
constructor(graph?: Graph) {
|
|
26
|
+
this._graph = graph ?? { nodes: [], edges: [] };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
[inspectCustom]() {
|
|
30
|
+
return this.toJSON();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Return stable sorted JSON representation of graph.
|
|
35
|
+
*/
|
|
36
|
+
// TODO(burdon): Create separate toJson method with computed signal.
|
|
37
|
+
toJSON() {
|
|
38
|
+
const { id, nodes, edges } = getSnapshot(this._graph);
|
|
39
|
+
nodes.sort(({ id: a }, { id: b }) => a.localeCompare(b));
|
|
40
|
+
edges.sort(({ id: a }, { id: b }) => a.localeCompare(b));
|
|
41
|
+
return stripUndefined({ id, nodes, edges });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get graph(): Graph {
|
|
45
|
+
return this._graph;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get nodes(): Node[] {
|
|
49
|
+
return this._graph.nodes as Node[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get edges(): Edge[] {
|
|
53
|
+
return this._graph.edges as Edge[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
//
|
|
57
|
+
// Nodes
|
|
58
|
+
//
|
|
59
|
+
|
|
60
|
+
findNode(id: string): Node | undefined {
|
|
61
|
+
return this.nodes.find((node) => node.id === id);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
getNode(id: string): Node {
|
|
65
|
+
return this.findNode(id) ?? failedInvariant();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
filterNodes({ type }: Partial<GraphNode> = {}): Node[] {
|
|
69
|
+
return this.nodes.filter((node) => !type || type === node.type);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
//
|
|
73
|
+
// Edges
|
|
74
|
+
//
|
|
75
|
+
|
|
76
|
+
findEdge(id: string): Edge | undefined {
|
|
77
|
+
return this.edges.find((edge) => edge.id === id);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
getEdge(id: string): Edge {
|
|
81
|
+
return this.findEdge(id) ?? failedInvariant();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
filterEdges({ type, source, target }: Partial<GraphEdge> = {}): Edge[] {
|
|
85
|
+
return this.edges.filter(
|
|
86
|
+
(edge) =>
|
|
87
|
+
(!type || type === edge.type) && (!source || source === edge.source) && (!target || target === edge.target),
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
//
|
|
92
|
+
// Traverse
|
|
93
|
+
//
|
|
94
|
+
|
|
95
|
+
traverse(root: Node): Node[] {
|
|
96
|
+
return this._traverse(root);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private _traverse(root: Node, visited: Set<string> = new Set()): Node[] {
|
|
100
|
+
if (visited.has(root.id)) {
|
|
101
|
+
return [];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
visited.add(root.id);
|
|
105
|
+
const targets = this.filterEdges({ source: root.id })
|
|
106
|
+
.map((edge) => this.getNode(edge.target))
|
|
107
|
+
.filter(isNotFalsy);
|
|
108
|
+
|
|
109
|
+
return [root, ...targets.flatMap((target) => this._traverse(target, visited))];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Typed wrapper.
|
|
115
|
+
*/
|
|
116
|
+
export abstract class AbstractGraphModel<
|
|
117
|
+
Node extends BaseGraphNode,
|
|
118
|
+
Edge extends BaseGraphEdge,
|
|
119
|
+
Model extends AbstractGraphModel<Node, Edge, Model, Builder>,
|
|
120
|
+
Builder extends AbstractGraphBuilder<Node, Edge, Model>,
|
|
121
|
+
> extends ReadonlyGraphModel<Node, Edge> {
|
|
122
|
+
/**
|
|
123
|
+
* Allows chaining.
|
|
124
|
+
*/
|
|
125
|
+
abstract get builder(): Builder;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Shallow copy of provided graph.
|
|
129
|
+
*/
|
|
130
|
+
abstract copy(graph?: Partial<Graph>): Model;
|
|
131
|
+
|
|
132
|
+
clear(): this {
|
|
133
|
+
this._graph.nodes.length = 0;
|
|
134
|
+
this._graph.edges.length = 0;
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
addGraph(graph: Model): this {
|
|
139
|
+
this.addNodes(graph.nodes);
|
|
140
|
+
this.addEdges(graph.edges);
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
addGraphs(graphs: Model[]): this {
|
|
145
|
+
graphs.forEach((graph) => {
|
|
146
|
+
this.addNodes(graph.nodes);
|
|
147
|
+
this.addEdges(graph.edges);
|
|
148
|
+
});
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
addNode(node: Node): Node {
|
|
153
|
+
invariant(node.id, 'ID is required');
|
|
154
|
+
invariant(!this.findNode(node.id), `node already exists: ${node.id}`);
|
|
155
|
+
this._graph.nodes.push(node);
|
|
156
|
+
return node;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
addNodes(nodes: Node[]): Node[] {
|
|
160
|
+
return nodes.map((node) => this.addNode(node));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
addEdge(edge: MakeOptional<Edge, 'id'>): Edge {
|
|
164
|
+
invariant(edge.source);
|
|
165
|
+
invariant(edge.target);
|
|
166
|
+
if (!edge.id) {
|
|
167
|
+
// TODO(burdon): Generate random id.
|
|
168
|
+
edge = { id: createEdgeId(edge), ...edge };
|
|
169
|
+
}
|
|
170
|
+
invariant(!this.findNode(edge.id!));
|
|
171
|
+
this._graph.edges.push(edge as Edge);
|
|
172
|
+
return edge as Edge;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
addEdges(edges: Edge[]): Edge[] {
|
|
176
|
+
return edges.map((edge) => this.addEdge(edge));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
removeNode(id: string): Model {
|
|
180
|
+
const nodes = removeBy<Node>(this._graph.nodes as Node[], (node) => node.id === id);
|
|
181
|
+
const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.source === id || edge.target === id);
|
|
182
|
+
return this.copy({ nodes, edges });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
removeNodes(ids: string[]): Model {
|
|
186
|
+
const graphs = ids.map((id) => this.removeNode(id));
|
|
187
|
+
return this.copy().addGraphs(graphs);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
removeEdge(id: string): Model {
|
|
191
|
+
const edges = removeBy<Edge>(this._graph.edges as Edge[], (edge) => edge.id === id);
|
|
192
|
+
return this.copy({ nodes: [], edges });
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
removeEdges(ids: string[]): Model {
|
|
196
|
+
const graphs = ids.map((id) => this.removeEdge(id));
|
|
197
|
+
return this.copy().addGraphs(graphs);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Chainable wrapper
|
|
203
|
+
*/
|
|
204
|
+
export abstract class AbstractGraphBuilder<
|
|
205
|
+
Node extends BaseGraphNode,
|
|
206
|
+
Edge extends BaseGraphEdge,
|
|
207
|
+
Model extends GraphModel<Node, Edge>,
|
|
208
|
+
> {
|
|
209
|
+
constructor(protected readonly _model: Model) {}
|
|
210
|
+
|
|
211
|
+
get model(): Model {
|
|
212
|
+
return this._model;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
call(cb: (builder: this) => void): this {
|
|
216
|
+
cb(this);
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
getNode(id: string): Node {
|
|
221
|
+
return this.model.getNode(id);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
addNode(node: Node): this {
|
|
225
|
+
this._model.addNode(node);
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
addEdge(edge: MakeOptional<Edge, 'id'>): this {
|
|
230
|
+
this._model.addEdge(edge);
|
|
231
|
+
return this;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
addNodes(nodes: Node[]): this {
|
|
235
|
+
this._model.addNodes(nodes);
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
addEdges(edges: Edge[]): this {
|
|
240
|
+
this._model.addEdges(edges);
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export class GraphModel<
|
|
246
|
+
Node extends BaseGraphNode = BaseGraphNode,
|
|
247
|
+
Edge extends BaseGraphEdge = BaseGraphEdge,
|
|
248
|
+
> extends AbstractGraphModel<Node, Edge, GraphModel<Node, Edge>, GraphBuilder<Node, Edge>> {
|
|
249
|
+
override get builder() {
|
|
250
|
+
return new GraphBuilder<Node, Edge>(this);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
override copy(graph?: Partial<Graph>) {
|
|
254
|
+
return new GraphModel<Node, Edge>({ nodes: graph?.nodes ?? [], edges: graph?.edges ?? [] });
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export class GraphBuilder<
|
|
259
|
+
Node extends BaseGraphNode = BaseGraphNode,
|
|
260
|
+
Edge extends BaseGraphEdge = BaseGraphEdge,
|
|
261
|
+
> extends AbstractGraphBuilder<Node, Edge, GraphModel<Node, Edge>> {
|
|
262
|
+
override call(cb: (builder: this) => void) {
|
|
263
|
+
cb(this);
|
|
264
|
+
return this;
|
|
265
|
+
}
|
|
266
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { S } from '@dxos/echo-schema';
|
|
6
|
+
import { type Specialize } from '@dxos/util';
|
|
7
|
+
|
|
8
|
+
export const BaseGraphNode = S.Struct({
|
|
9
|
+
id: S.String,
|
|
10
|
+
type: S.optional(S.String),
|
|
11
|
+
data: S.optional(S.Any),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
/** Raw base type. */
|
|
15
|
+
export type BaseGraphNode = S.Schema.Type<typeof BaseGraphNode>;
|
|
16
|
+
/** Typed node data. */
|
|
17
|
+
export type GraphNode<Data = any, Optional extends boolean = false> = Specialize<
|
|
18
|
+
BaseGraphNode,
|
|
19
|
+
Optional extends true ? { data?: Data } : { data: Data }
|
|
20
|
+
>;
|
|
21
|
+
|
|
22
|
+
export declare namespace GraphNode {
|
|
23
|
+
export type Optional<T = any> = GraphNode<T, true>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const BaseGraphEdge = S.Struct({
|
|
27
|
+
id: S.String,
|
|
28
|
+
type: S.optional(S.String),
|
|
29
|
+
data: S.optional(S.Any),
|
|
30
|
+
source: S.String,
|
|
31
|
+
target: S.String,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/** Raw base type. */
|
|
35
|
+
export type BaseGraphEdge = S.Schema.Type<typeof BaseGraphEdge>;
|
|
36
|
+
|
|
37
|
+
/** Typed edge data. */
|
|
38
|
+
export type GraphEdge<Data = any, Optional extends boolean = false> = Specialize<
|
|
39
|
+
BaseGraphEdge,
|
|
40
|
+
Optional extends true ? { data?: Data } : { data: Data }
|
|
41
|
+
>;
|
|
42
|
+
|
|
43
|
+
export declare namespace GraphEdge {
|
|
44
|
+
export type Optional<T = any> = GraphEdge<T, true>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Allows any additional properties on graph nodes.
|
|
49
|
+
*/
|
|
50
|
+
const ExtendableBaseGraphNode = S.extend(BaseGraphNode, S.Struct({}, { key: S.String, value: S.Any }));
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Generic graph.
|
|
54
|
+
*/
|
|
55
|
+
export const Graph = S.Struct({
|
|
56
|
+
id: S.optional(S.String),
|
|
57
|
+
nodes: S.mutable(S.Array(ExtendableBaseGraphNode)),
|
|
58
|
+
edges: S.mutable(S.Array(BaseGraphEdge)),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
export type Graph = S.Schema.Type<typeof Graph>;
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type ReactiveEchoObject } from '@dxos/echo-db';
|
|
6
|
+
import { FormatEnum } from '@dxos/echo-schema';
|
|
7
|
+
import { invariant } from '@dxos/invariant';
|
|
8
|
+
import { getSchema, create } from '@dxos/live-object';
|
|
9
|
+
import { log } from '@dxos/log';
|
|
10
|
+
import { getSchemaProperties } from '@dxos/schema';
|
|
11
|
+
|
|
12
|
+
import { GraphModel } from './model';
|
|
13
|
+
import { Graph, type GraphNode } from './types';
|
|
14
|
+
|
|
15
|
+
const KEY_REGEX = /\w+/;
|
|
16
|
+
|
|
17
|
+
// NOTE: The `relation` is different from the `type`.
|
|
18
|
+
type EdgeMeta = { source: string; target: string; relation?: string };
|
|
19
|
+
|
|
20
|
+
export const createEdgeId = ({ source, target, relation }: EdgeMeta) => {
|
|
21
|
+
invariant(source.match(KEY_REGEX), `invalid source: ${source}`);
|
|
22
|
+
invariant(target.match(KEY_REGEX), `invalid target: ${target}`);
|
|
23
|
+
return [source, relation, target].join('_');
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const parseEdgeId = (id: string): EdgeMeta => {
|
|
27
|
+
const [source, relation, target] = id.split('_');
|
|
28
|
+
invariant(source.length && target.length);
|
|
29
|
+
return { source, relation: relation.length ? relation : undefined, target };
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new reactive graph from a set of ECHO objects.
|
|
34
|
+
* References are mapped onto graph edges.
|
|
35
|
+
*/
|
|
36
|
+
export const createGraph = (objects: ReactiveEchoObject<any>[]): GraphModel<GraphNode<ReactiveEchoObject<any>>> => {
|
|
37
|
+
const graph = new GraphModel<GraphNode<ReactiveEchoObject<any>>>(create(Graph, { nodes: [], edges: [] }));
|
|
38
|
+
|
|
39
|
+
// Map objects.
|
|
40
|
+
objects.forEach((object) => {
|
|
41
|
+
graph.addNode({ id: object.id, type: object.typename, data: object });
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Find references.
|
|
45
|
+
objects.forEach((object) => {
|
|
46
|
+
const schema = getSchema(object);
|
|
47
|
+
if (!schema) {
|
|
48
|
+
log.info('no schema for object', { id: object.id.slice(0, 8) });
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Parse schema to follow referenced objects.
|
|
53
|
+
for (const prop of getSchemaProperties(schema.ast, object)) {
|
|
54
|
+
if (prop.format === FormatEnum.Ref) {
|
|
55
|
+
const source = object;
|
|
56
|
+
const target = object[prop.name]?.target;
|
|
57
|
+
if (target) {
|
|
58
|
+
graph.addEdge({
|
|
59
|
+
id: createEdgeId({ source: source.id, target: target.id, relation: String(prop.name) }),
|
|
60
|
+
source: source.id,
|
|
61
|
+
target: target.id,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return graph;
|
|
69
|
+
};
|