@dxos/graph 0.8.2-main.5885341 → 0.8.2-main.85fa0e5
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 +78 -81
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +75 -76
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +78 -81
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/index.d.ts +0 -1
- 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 +8 -0
- package/dist/types/src/util.d.ts.map +1 -1
- package/package.json +13 -13
- package/src/index.ts +0 -1
- package/src/model.ts +1 -1
- package/src/util.ts +47 -0
- package/dist/types/src/create.d.ts +0 -9
- package/dist/types/src/create.d.ts.map +0 -1
- package/src/create.ts +0 -52
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 Graph, type GraphNode, type GraphEdge, type BaseGraphNode, type BaseGraphEdge } from './types';
|
|
11
11
|
import { createEdgeId } from './util';
|
|
12
12
|
|
|
13
13
|
/**
|
package/src/util.ts
CHANGED
|
@@ -2,7 +2,15 @@
|
|
|
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';
|
|
5
7
|
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';
|
|
6
14
|
|
|
7
15
|
const KEY_REGEX = /\w+/;
|
|
8
16
|
|
|
@@ -20,3 +28,42 @@ export const parseEdgeId = (id: string): EdgeMeta => {
|
|
|
20
28
|
invariant(source.length && target.length);
|
|
21
29
|
return { source, relation: relation.length ? relation : undefined, target };
|
|
22
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>>>(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
|
+
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { type ReactiveEchoObject } from '@dxos/echo-db';
|
|
2
|
-
import { GraphModel } from './model';
|
|
3
|
-
import { type GraphNode } from './types';
|
|
4
|
-
/**
|
|
5
|
-
* Creates a new reactive graph from a set of ECHO objects.
|
|
6
|
-
* References are mapped onto graph edges.
|
|
7
|
-
*/
|
|
8
|
-
export declare const createGraph: (objects: ReactiveEchoObject<any>[]) => GraphModel<GraphNode<ReactiveEchoObject<any>>>;
|
|
9
|
-
//# sourceMappingURL=create.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../../src/create.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAMxD,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAS,KAAK,SAAS,EAAE,MAAM,SAAS,CAAC;AAGhD;;;GAGG;AACH,eAAO,MAAM,WAAW,YAAa,kBAAkB,CAAC,GAAG,CAAC,EAAE,KAAG,UAAU,CAAC,SAAS,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAiC7G,CAAC"}
|
package/src/create.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
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
|
-
};
|