npm install @neo4j/graph-schema-utils
There are two parts to this repo:
- A JSON schema that describes how a graph schema should be expressed in JSON.
- Utility functions that help the consumers to validate and work with a graph schema.
The JSON that describes the shape of a graph schema is available in the source code (packages/json-schema/json-schema.json
) and published on: https://dist.neo4j.org/json-graph-schema/ (to be published, not in place yet)
This function is needed to perform a validation on a graph schema. The validateSchema function compares the output against the JSON schema.
import { validateSchema } from "@neo4j/graph-schema-utils";
validateSchema(jsonSchema, graphSchema);
Since the references in the JSON document are references by id:s, there's a parser utility that hydrates the references and makes it easy to work with the schema.
import { formatters } from "@neo4j/graph-schema-utils";
const parsed = formatters.json.fromJson(graphSchemaJsonString);
You can also create a schema model programatically. Example:
import { model } from "@neo4j/graph-schema-utils";
const labels = [
new model.NodeLabel("l1", "Person"),
new model.NodeLabel("l1", "Movie"),
];
const relationshipTypes = [new model.RelationshipType("rt1", "ACTED_IN")];
const properties = [
new model.Property("name", new model.PropertyBaseType("string"), true),
new model.Property("title", new model.PropertyBaseType("string"), true),
new model.Property(
"roles",
new model.PropertyArrayType(new model.PropertyBaseType("string")),
false
),
];
const nodeObjectTypes = [
new model.NodeObjectType("n1", [labels[0]], [properties[0]]), // (:Person {name}) node type
new model.NodeObjectType("n2", [labels[1]], [properties[1]]), // (:Movie {title}) node type
];
const relationshipObjectTypes = [
// (:Person {name})-[:ACTED_IN {roles}]->(:Movie {title})
new model.RelationshipObjectType(
"r1",
relationshipTypes[0],
nodeObjectTypes[0],
nodeObjectTypes[1],
[properties[2]]
),
];
const graphSchema = new model.GraphSchema(
nodeObjectTypes,
relationshipObjectTypes
);
If you need to transport or persist the schema, you can serialize the model into the JSON represenation.
import { formatters } from "@neo4j/graph-schema-utils";
const serialized = formatters.json.toJson(graphSchema);
We welcome contributions to this repo. Fork this repo and open a PR!
To run a single test run:
npm run test
To watch for changes and run tests on change:
npm run test:watch
To build the TypeScript:
npm run build