From fe109f15d84f3627b3c9867afdedc6de9351941a Mon Sep 17 00:00:00 2001 From: Kris Daniels Date: Fri, 20 Dec 2013 10:00:35 +0100 Subject: [PATCH] added postgres node --- storage/postgres/110-postgresout.html | 148 ++++++++++++++++++++++++++ storage/postgres/110-postgresout.js | 83 +++++++++++++++ storage/postgres/icons/postgres.png | Bin 0 -> 625 bytes 3 files changed, 231 insertions(+) create mode 100644 storage/postgres/110-postgresout.html create mode 100644 storage/postgres/110-postgresout.js create mode 100644 storage/postgres/icons/postgres.png diff --git a/storage/postgres/110-postgresout.html b/storage/postgres/110-postgresout.html new file mode 100644 index 00000000..ab3291cc --- /dev/null +++ b/storage/postgres/110-postgresout.html @@ -0,0 +1,148 @@ + + + + + + + + + + + diff --git a/storage/postgres/110-postgresout.js b/storage/postgres/110-postgresout.js new file mode 100644 index 00000000..bf882b6a --- /dev/null +++ b/storage/postgres/110-postgresout.js @@ -0,0 +1,83 @@ +/** + * Copyright 2013 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +// If you use this as a template, replace IBM Corp. with your own name. + +// Sample Node-RED node file + +// Require main module +var RED = require(process.env.NODE_RED_HOME+"/red/red"); +var pg=require('pg'); +var named=require('node-postgres-named'); + +function PostgresDatabaseNode(n) { + RED.nodes.createNode(this,n); + this.hostname = n.hostname; + this.port = n.port; + this.db = n.db; + this.username = n.username; + this.password = n.password; +} + +RED.nodes.registerType("postgresdb",PostgresDatabaseNode); + +function PostgresNode(n) { + RED.nodes.createNode(this,n); + + this.topic = n.topic; + this.postgresdb = n.postgresdb; + this.postgresConfig = RED.nodes.getNode(this.postgresdb); + this.sqlquery = n.sqlquery; + this.outputs = n.outputs; + + var node = this; + if(this.postgresConfig) + { + + var conString = 'postgres://'+this.postgresConfig.username +':' + this.postgresConfig.password + '@' + this.postgresConfig.hostname + ':' + this.postgresConfig.port + '/' + this.postgresConfig.db; + node.clientdb = new pg.Client(conString); + named.patch(node.clientdb); + + node.clientdb.connect(function(err){ + if(err) { node.error(err); } + else { + node.on('input', + function(msg){ + node.clientdb.query(node.sqlquery, + msg.payload, + function (err, results) { + if(err) { node.error(err); } + else { + if(node.outputs>0) + { + msg.payload = results.rows; + node.send(msg); + } + } + }); + }); + } + }); + } else { + this.error("missing postgres configuration"); + } + + this.on("close", function() { + if(node.clientdb) node.clientdb.end(); + }); +} + +RED.nodes.registerType("postgres",PostgresNode); diff --git a/storage/postgres/icons/postgres.png b/storage/postgres/icons/postgres.png new file mode 100644 index 0000000000000000000000000000000000000000..e46c31696bc4e2240ffe3450323e74c6ae0bbe33 GIT binary patch literal 625 zcmV-%0*?KOP)HWn7*8>A2= zPoR*(#?ID4Ng-Z2`D|v#*^}9G_Q^$X!NAO#%Ubi#|Ib1tYvF+7Tk2bNTU{%hi%RaO z+0u;pmE@e8iN6ygs6-Pu4SWN3*>3`iz|YLz01Lo(U>VqnYtwxb;8(jiL}viLR)SqD z1S78h0gQn!z`a63fA^_p)z^KPtEWoPRNA)M6#TZ38nckHwYMK?4KOY25eQ8(I4au( z9s{?_>Cc9%ou8YeFVsS8^B56MPbce|0o0Y21)zP49ZV{twXZ5lU%F6 zJHXY#;uYZ09EXQYVxc4p3Xj~jvHH0{@5uleslHJ!m#W36qDQlmG~}C8-7M_g)8Q0& z3as_KWBZ|>2c8dv_gehW?Sr&0d(}_s{jC1=-ELH=nfn4h$;NjxSWG^E8l+1-g}qaN z`?N%Xbv4~C#$ue>0`QxNagEwd@_w%dR+aT6nOr8t8`f+T2{!M~)G3zp*z~Z)vf&@w ze-m)ypaJJ4O^qDmNd0h}kP55