@dxos/graph 0.8.2-main.36232bc → 0.8.2-main.7503643
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/dist/lib/browser/index.mjs +81 -78
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +76 -75
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +81 -78
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/create.d.ts +9 -0
- package/dist/types/src/create.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/model.d.ts +1 -1
- package/dist/types/src/model.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +0 -8
- package/dist/types/src/util.d.ts.map +1 -1
- package/package.json +13 -13
- package/src/create.ts +52 -0
- package/src/index.ts +1 -0
- package/src/model.ts +1 -1
- package/src/util.ts +0 -47
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/graph",
|
|
3
|
-
"version": "0.8.2-main.
|
|
3
|
+
"version": "0.8.2-main.7503643",
|
|
4
4
|
"description": "Low-level graph API",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -33,23 +33,23 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@preact/signals-core": "^1.6.0",
|
|
35
35
|
"lodash.defaultsdeep": "^4.6.1",
|
|
36
|
-
"@dxos/async": "0.8.2-main.
|
|
37
|
-
"@dxos/
|
|
38
|
-
"@dxos/
|
|
39
|
-
"@dxos/
|
|
40
|
-
"@dxos/invariant": "0.8.2-main.
|
|
41
|
-
"@dxos/
|
|
42
|
-
"@dxos/log": "0.8.2-main.
|
|
43
|
-
"@dxos/schema": "0.8.2-main.
|
|
44
|
-
"@dxos/util": "0.8.2-main.
|
|
36
|
+
"@dxos/async": "0.8.2-main.7503643",
|
|
37
|
+
"@dxos/echo-db": "0.8.2-main.7503643",
|
|
38
|
+
"@dxos/debug": "0.8.2-main.7503643",
|
|
39
|
+
"@dxos/echo-schema": "0.8.2-main.7503643",
|
|
40
|
+
"@dxos/invariant": "0.8.2-main.7503643",
|
|
41
|
+
"@dxos/live-object": "0.8.2-main.7503643",
|
|
42
|
+
"@dxos/log": "0.8.2-main.7503643",
|
|
43
|
+
"@dxos/schema": "0.8.2-main.7503643",
|
|
44
|
+
"@dxos/util": "0.8.2-main.7503643"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@antv/graphlib": "^2.0.4",
|
|
48
48
|
"@antv/layout": "^1.2.13",
|
|
49
49
|
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
50
|
-
"@dxos/echo-db": "0.8.2-main.
|
|
51
|
-
"@dxos/
|
|
52
|
-
"@dxos/
|
|
50
|
+
"@dxos/echo-db": "0.8.2-main.7503643",
|
|
51
|
+
"@dxos/random": "0.8.2-main.7503643",
|
|
52
|
+
"@dxos/echo-schema": "0.8.2-main.7503643"
|
|
53
53
|
},
|
|
54
54
|
"publishConfig": {
|
|
55
55
|
"access": "public"
|
package/src/create.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type ReactiveEchoObject } from '@dxos/echo-db';
|
|
6
|
+
import { FormatEnum, getSchema } from '@dxos/echo-schema';
|
|
7
|
+
import { live } from '@dxos/live-object';
|
|
8
|
+
import { log } from '@dxos/log';
|
|
9
|
+
import { getSchemaProperties } from '@dxos/schema';
|
|
10
|
+
|
|
11
|
+
import { GraphModel } from './model';
|
|
12
|
+
import { Graph, type GraphNode } from './types';
|
|
13
|
+
import { createEdgeId } from './util';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new reactive graph from a set of ECHO objects.
|
|
17
|
+
* References are mapped onto graph edges.
|
|
18
|
+
*/
|
|
19
|
+
export const createGraph = (objects: ReactiveEchoObject<any>[]): GraphModel<GraphNode<ReactiveEchoObject<any>>> => {
|
|
20
|
+
const graph = new GraphModel<GraphNode<ReactiveEchoObject<any>>>(live(Graph, { nodes: [], edges: [] }));
|
|
21
|
+
|
|
22
|
+
// Map objects.
|
|
23
|
+
objects.forEach((object) => {
|
|
24
|
+
graph.addNode({ id: object.id, type: object.typename, data: object });
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Find references.
|
|
28
|
+
objects.forEach((object) => {
|
|
29
|
+
const schema = getSchema(object);
|
|
30
|
+
if (!schema) {
|
|
31
|
+
log.info('no schema for object', { id: object.id.slice(0, 8) });
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Parse schema to follow referenced objects.
|
|
36
|
+
for (const prop of getSchemaProperties(schema.ast, object)) {
|
|
37
|
+
if (prop.format === FormatEnum.Ref) {
|
|
38
|
+
const source = object;
|
|
39
|
+
const target = object[prop.name]?.target;
|
|
40
|
+
if (target) {
|
|
41
|
+
graph.addEdge({
|
|
42
|
+
id: createEdgeId({ source: source.id, target: target.id, relation: String(prop.name) }),
|
|
43
|
+
source: source.id,
|
|
44
|
+
target: target.id,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return graph;
|
|
52
|
+
};
|
package/src/index.ts
CHANGED
package/src/model.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { failedInvariant, invariant } from '@dxos/invariant';
|
|
|
7
7
|
import { getSnapshot } from '@dxos/live-object';
|
|
8
8
|
import { type MakeOptional, isNotFalsy, removeBy, stripUndefined } from '@dxos/util';
|
|
9
9
|
|
|
10
|
-
import { type
|
|
10
|
+
import { type BaseGraphEdge, type BaseGraphNode, type Graph, type GraphEdge, type GraphNode } from './types';
|
|
11
11
|
import { createEdgeId } from './util';
|
|
12
12
|
|
|
13
13
|
/**
|
package/src/util.ts
CHANGED
|
@@ -2,15 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type ReactiveEchoObject } from '@dxos/echo-db';
|
|
6
|
-
import { FormatEnum, getSchema } from '@dxos/echo-schema';
|
|
7
5
|
import { invariant } from '@dxos/invariant';
|
|
8
|
-
import { live } 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
6
|
|
|
15
7
|
const KEY_REGEX = /\w+/;
|
|
16
8
|
|
|
@@ -28,42 +20,3 @@ export const parseEdgeId = (id: string): EdgeMeta => {
|
|
|
28
20
|
invariant(source.length && target.length);
|
|
29
21
|
return { source, relation: relation.length ? relation : undefined, target };
|
|
30
22
|
};
|
|
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>>>(live(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
|
-
};
|