-
Notifications
You must be signed in to change notification settings - Fork 6
/
neo4j.js
59 lines (56 loc) · 2.13 KB
/
neo4j.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
51
52
53
54
55
56
57
58
59
const neo4j = require('neo4j-driver').v1;
module.exports = function (RED) {
function NulliNeo4j(config) {
RED.nodes.createNode(this, config);
var node = this;
//console.log(`url = ${config.url}`);
const driver = neo4j.driver(config.url, neo4j.auth.basic(config.username, config.password));
const session = driver.session();
if (session) {
node.status({
fill: "green",
shape: "dot",
text: "node-red:common.status.connected"
});
node.on('input', function (msg) {
//console.log(`decoding QR data from ${msg.payload}`);
var query = config.query || msg.query;
let params = null;
if (typeof (msg.params) === 'string') {
params = JSON.parse(msg.params);
} else {
params = msg.params;
}
//console.log(`params: ${params}`);
const resultPromise = session.run(query, params);
var array_result = {
payload: []
}
resultPromise.then(result => {
session.close();
if (result.records.length > 1) {
result.records.forEach(function (item, index, array) {
array_result.payload.push(item.get(0).properties);
});
msg.payload = array_result.payload;
node.send([null, msg]);
} else {
msg.payload = result.records[0].get(0).properties;
node.send([msg, null]);
}
});
});
} else {
node.status({
fill: "red",
shape: "dot",
text: "node-red:common.status.disconnected"
});
}
node.on('close', function () {
// tidy up any state
driver.close();
});
}
RED.nodes.registerType("node-red-contrib-nulli-neo4j", NulliNeo4j);
}