Skip to content

Commit e5af41b

Browse files
committed
Merge pull request #17 from krisdaniels/master
Added Postgres Node
2 parents 90eef93 + e72ced8 commit e5af41b

File tree

3 files changed

+269
-0
lines changed

3 files changed

+269
-0
lines changed

storage/postgres/110-postgres.html

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<!--
2+
Copyright 2013 Kris Daniels.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
<script type="text/x-red" data-template-name="postgresdb">
18+
<div class="form-row">
19+
<label for="node-config-input-hostname"><i class="icon-bookmark"></i> Host</label>
20+
<input class="input-append-left" type="text" id="node-config-input-hostname" placeholder="localhost" style="width: 40%;" >
21+
<label for="node-config-input-port" style="margin-left: 10px; width: 35px; "> Port</label>
22+
<input type="text" id="node-config-input-port" placeholder="5432" style="width:45px">
23+
</div>
24+
<div class="form-row">
25+
<label for="node-config-input-db"><i class="icon-briefcase"></i> Database</label>
26+
<input type="text" id="node-config-input-db" placeholder="test">
27+
</div>
28+
<div class="form-row">
29+
<label for="node-config-input-name"><i class="icon-user"></i> Username</label>
30+
<input type="text" id="node-config-input-user" placeholder="postgres">
31+
<label for="node-config-input-password"><i class="icon-lock"></i> Password</label>
32+
<input type="password" id="node-config-input-password" placeholder="postgres">
33+
</div>
34+
</script>
35+
36+
<script type="text/javascript">
37+
RED.nodes.registerType('postgresdb',{
38+
category: 'config',
39+
color:"rgb(218, 196, 180)",
40+
defaults: {
41+
hostname: { value:"localhost",required:true},
42+
port: { value: 5432,required:true},
43+
db: { value:"postgres",required:true}
44+
},
45+
label: function() {
46+
return this.name||this.hostname+":"+this.port+"/"+this.db;
47+
},
48+
oneditprepare: function() {
49+
50+
$.getJSON('postgresdb/'+this.id,function(data) {
51+
if (data.user) {
52+
$('#node-config-input-user').val(data.user);
53+
}
54+
if (data.hasPassword) {
55+
$('#node-config-input-password').val('__PWRD__');
56+
} else {
57+
$('#node-config-input-password').val('');
58+
}
59+
});
60+
},
61+
oneditsave: function() {
62+
63+
var newUser = $('#node-config-input-user').val();
64+
var newPass = $('#node-config-input-password').val();
65+
var credentials = {};
66+
credentials.user = newUser;
67+
if (newPass != '__PWRD__') {
68+
credentials.password = newPass;
69+
}
70+
$.ajax({
71+
url: 'postgresdb/'+this.id,
72+
type: 'POST',
73+
data: credentials,
74+
success:function(result){}
75+
});
76+
},
77+
ondelete: function() {
78+
$.ajax({
79+
url: 'postgresdb/'+this.id,
80+
type: 'DELETE',
81+
success: function(result) {}
82+
});
83+
}
84+
});
85+
</script>
86+
87+
<script type="text/x-red" data-template-name="postgres">
88+
<div class="form-row">
89+
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
90+
<input type="text" id="node-input-name" placeholder="Name">
91+
</div>
92+
<div class="form-row">
93+
<label for="node-input-postgresdb"><i class="icon-tag"></i> Server</label>
94+
<input type="text" id="node-input-postgresdb">
95+
</div>
96+
<div class="form-row">
97+
<label>&nbsp;</label>
98+
<input type="checkbox" id="node-input-output" placeholder="once" style="display: inline-block; width: auto; vertical-align: top;">
99+
<label for="node-input-output" style="width: 70%;">Receive query output ?</label>
100+
</div>
101+
</script>
102+
103+
<script type="text/x-red" data-help-name="postgres">
104+
<p>A PostgreSql I/O node. </p>
105+
<p>Executes the query specified in msg.payload with optional query parameters in msg.queryParameters</p>
106+
<p>The queryParameters in the query must be specified as $propertyname</p>
107+
<p>See the node-postgres-named package for more info</p>
108+
<p>When receiving data from the query, the msg.payload on the output will be a json array of the returned records</p>
109+
110+
</script>
111+
112+
<script type="text/javascript">
113+
RED.nodes.registerType('postgres',{
114+
category: 'storage-output',
115+
color:"rgb(148, 226, 252)",
116+
defaults: {
117+
postgresdb: { type:"postgresdb",required:true},
118+
name: {value:""},
119+
output: {value:false},
120+
outputs: {value:0}
121+
},
122+
inputs: 1,
123+
outputs: 0,
124+
icon: "postgres.png",
125+
align: "right",
126+
label: function() {
127+
return this.name||(this.sqlquery?this.sqlquery:"postgres");
128+
},
129+
labelStyle: function() {
130+
return this.name?"node_label_italic":"";
131+
},
132+
oneditprepare: function() {
133+
134+
$( "#node-input-output" ).prop( "checked", this.output );
135+
$("#node-input-name").focus();
136+
137+
},
138+
oneditsave: function() {
139+
140+
var hasOutput = $( "#node-input-output" ).prop( "checked" );
141+
this.outputs = hasOutput ? 1:0;
142+
143+
}
144+
});
145+
146+
</script>

