Skip to content

Instantly share code, notes, and snippets.

@timlrx
Created April 22, 2021 09:34
Show Gist options
  • Save timlrx/6a4feee5a6e5910ee3763b75f85db5de to your computer and use it in GitHub Desktop.
Save timlrx/6a4feee5a6e5910ee3763b75f85db5de to your computer and use it in GitHub Desktop.
neo4j to motif format
import Record from 'neo4j-driver/types/record';
export const buildNode = (n: any) => {
let node: Node = {
id: `node-${n.identity.toString()}`,
labels: n.labels[0],
};
if (n.properties) {
for (let [key, value] of Object.entries(n.properties)) {
node[key] = value instanceof Neo4j.types.Integer ? value.toInt() : value;
}
}
return { ...node };
};
export const buildEdge = (e: any) => {
let edge: Edge = {
id: `edge-${e.identity.toString()}`,
source: `node-${e.start.toString()}`,
target: `node-${e.end.toString()}`,
erelationship: e.type,
};
if (e.properties) {
for (let [key, value] of Object.entries(e.properties)) {
edge[key] = value instanceof Neo4j.types.Integer ? value.toInt() : value;
}
}
return { ...edge };
};
/* Adapted from https://github.com/neo4j-contrib/neovis.js/blob/master/src/neovis.js */
export const toMotifFormat = (records: Record[]): GraphData => {
let nodes: Node[] = [];
let edges: Edge[] = [];
records.forEach((record) => {
Object.values(record.toObject()).map(async (v) => {
if (v instanceof Neo4j.types.Node) {
let node = buildNode(v);
try {
nodes.push(node);
} catch (e) {
console.log(e, 'error');
}
} else if (v instanceof Neo4j.types.Relationship) {
let edge = buildEdge(v);
edges.push(edge);
} else if (v instanceof Neo4j.types.Path) {
let startNode = buildNode(v.start);
let endNode = buildNode(v.end);
nodes.push(startNode);
nodes.push(endNode);
for (let obj of v.segments) {
nodes.push(buildNode(obj.start));
nodes.push(buildNode(obj.end));
edges.push(buildEdge(obj.relationship));
}
} else if (v instanceof Array) {
for (let obj of v) {
if (obj instanceof Neo4j.types.Node) {
let node = buildNode(obj);
nodes.push(node);
} else if (obj instanceof Neo4j.types.Relationship) {
let edge = buildEdge(obj);
edges.push(edge);
}
}
} else {
console.log('Invalid format');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment