An expressive, serialisable format for Graph-like data structures.
ugraph, a parser implementation for TS/JS on NPM.yarn add ugraph
import { parse } from "ugraph";
const data = [
{ $id: "abc" },
{ $id: "foo", prop: { $node: "bar" } },
{ $id: "bar", prop: { $node: "foo" } },
];
const $graph = parse({ data });
// handle errors
if ($graph.error) ...
// use valid graph
const graph = $graph.value
// parses consistent object references
>>> foo = graph.nodes.get("foo")
>>> bar = graph.nodes.get("bar")
>>> foo.prop === bar
// true
>>> bar.prop === foo
// true
Node{ $id: string }
"$id") property.{
"$id": "foo"
}
Reference (i.e. Edges){ $node: string }
{
"property": { "$node": "bar" }
}
{ $node: Node }
{
"property": { "$node": { "$id": "bar", /* ... */ } }
}
{ $node: string | Node, [key]: any }
{
"friends": [
{
// reference
"$node": "personA",
// properties
"friendsSince": "2022-01-01T00:00:00.0Z",
"friendsUntil": "2023-02-02T00:00:00.0Z"
}
],
// ...
"users": [
// node
{ "$id": "personA" }
]
}
{ $node: string | Node, [key]: Reference }
{
"favourites": [
{
// reference (1)
"$node": "note:j78arsmqw4",
"addedAt": "2022-01-01T00:00:00.0Z",
"addedBy": {
// reference (2)
"$node": "user:vl1vh2i22i"
}
}
],
// ...
"notes": [
// node (1)
{ "$id": "note:j78arsmqw4" }
],
"users": [
// node (2)
{ "$id": "user:vl1vh2i22i" }
]
}
Accessors (i.e. References to Node Properties){ $node: string | Node, $path: string }
"$path" uses JSON Path format.[
{
"$id": "aaa",
"name": "foo"
},
{
"$id": "bbb",
"name": { "$node": "aaa", "$path": "$.name" }
}
]
$path.[
{
"$id": "aaa",
"endpoint": "hello",
},
{
"$id": "bbb",
// "url" computed property on runtime interface
// -> e.g. Webhook.url() => "https://mysite.com/webhooks/hello"
"url": { "$node": "aaa", "$path": "$.url" }
}
]
Copyright 2022 Transformd Pty Ltd
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Generated using TypeDoc