@dxos/graph 0.8.2-main.f11618f → 0.8.2-staging.42af850
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 +138 -97
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +136 -93
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +138 -97
- 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 +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/model.d.ts +31 -36
- package/dist/types/src/model.d.ts.map +1 -1
- package/dist/types/src/selection.d.ts +22 -0
- package/dist/types/src/selection.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +35 -36
- package/dist/types/src/types.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/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +19 -18
- package/src/index.ts +1 -0
- package/src/model.test.ts +78 -10
- package/src/model.ts +71 -20
- package/src/selection.ts +70 -0
- package/src/types.ts +39 -29
- package/src/util.ts +0 -47
package/src/types.ts
CHANGED
|
@@ -2,38 +2,50 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { Schema } from 'effect';
|
|
6
|
+
|
|
6
7
|
import { type Specialize } from '@dxos/util';
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
//
|
|
10
|
+
// Node
|
|
11
|
+
//
|
|
12
|
+
|
|
13
|
+
// TODO(burdon): Make type extensible (i.e., not dependent on `data` property)?
|
|
14
|
+
export const BaseGraphNode = Schema.Struct({
|
|
15
|
+
id: Schema.String,
|
|
16
|
+
type: Schema.optional(Schema.String),
|
|
17
|
+
data: Schema.optional(Schema.Any),
|
|
12
18
|
});
|
|
13
19
|
|
|
14
20
|
/** Raw base type. */
|
|
15
|
-
export type BaseGraphNode =
|
|
21
|
+
export type BaseGraphNode = Schema.Schema.Type<typeof BaseGraphNode>;
|
|
16
22
|
|
|
17
23
|
/** Typed node data. */
|
|
18
|
-
|
|
24
|
+
type GraphNode<Data = any, Optional extends boolean = false> = Specialize<
|
|
19
25
|
BaseGraphNode,
|
|
20
26
|
Optional extends true ? { data?: Data } : { data: Data }
|
|
21
27
|
>;
|
|
22
28
|
|
|
23
29
|
export declare namespace GraphNode {
|
|
24
|
-
export type
|
|
30
|
+
export type Any = GraphNode<any, true>;
|
|
31
|
+
export type Optional<Data = any> = GraphNode<Data, true>;
|
|
32
|
+
export type Required<Data = any> = GraphNode<Data, false>;
|
|
25
33
|
}
|
|
26
34
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
//
|
|
36
|
+
// Edge
|
|
37
|
+
//
|
|
38
|
+
|
|
39
|
+
export const BaseGraphEdge = Schema.Struct({
|
|
40
|
+
id: Schema.String,
|
|
41
|
+
type: Schema.optional(Schema.String),
|
|
42
|
+
source: Schema.String,
|
|
43
|
+
target: Schema.String,
|
|
44
|
+
data: Schema.optional(Schema.Any),
|
|
33
45
|
});
|
|
34
46
|
|
|
35
47
|
/** Raw base type. */
|
|
36
|
-
export type BaseGraphEdge =
|
|
48
|
+
export type BaseGraphEdge = Schema.Schema.Type<typeof BaseGraphEdge>;
|
|
37
49
|
|
|
38
50
|
/** Typed edge data. */
|
|
39
51
|
export type GraphEdge<Data = any, Optional extends boolean = false> = Specialize<
|
|
@@ -42,21 +54,19 @@ export type GraphEdge<Data = any, Optional extends boolean = false> = Specialize
|
|
|
42
54
|
>;
|
|
43
55
|
|
|
44
56
|
export declare namespace GraphEdge {
|
|
45
|
-
export type
|
|
57
|
+
export type Any = GraphEdge<any, true>;
|
|
58
|
+
export type Optional<Data = any> = GraphEdge<Data, true>;
|
|
59
|
+
export type Required<Data = any> = GraphEdge<Data, false>;
|
|
46
60
|
}
|
|
47
61
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
export const Graph = S.Struct({
|
|
57
|
-
id: S.optional(S.String),
|
|
58
|
-
nodes: S.mutable(S.Array(ExtendableBaseGraphNode)),
|
|
59
|
-
edges: S.mutable(S.Array(BaseGraphEdge)),
|
|
62
|
+
//
|
|
63
|
+
// Graph
|
|
64
|
+
//
|
|
65
|
+
|
|
66
|
+
export const Graph = Schema.Struct({
|
|
67
|
+
id: Schema.optional(Schema.String),
|
|
68
|
+
nodes: Schema.mutable(Schema.Array(BaseGraphNode)),
|
|
69
|
+
edges: Schema.mutable(Schema.Array(BaseGraphEdge)),
|
|
60
70
|
});
|
|
61
71
|
|
|
62
|
-
export type Graph =
|
|
72
|
+
export type Graph = Schema.Schema.Type<typeof Graph>;
|
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
|
-
};
|