forked from nullibrew/node-red-contrib-nulli-neo4j
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathneo4j.js
155 lines (134 loc) · 5.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const neo4j = require('neo4j-driver')
module.exports = function (RED) {
function Neo4jBolt (config) {
RED.nodes.createNode(this, config)
var node = this
var sessions = []
var readySessionList = []
const SESSION_TIMEOUT_MS = 4000; // 4 seconds of inactivity
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'
});
if (boltSession.inactivityTimeout) {
clearTimeout(boltSession.inactivityTimeout);
}
let params = null
if (typeof (msg.params) === 'string') {
params = JSON.parse(msg.params)
} else {
params = msg.params
}
function processInteger (integer) {
if (integer === null) return null;
if (integer.constructor.name === 'Integer') {
return integer.toNumber()
}
return integer
}
function processRecord (record) {
if (record === null) return null;
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(() => {
// Return the session to the ready pool and set an inactivity timeout
readySessionList.push(readySession)
boltSession.inactivityTimeout = setTimeout(() => {
// Close the session if not used within the timeout period
boltSession.close();
sessions[readySession] = null; // Remove from the session pool
//console.log(`Session ${readySession} closed due to inactivity.`);
}, SESSION_TIMEOUT_MS);
})
} 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)
}