Skip to content

Utility library to work with the Graph Schema JSON representation

License

Notifications You must be signed in to change notification settings

neo4j/graph-schema-json-js-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ced3c20 · Dec 19, 2024
Dec 19, 2024
Sep 12, 2023
Dec 18, 2024
Dec 19, 2024
Oct 31, 2023
Aug 30, 2024
Feb 7, 2023
Jan 25, 2023
Dec 12, 2022
Jan 2, 2023
Nov 28, 2023
Dec 18, 2024
Nov 28, 2023

Repository files navigation

Graph Schema JSON utility library

Install

npm install @neo4j/graph-schema-utils

How to consume

There are two parts to this repo:

  1. A JSON schema that describes how a graph schema should be expressed in JSON.
  2. Utility functions that help the consumers to validate and work with a graph schema.

JSON-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)

Validate

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);

Parse

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);

Model

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
);

Serialize

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);

Contribute

We welcome contributions to this repo. Fork this repo and open a PR!

Run test

To run a single test run:

npm run test

To watch for changes and run tests on change:

npm run test:watch

Build

To build the TypeScript:

npm run build