-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodefactory-db.js
50 lines (42 loc) · 1.5 KB
/
nodefactory-db.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
47
48
49
50
const Node = require('./node');
class NodeFactoryDB {
constructor(graph, db) {
this.db = db;
this.graph = graph;
}
getNode(nodeId){
let record = this.db.prepare('SELECT * FROM nodes WHERE id = ?').get(nodeId);
let node = this.createNode(record);
return node;
}
createNode = (record)=> {
let node = new Node (
record.id,
record.name === 'null' ? null : record.name,
record.latitude,
record.longitude
);
this.graph.push(node);
return node;
};
/**
* This functions does two things,
* 1- check if the node is already in the graph if not it create it.
* 2- return all the neighbors from a given node.
* */
getNeighbors(node){
let data = this.db.prepare(`SELECT * from nodes
WHERE id IN (
SELECT destination FROM relations
where source = ?
)`)
.all(node.id);
let classifiedNodes = data.reduce((result, record) => {
let n = this.graph.get(record);
n ? result.alreadyDiscoveredNodes.push(n) : result.newNodes.push(record);
return result;
}, {alreadyDiscoveredNodes: [], newNodes: []});
return classifiedNodes.alreadyDiscoveredNodes.concat(classifiedNodes.newNodes.map(this.createNode));
}
}
module.exports = NodeFactoryDB;