-
Notifications
You must be signed in to change notification settings - Fork 11
/
node-red-contrib-alafile.js
executable file
·126 lines (121 loc) · 3.21 KB
/
node-red-contrib-alafile.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
/**
Double backslash for Windows paths
@param path {string} String with path
@return {string} Modified path
@example
\Program Files\Mysoft => \\Program Files\\Mysoft
*/
function doubleb(path) {
return path.replace(/\\/g, '\\\\');
}
module.exports = function(RED) {
var alasql = require('alasql');
function AlaFileNodeOut(config) {
RED.nodes.createNode(this, config);
var node = this;
node.filename = config.filename;
node.format = config.format;
node.columns = config.columns;
node.headers = config.headers;
node.on('input', (msg) => {
var filename = node.filename || msg.filename || '';
if (filename !== '') {
node.status({fill: 'green', shape: 'dot', text: ' ' + filename + '.' + this.format});
}
if (filename === '') {
node.warn(RED._('file.errors.nofilename'));
node.status({
fill: 'red',
shape: 'ring',
text: 'No filename in node config or msg.filename!',
});
} else if (msg.hasOwnProperty('payload') && typeof msg.payload !== 'undefined') {
var sql =
'SELECT ' +
this.columns +
' INTO ' +
this.format +
'("' +
doubleb(filename) +
'",{headers:' +
this.headers +
'}) FROM ?';
var bind = Array.isArray(msg.payload) ? [msg.payload] : [[msg.payload]];
alasql
.promise(sql, bind)
.then((res) => {
msg.payload = res;
node.send(msg);
})
.catch((err) => {
node.error(err, msg);
});
} else {
node.status({
fill: 'red',
shape: 'ring',
text: 'No msg.payload defined with data to output!',
});
}
});
this.on('close', () => {
node.status({});
});
}
RED.nodes.registerType('alafile out', AlaFileNodeOut);
function AlaFileNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.filename = config.filename;
node.format = config.format;
node.columns = config.columns;
node.headers = config.headers;
node.on('input', (msg) => {
var filename = node.filename || msg.filename || '';
if (filename !== '') {
node.status({fill: 'green', shape: 'dot', text: ' ' + filename + '.' + this.format});
}
if (filename === '') {
node.warn(RED._('file.errors.nofilename'));
node.status({
fill: 'red',
shape: 'ring',
text: 'No filename in node config or msg.filename!',
});
} else if (msg.hasOwnProperty('payload') && typeof msg.payload !== 'undefined') {
var sql =
'SELECT ' +
this.columns +
' FROM ' +
this.format +
'("' +
doubleb(filename) +
'.' +
this.format +
'",{headers:' +
this.headers +
'})';
var bind = Array.isArray(msg.payload) ? [msg.payload] : [[msg.payload]];
if (!require('fs').existsSync(doubleb(filename) + '.' + this.format)) {
var notExist = ' ' + doubleb(filename) + '.' + this.format + ' does not exist!';
node.warn(RED._(notExist));
node.status({fill: 'red', shape: 'ring', text: notExist});
} else {
alasql
.promise(sql, bind)
.then((res) => {
msg.payload = res;
node.send(msg);
})
.catch((err) => {
node.error(err, msg);
});
}
}
});
this.on('close', () => {
node.status({});
});
}
RED.nodes.registerType('alafile in', AlaFileNode);
};