storage/postgres/110-postgres.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* Copyright 2013 Kris Daniels.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
var RED = require(process.env.NODE_RED_HOME+"/red/red");
18+
var pg=require('pg');
19+
var named=require('node-postgres-named');
20+
var querystring = require('querystring');
21+
22+
RED.app.get('/postgresdb/:id',function(req,res) {
23+
var credentials = RED.nodes.getCredentials(req.params.id);
24+
if (credentials) {
25+
res.send(JSON.stringify({user:credentials.user,hasPassword:(credentials.password&&credentials.password!="")}));
26+
} else {
27+
res.send(JSON.stringify({}));
28+
}
29+
});
30+
31+
RED.app.delete('/postgresdb/:id',function(req,res) {
32+
RED.nodes.deleteCredentials(req.params.id);
33+
res.send(200);
34+
});
35+
36+
RED.app.post('/postgresdb/:id',function(req,res) {
37+
var body = "";
38+
req.on('data', function(chunk) {
39+
body+=chunk;
40+
});
41+
req.on('end', function(){
42+
var newCreds = querystring.parse(body);
43+
var credentials = RED.nodes.getCredentials(req.params.id)||{};
44+
if (newCreds.user == null || newCreds.user == "") {
45+
delete credentials.user;
46+
} else {
47+
credentials.user = newCreds.user;
48+
}
49+
if (newCreds.password == "") {
50+
delete credentials.password;
51+
} else {
52+
credentials.password = newCreds.password||credentials.password;
53+
}
54+
RED.nodes.addCredentials(req.params.id,credentials);
55+
res.send(200);
56+
});
57+
});
58+
59+
60+
function PostgresDatabaseNode(n) {
61+
RED.nodes.createNode(this,n);
62+
this.hostname = n.hostname;
63+
this.port = n.port;
64+
this.db = n.db;
65+
66+
var credentials = RED.nodes.getCredentials(n.id);
67+
if (credentials) {
68+
this.user = credentials.user;
69+
this.password = credentials.password;
70+
}
71+
}
72+
73+
RED.nodes.registerType("postgresdb",PostgresDatabaseNode);
74+
75+
function PostgresNode(n) {
76+
RED.nodes.createNode(this,n);
77+
78+
this.topic = n.topic;
79+
this.postgresdb = n.postgresdb;
80+
this.postgresConfig = RED.nodes.getNode(this.postgresdb);
81+
this.sqlquery = n.sqlquery;
82+
this.output = n.output;
83+
84+
var node = this;
85+
86+
if(this.postgresConfig)
87+
{
88+
89+
var conString = 'postgres://'+this.postgresConfig.user +':' + this.postgresConfig.password + '@' + this.postgresConfig.hostname + ':' + this.postgresConfig.port + '/' + this.postgresConfig.db;
90+
node.clientdb = new pg.Client(conString);
91+
named.patch(node.clientdb);
92+
93+
node.clientdb.connect(function(err){
94+
if(err) { node.error(err); }
95+
else {
96+
node.on('input',
97+
function(msg){
98+
if(!msg.queryParameters) msg.queryParameters={};
99+
node.clientdb.query(msg.payload,
100+
msg.queryParameters,
101+
function (err, results) {
102+
if(err) { node.error(err); }
103+
else {
104+
if(node.output)
105+
{
106+
msg.payload = results.rows;
107+
node.send(msg);
108+
}
109+
}
110+
});
111+
});
112+
}
113+
});
114+
} else {
115+
this.error("missing postgres configuration");
116+
}
117+
118+
this.on("close", function() {
119+
if(node.clientdb) node.clientdb.end();
120+
});
121+
}
122+
123+
RED.nodes.registerType("postgres",PostgresNode);

storage/postgres/icons/postgres.png

625 Bytes
Loading

0 commit comments

Comments
 (0)