Skip to content

Commit 76df2fb

Browse files
zinenAlkarex
andauthored
Adopt node-red 1.0=< schema (#15)
* Dont send anything to console that can be catched * Adopt node-red 1.0=< schema * Status not used anywhere * Rearrange tickUpstreamNode back as before * Replaced node.send with send callback * duplicate variable renamed * node-red done callback now called on split query * Minor formatting Co-authored-by: Alexandre Alapetite <[email protected]>
1 parent c0c5f63 commit 76df2fb

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

postgresql.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ module.exports = function (RED) {
112112
let cursor;
113113
let getNextRows;
114114

115-
node.on('input', async (msg) => {
115+
node.on('input', async (msg, send, done) => {
116+
// 'send' and 'done' require Node-RED 1.0+
117+
send = send || function () { node.send.apply(node, arguments); };
118+
116119
if (tickUpstreamId === undefined) {
117120
tickUpstreamId = findInputNodeId(node, (n) => RED.nodes.getNode(n.id).tickConsumer);
118121
tickUpstreamNode = tickUpstreamId ? RED.nodes.getNode(tickUpstreamId) : null;
@@ -146,17 +149,23 @@ module.exports = function (RED) {
146149
};
147150

148151
const handleError = (err) => {
149-
console.error(err);
150152
const error = (err ? err.toString() : 'Unknown error!') + ' ' + query;
151-
node.error(error);
152153
handleDone();
153154
msg.payload = error;
154155
msg.parts = {
155156
id: partsId,
156157
abort: true,
157158
};
158159
downstreamReady = false;
159-
node.send(msg);
160+
if (err) {
161+
if (done) {
162+
// Node-RED 1.0+
163+
done(err);
164+
} else {
165+
// Node-RED 0.x
166+
node.error(err, msg);
167+
}
168+
}
160169
};
161170

162171
handleDone();
@@ -180,8 +189,8 @@ module.exports = function (RED) {
180189
if (err) {
181190
handleError(err);
182191
} else {
183-
const done = rows.length < node.rowsPerMsg;
184-
if (done) {
192+
const complete = rows.length < node.rowsPerMsg;
193+
if (complete) {
185194
handleDone();
186195
}
187196
const msg2 = Object.assign({}, msg, {
@@ -199,17 +208,20 @@ module.exports = function (RED) {
199208
if (msg.parts) {
200209
msg2.parts.parts = msg.parts;
201210
}
202-
if (done) {
211+
if (complete) {
203212
msg2.parts.count = partsIndex + 1;
204213
msg2.complete = true;
205214
}
206215
partsIndex++;
207216
downstreamReady = false;
208-
node.send(msg2);
209-
if (done) {
217+
send(msg2);
218+
if (complete) {
210219
if (tickUpstreamNode) {
211220
tickUpstreamNode.receive({ tick: true });
212221
}
222+
if (done) {
223+
done();
224+
}
213225
} else {
214226
getNextRows();
215227
}
@@ -247,10 +259,13 @@ module.exports = function (RED) {
247259

248260
handleDone();
249261
downstreamReady = false;
250-
node.send(msg);
262+
send(msg);
251263
if (tickUpstreamNode) {
252264
tickUpstreamNode.receive({ tick: true });
253265
}
266+
if (done) {
267+
done();
268+
}
254269
} catch (ex) {
255270
handleError(ex);
256271
}
@@ -263,8 +278,6 @@ module.exports = function (RED) {
263278
}
264279
}
265280
});
266-
267-
node.on('close', () => node.status({}));
268281
}
269282

270283
RED.nodes.registerType('postgresql', PostgreSQLNode);

0 commit comments

Comments
 (0)