Skip to content
Snippets Groups Projects
Commit 11644b6e authored by Marc Feger's avatar Marc Feger
Browse files

Add graph structure

parent dfe505c8
Branches
No related tags found
No related merge requests found
import './neo'
import {Graph} from "./js/graph/graph";
import {Edge} from "./js/graph/edge";
import {Node} from "./js/graph/node";
import {Neo4JAdapter} from "./js/neo4j/neo"
// TODO: Dockerize and add a Neo4j-Server
// TODO: Why are there so many results? Improve the loops
// TODO: Lift the nodes and relations into a good representation
// TODO: Add a class for the data
// TODO: Lift the data in a graph and visualize them
\ No newline at end of file
var graph = new Graph();
var neo = new Neo4JAdapter('neo4j', 'W7uFSy$ywR3M3ck');
graph = neo.getGraph();
console.log(graph);
\ No newline at end of file
export class Edge {
constructor(id, properties, type, start, end) {
this.id = id;
this.properties = properties;
this.type = type;
this.start = start;
this.end = end;
}
}
\ No newline at end of file
import {Edge} from './edge'
export class Graph {
constructor() {
this.edges = {};
this.nodes = {};
}
addEdge(edge) {
if (!(edge.id in this.edges)) {
this.edges[edge.id] = edge;
console.log("Added: " + JSON.stringify(edge) + " to edges.");
}
};
addNode(node) {
if (!(node.id in this.nodes)) {
this.nodes[node.id] = node;
console.log("Added: " + JSON.stringify(node) + " to nodes.");
}
};
_asList(dict) {
var list = [];
for (var key in dict) {
if (dict.hasOwnProperty(key)) {
list.push(dict[key]);
}
}
return list;
}
get edgesAsList() {
return this._asList(this.edges);
}
get nodesAsList() {
return this._asList(this.nodes);
}
}
\ No newline at end of file
export class Node {
constructor(id, properties, labels) {
this.id = id;
this.properties = properties;
this.labels = labels
}
}
\ No newline at end of file
import {v1 as neo4j} from 'neo4j-driver'
import {Edge} from '../graph/edge'
import {Node} from '../graph/node'
import {Graph} from '../graph/graph'
export class Neo4JAdapter {
constructor(username, password) {
this.username = username;
this.password = password;
this.authToken = neo4j.auth.basic(this.username, this.password);
}
getGraph(graph) {
const driver = neo4j.driver('bolt://localhost:7687', this.authToken, {
encrypted: false
});
const session = driver.session();
const resultPromise = session.run(
'match (a:User)<-[r]-() return *'
);
var graph = new Graph();
resultPromise.then(result => {
session.close();
result.records.forEach(function (element) {
element.forEach(function (subelement) {
if (subelement instanceof neo4j.types.Node) {
graph.addNode(new Node(subelement.identity.toInt(), subelement.properties, subelement.labels));
} else if (subelement instanceof neo4j.types.Relationship) {
graph.addEdge(new Edge(subelement.identity.toInt(), subelement.properties, subelement.type, subelement.start.toInt(), subelement.end.toInt()));
}
});
});
driver.close();
});
return graph
}
}
import {v1 as neo4j} from 'neo4j-driver'
const authToken = neo4j.auth.basic('neo4j', 'W7uFSy$ywR3M3ck');
console.log(authToken);
const driver = neo4j.driver('bolt://localhost:7687', authToken, {
encrypted: false
});
const session = driver.session();
const personName = 'Marc';
const resultPromise = session.run(
'match (a:Person)-[r:IS_FRIEND]->(b:Person) RETURN a,b,r',
{name: personName}
);
resultPromise.then(result => {
session.close();
result.records.forEach(function (element) {
element.forEach(function (subelement) {
console.log("++++++ " + subelement.constructor.name + " ++++++");
if (subelement instanceof neo4j.types.Node) {
console.log("Properties: " + JSON.stringify(subelement.properties));
console.log("ID: " + subelement.identity.toInt());
console.log("Labels: " + subelement.labels);
} else if (subelement instanceof neo4j.types.Relationship) {
console.log("Type: " + subelement.type);
console.log("Properties: " + JSON.stringify(subelement.properties));
console.log("Start: " + subelement.start.toInt());
console.log("End: " + subelement.end.toInt());
}
});
});
driver.close();
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment