@dxos/graph 0.8.2-staging.7ac8446 → 0.8.2

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/src/types.ts CHANGED
@@ -2,38 +2,50 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { S } from '@dxos/echo-schema';
5
+ import { Schema } from 'effect';
6
+
6
7
  import { type Specialize } from '@dxos/util';
7
8
 
8
- export const BaseGraphNode = S.Struct({
9
- id: S.String,
10
- type: S.optional(S.String),
11
- data: S.optional(S.Any),
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 = S.Schema.Type<typeof BaseGraphNode>;
21
+ export type BaseGraphNode = Schema.Schema.Type<typeof BaseGraphNode>;
16
22
 
17
23
  /** Typed node data. */
18
- export type GraphNode<Data = any, Optional extends boolean = false> = Specialize<
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 Optional<T = any> = GraphNode<T, true>;
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
- export const BaseGraphEdge = S.Struct({
28
- id: S.String,
29
- type: S.optional(S.String),
30
- data: S.optional(S.Any),
31
- source: S.String,
32
- target: S.String,
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 = S.Schema.Type<typeof 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 Optional<T = any> = GraphEdge<T, true>;
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
- * Allows any additional properties on graph nodes.
50
- */
51
- const ExtendableBaseGraphNode = S.extend(BaseGraphNode, S.Struct({}, { key: S.String, value: S.Any }));
52
-
53
- /**
54
- * Generic graph.
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 = S.Schema.Type<typeof 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 } from '@dxos/echo-schema';
7
5
  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
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>>>(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
- };