This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
forked from HySoaKa/node-red-contrib-postgrestor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
postgrestor.js
116 lines (110 loc) · 3.06 KB
/
postgrestor.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
module.exports = function(RED) {
'use strict';
const mustache = require('mustache');
const Pool = require('pg').Pool;
function getField(node, kind, value) {
switch (kind) {
case 'flow': {
return node.context().flow.get(value);
}
case 'global': {
return node.context().global.get(value);
}
case 'num': {
return parseInt(value);
}
case 'bool': {
return JSON.parse(value);
}
default: {
return value;
}
}
}
function PostgresDBNode(n) {
const node = this;
RED.nodes.createNode(node, n);
node.name = n.name;
node.host = n.host;
node.hostFieldType = n.hostFieldType;
node.port = n.port;
node.portFieldType = n.portFieldType;
node.database = n.database;
node.databaseFieldType = n.databaseFieldType;
node.ssl = n.ssl;
node.sslFieldType = n.sslFieldType;
node.max = n.max;
node.maxFieldType = n.maxFieldType;
node.min = n.min;
node.minFieldType = n.minFieldType;
node.idle = n.idle;
node.idleFieldType = n.idleFieldType;
node.user = n.user;
node.userFieldType = n.userFieldType;
node.password = n.password;
node.passwordFieldType = n.passwordFieldType;
}
RED.nodes.registerType('postgresDB', PostgresDBNode);
let myPool = false;
function PostgrestorNode(config) {
const node = this;
RED.nodes.createNode(node, config);
node.topic = config.topic;
node.config = RED.nodes.getNode(config.postgresDB);
node.on('input', (msg) => {
const query = mustache.render(config.query, { msg });
const {
user,
password,
host,
port,
database,
ssl,
max,
min,
iddle,
userFieldType,
passwordFieldType,
hostFieldType,
portFieldType,
databaseFieldType,
sslFieldType,
maxFieldType,
minFieldType,
idleFieldType
} = node.config;
myPool =
myPool ||
new Pool({
user: getField(node, userFieldType, user),
password: getField(node, passwordFieldType, password),
host: getField(node, hostFieldType, host),
port: getField(node, portFieldType, port),
database: getField(node, databaseFieldType, database),
ssl: getField(node, sslFieldType, ssl),
max: getField(node, maxFieldType, max),
min: getField(node, minFieldType, min),
idleTimeoutMillis: getField(node, idleFieldType, iddle)
});
const asyncQuery = async () => {
let client = false;
try {
client = await myPool.connect();
msg.payload = await client.query(query);
} catch (err) {
const error = err.toString();
node.error(error);
msg.payload = error;
} finally {
if (client) {
client.release();
}
node.send(msg);
}
};
asyncQuery();
});
node.on('close', () => node.status({}));
}
RED.nodes.registerType('postgrestor', PostgrestorNode);
};