Skip to content

Instantly share code, notes, and snippets.

@dmccreary
Created May 1, 2020 13:28
Show Gist options
  • Save dmccreary/3a87807068da35986dbd22740f98a3de to your computer and use it in GitHub Desktop.
Save dmccreary/3a87807068da35986dbd22740f98a3de to your computer and use it in GitHub Desktop.
Node.js script that will convert a TigerGraph schema archive JSON file into GSQL.
#!/usr/local/bin/node
# written by Josh Meekhof
var fs = require("fs");
var content = fs.readFileSync("schema.json");
var schema = JSON.parse(content);
var vertextT = [];
var edgeT = [];
const joiner = (acc, val) => acc + (val + "\n");
vertices = schema.VertexTypes.map(call_vertex_handler(vertextT));
edges = schema.EdgeTypes.map(call_edge_handler(edgeT));
graph = "CREATE GRAPH " + schema.GraphName + "( " + vertextT.concat(edgeT).join(", ") + ") ";
statements = vertices.reduce(joiner) + edges.reduce(joiner) + graph;
console.log(statements);
/*
console.log(vertextT);
console.log(edgeT);
console.log(vertices);
*/
function call_edge_handler(et) {
function e_handler(e){
return edge_handler(e,et);
}
return e_handler;
}
function edge_handler(edge, edgeTypes) {
edgeTypes.push(edge.Name);
var strEdge = "CREATE " + (edge.IsDirected ?"":"UN") + "DIRECTED EDGE " + edge.Name;
var strAttributes = edge.Attributes.map(attribute_handler);
strAttributes.unshift("TO " + edge.ToVertexTypeName);
strAttributes.unshift("FROM " + edge.FromVertexTypeName);
strEdge = strEdge + " ( " + strAttributes.join(", ") + " ) ";
var cfg = config_handler(edge.Config);
strEdge = strEdge + cfg
return strEdge;
}
function call_vertex_handler(vt) {
function vert_handler(v){
return vertex_handler(v,vt);
}
return vert_handler;
}
function vertex_handler(vertex, vertexTypes) {
//Create a string for a CREATE VERTEX Gsql command
vertexTypes.push(vertex.Name);
var strVertex = "CREATE VERTEX " + vertex.Name;
//Create an array to hold all the attributes, treating primary ID as an attribute
var strAttribute = vertex.Attributes.map(attribute_handler);
strAttribute.unshift("primary_id " + attribute_handler(vertex.PrimaryId));
strVertex = strVertex + "(" + strAttribute.join(", ") + ") ";
var cfg = config_handler(vertex.Config);
strVertex = strVertex + cfg;
return strVertex;
}
function attribute_handler(attribute) {
var strAttribute = attribute.AttributeName + " " + attribute.AttributeType.Name;
return strAttribute;
}
function config_handler(config) {
var strConf = "WITH "
var arConf = [];
var cfgKeys = Object.keys(config);
cfgKeys.forEach(function(key) {
arConf.push(key + '="' + config[key] + '"');
});
strConf = strConf + arConf.join(", ");
return strConf
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment