forked from efa2000/node-red-contrib-odbc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathodbc.js
58 lines (49 loc) · 1.51 KB
/
odbc.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
module.exports = function(RED) {
const mustache = require("mustache");
const odbc = require('odbc');
function connection(config) {
RED.nodes.createNode(this, config);
this.connection = `${config.connection};UID=${config.username};PWD=${config.password}`;
}
RED.nodes.registerType("ODBC_CONNECTION", connection, {
credentials: {
username: {type:"text"},
password: {type:"password"}
}
});
function odbc(config) {
RED.nodes.createNode(this, config);
const node = this;
node.connection = RED.nodes.getNode(config.odbc);
node.query = config.query;
node.outfield = config.outField;
odbc.pool(node.connection, (error, pool) => {
if (error) {
node.error(error);
node.status({fill: "red", shape: "ring", text: error.message});
}
pool.connect((error, connection) => {
if (error) {
node.error(error);
node.status({fill: "red", shape: "ring", text: error.message});
}
node.on('input', (msg) => {
node.status({fill:"blue",shape:"dot",text:"requesting"});
const query = mustache.render(node.query, msg);
pool.query(query, (error, results) => {
if (error) {
node.error(error);
node.status({fill: "red", shape: "ring", text: error.message});
pool.close();
return;
}
msg.payload = results;
node.send(msg);
node.status({fill:'blue',shape:'dot',text:'finish'});
})
});
});
});
}
RED.nodes.registerType("ODBC", odbc);
}