-
Notifications
You must be signed in to change notification settings - Fork 114
/
cypher.js
46 lines (42 loc) · 1.53 KB
/
cypher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function cypher(model) {
function props(element) {
var props = {};
element.properties().list().forEach(function (property) {
props[property.key] = property.value;
});
return props;
}
function isIdentifier(name) {
return /^[_a-zA-Z]\w*$/.test(name);
}
function quote(name) {
return isIdentifier(name) ? name : "`" + name + "`";
}
function render(props) {
var res = "";
for (var key in props) {
if (res.length > 0) res += ",";
if (props.hasOwnProperty(key)) {
res += quote(key) + ":";
var value = props[key];
res += typeof value == "string" && value[0] != "'" && value[0] != '"' ? "'" + value + "'" : value;
}
}
return res.length == 0 ? "" : "{" + res + "}";
}
var statements = [];
model.nodeList().forEach(function (node) {
statements.push("(" + quote(node.id) +" :" + quote(node.caption() || "Node") + " " + render(props(node)) + ") ");
});
model.relationshipList().forEach(function (rel) {
statements.push("(" + quote(rel.start.id) +
")-[:`" + quote(rel.relationshipType()||"RELATED_TO") +
"` " + render(props(rel)) +
"]->("+ quote(rel.end.id) +")"
);
});
if (statements.length==0) return "";
return "CREATE \n " + statements.join(",\n ");
};
if (typeof exports != "undefined") exports.cypher=cypher
gd.cypher=function(model) {return cypher(model || this.model());}