-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathas400.js
executable file
·60 lines (52 loc) · 2.06 KB
/
as400.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
module.exports = function(RED) {
"use strict";
var as400 = require('node-jt400');
function as400Node(config) {
RED.nodes.createNode(this,config);
this.host = config.host;
this.naming = config.naming || system;
var node = this;
var db = as400.pool({host: node.host, user: node.credentials.user, password: node.credentials.password, naming: node.naming});
node.query = function(node, msg){
if ( msg.payload !== null && typeof msg.payload === 'string' && msg.payload !== '') {
db.query(msg.payload).then(function (rows) {
if (rows) {
var c = rows.length;
rows.forEach(function(row) {
c--
if(c==0){
node.send({ topic: msg.topic, payload: row, complete: true });
}else{
node.send({ topic: msg.topic, payload: row });
}
})
node.send([ null, { topic: msg.topic, control: 'end' }]);
}else{
node.error(msg);
}
});
}
else {
if (msg.payload === null) {
node.error("msg.payload : the query is not defined");
}
if (typeof msg.payload !== 'string') {
node.error("msg.payload : the query is not defined as a string");
}
if (typeof msg.payload === 'string' && msg.payload === '') {
node.error("msg.payload : the query string is empty");
}
}
}
node.on('input', function(msg) {
node.send([ null, { control: 'start', query: msg.payload } ]);
node.query(node, msg);
});
}
RED.nodes.registerType("as400",as400Node,{
credentials: {
user: {type: "text"},
password: {type: "password"}
}
});
}