forked from nullibrew/node-red-contrib-nulli-neo4j
-
Notifications
You must be signed in to change notification settings - Fork 4
/
neo4j.js
139 lines (121 loc) · 4.4 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const neo4j = require('neo4j-driver')
module.exports = function (RED) {
function Neo4jBolt (config) {
RED.nodes.createNode(this, config)
var node = this
var sessions = []
var readySessionList = []
node.server = RED.nodes.getNode(config.server)
const driver = node.server.driver
// set up a list of sessions for later use, to avoid session allocation error later on
for( i=0; i<config.sessions; i++){
try {
sessions.push(driver.session({ database: config.database || 'neo4j' }));
readySessionList.push(i)
} catch (err) {
console.log(err)
break
}
}
if (readySessionList.length > 0) {
node.status({
fill: 'green',
shape: 'dot',
text: `connected: ${readySessionList.length} sesions`
})
node.on('input', function (msg) {
var query = config.query || msg.query
if( readySessionList.length > 0 ){
// remove and use a session
var readySession = readySessionList.shift()
var boltSession = driver.session({
database: msg.database || config.database || 'neo4j'
});
let params = null
if (typeof (msg.params) === 'string') {
params = JSON.parse(msg.params)
} else {
params = msg.params
}
function processInteger (integer) {
if (integer.constructor.name === 'Integer') {
return integer.toNumber()
}
return integer
}
function processRecord (record) {
if (record.constructor.name === 'Integer') {
return record.toNumber()
}
if (record.constructor.name === 'Path') {
record.start.identity = processInteger(record.start.identity)
record.end.identity = processInteger(record.end.identity)
record.segments = record.segments.map(segment => {
segment.start.identity = processInteger(segment.start.identity)
segment.end.identity = processInteger(segment.end.identity)
segment.relationship.identity = processInteger(segment.relationship.identity)
segment.relationship.start = processInteger(segment.relationship.start)
segment.relationship.end = processInteger(segment.relationship.end)
return segment
})
return record
}
if (record.constructor.name === 'Relationship') {
record.identity = processInteger(record.identity)
record.start = processInteger(record.start)
record.end = processInteger(record.end)
return record
}
if (record.constructor.name === 'Node') {
record.identity = processInteger(record.identity)
return record
}
return record
}
boltSession.run(query, params).then(result => {
if (result.records.length > 1) {
msg.payload = [];
result.records.forEach(function (record, index, array) {
let itm = {};
record.forEach(function (item, index, array) {
itm[index] = processRecord(item)
})
msg.payload.push(itm)
})
node.send([null, msg, null])
} else if (result.records.length == 1) {
let itm = {};
result.records[0].forEach(function (item, index, array) {
itm[index] = processRecord(item)
})
msg.payload = itm
node.send([msg, null, null])
} else {
msg.payload = [];
node.send([msg, null, null]);
}
})
.catch(err => {
node.error(err, msg);
}).finally(() => {
readySessionList.push(readySession)
})
} else {
// no sessions available, send the message out on the third port for further processing bty caller
node.send([null, null, msg])
}
})
} else {
node.status({
fill: 'red',
shape: 'dot',
text: 'node-red:common.status.disconnected'
})
}
node.on('close', function () {
sessions.map(s => s.close())
driver.close()
})
}
RED.nodes.registerType('neo4j-bolt', Neo4jBolt)
}