Giter Club home page Giter Club logo

node-red-contrib-postgres-multi's Introduction

node-red-contrib-postgres-multi

A Node-RED node to query PostgreSQL, with multiple query support.

Based on node-red-contrib-postgres by Kris Daniels.

Compatibility

This module is designed assuming you will only use this one or Kris' original version in a project, but not both at the same time. You can replace one with the other in your project and your flows will remain connected.

  • The configuration code is identical, so your database connection configuration should need no changes.
  • The output format is identical (assuming equivalent queries)
  • The input format is significantly different, and will require updates on any block generating queries to be passed to Postgres. (The queries shouldn't change, just the encapsulating data structure.)

Requirements

This module uses JavaScript features only found in Node versions 8+.

Install

Run the following command in the root directory of your Node-RED install

npm install node-red-contrib-postgres-multi

Usage

Assemble your queries as an array of objects on msg.payload:

msg.payload = [
    {
        query: 'begin',
    },
    {
        query: 'delete from mytable',
    },
    {
        query: 'insert into mytable (id, message) values (1, $hello), (2, $world)',
        params: {
            hello: 'Hi there',
            world: 'O\'Rorke',
        },
    },
    {
        query: 'commit',
    },
];

As you can see, this structure allows you to create your own transaction boundaries.

If you want the output of one or more queries, check the 'Receive output' box in the postgres node and include an output: true member in the query object(s) you expect results from. The results are then set on the msg.payload property of the outbound message.

msg.payload = [
    {
        query: 'select message from mytable where id=$1',
        params: [1],
        output: true,
    },
    {
        query: 'select message from mytable where id=$1',
        params: [2],
        output: true,
    },
];

# output:

[
    ['Hi there'],
    ['O\'Rorke']
]

Example DB

begin;
create table mytable
(
    id integer not null,
    message character varying(20)
);
create unique index on mytable (id);
commit;

Example node-red flow

Import the flow below in an empty sheet in nodered

[{"id":"460f2b8f.a23dbc","type":"tab","label":"Postgres example flows","disabled":false,"info":"# Postgres example flows\n\nThese flows demonstrate the use of the\n`node-red-contrib-postgres-multi` node.\n\n## Setup\n\nFor the flows in this tab,\nyou'll need a PostgreSQL table like so:\n\n    begin;\n    create table mytable\n    (\n        id integer not null,\n        message character varying(20)\n    );\n    create unique index on mytable (id);\n    commit;\n\nThen you'll need to configure the postgres\nblocks to have access to this database and table.\n"},{"id":"5498d534.e9cc54","type":"comment","z":"460f2b8f.a23dbc","name":"Reset","info":"This flow clears any contents from\nthe `mytable` table and inserts a single\nrecord:\n\n    id   message\n    1    'hello world'\n","x":90,"y":40,"wires":[]},{"id":"2ef9ce55.1ee282","type":"inject","z":"460f2b8f.a23dbc","name":"click me","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120,"y":100,"wires":[["98595979.a97fe"]]},{"id":"98595979.a97fe","type":"function","z":"460f2b8f.a23dbc","name":"prepare","func":"\nmsg.payload = [\n    {\n        query: 'begin',\n    },\n    {\n        query: 'delete from mytable',\n    },\n    {\n        query: 'insert into mytable (id, message) values ($1, $2)',\n        params: [1, 'hello world'],\n    },\n    {\n        query: 'select * from mytable',\n        output: true,\n    },\n    {\n        query: 'commit',\n    },\n];\n\nreturn msg;","outputs":1,"noerr":0,"x":300,"y":100,"wires":[["30d1db79.179dd4"]]},{"id":"30d1db79.179dd4","type":"postgres","z":"460f2b8f.a23dbc","postgresdb":"5932c310.1c96d4","name":"","output":true,"outputs":1,"x":480,"y":100,"wires":[["87793132.0d9d88"]]},{"id":"87793132.0d9d88","type":"debug","z":"460f2b8f.a23dbc","name":"","active":true,"console":"false","complete":"false","x":679.5,"y":100,"wires":[]},{"id":"27e47fba.f5896","type":"comment","z":"460f2b8f.a23dbc","name":"Demo","info":"This flow demonstrates:\n\n* transactions and auto-commit queries\n* positional arguments (i.e. $argName)\n* query output from multiple queries\n","x":86.5,"y":182,"wires":[]},{"id":"2de05c89.ff8454","type":"inject","z":"460f2b8f.a23dbc","name":"click me","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120,"y":240,"wires":[["b5127483.4ae8d8"]]},{"id":"b5127483.4ae8d8","type":"function","z":"460f2b8f.a23dbc","name":"prepare","func":"\nmsg.payload = [\n    {\n        query: 'begin',\n    },\n    {\n        query: 'delete from mytable',\n    },\n    {\n        query:\n            'insert into mytable (id, message) ' +\n            'values ($foo, $bar), ($baz, $boop)',\n        params: {\n            foo: 10,\n            bar: 'hello',\n            baz: 20,\n            boop: 'world',\n        },\n    },\n    {\n        query: 'commit',\n    },\n    {\n        query: 'select * from mytable',\n        output: true,\n    },\n    {\n        query: 'insert into mytable (id, message) values (30, \\'xtra\\')',\n    },\n    {\n        query: 'select message from mytable order by id',\n        output: true,\n    },\n];\n\nreturn msg;","outputs":1,"noerr":0,"x":300,"y":240,"wires":[["6308b299.b5a87c"]]},{"id":"6308b299.b5a87c","type":"postgres","z":"460f2b8f.a23dbc","postgresdb":"5932c310.1c96d4","name":"","output":true,"outputs":1,"x":480,"y":240,"wires":[["68797b5f.c0a3ec"]]},{"id":"68797b5f.c0a3ec","type":"debug","z":"460f2b8f.a23dbc","name":"","active":true,"console":"false","complete":"false","x":679.5,"y":240,"wires":[]},{"id":"35f6d8a5.a36018","type":"comment","z":"460f2b8f.a23dbc","name":"Rollback","info":"If you start a transaction and don't commit it,\nyour changes will not be saved.","x":100,"y":320,"wires":[]},{"id":"ba356a1b.b7c2e8","type":"inject","z":"460f2b8f.a23dbc","name":"click me","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120,"y":380,"wires":[["73628965.7539d8"]]},{"id":"73628965.7539d8","type":"function","z":"460f2b8f.a23dbc","name":"prepare","func":"\nmsg.payload = [\n    {\n        query: 'begin',\n    },\n    {\n        query: 'delete from mytable',\n    },\n    {\n        query:\n            'insert into mytable (id, message) ' +\n            'values ($foo, $bar), ($baz, $boop), ($bing, $bang)',\n        params: {\n            foo: 10,\n            bar: 'one does not simply',\n            baz: 20,\n            boop: 'begin',\n            bing: 30,\n            bang: 'but not commit',\n        },\n    },\n    {\n        query: 'select message from mytable order by id',\n        output: true,\n    },\n];\n\nreturn msg;","outputs":1,"noerr":0,"x":300,"y":380,"wires":[["f7c2f77b.85d6b8"]]},{"id":"f7c2f77b.85d6b8","type":"postgres","z":"460f2b8f.a23dbc","postgresdb":"5932c310.1c96d4","name":"","output":true,"outputs":1,"x":480,"y":380,"wires":[["77248a23.6a48b4","b5a1d7d5.4301a8"]]},{"id":"77248a23.6a48b4","type":"debug","z":"460f2b8f.a23dbc","name":"","active":true,"console":"false","complete":"false","x":679.5,"y":380,"wires":[]},{"id":"b5a1d7d5.4301a8","type":"function","z":"460f2b8f.a23dbc","name":"check","func":"\nmsg.payload = [\n    {\n        query: 'select message from mytable order by id',\n        output: true,\n    },\n];\n\nreturn msg;","outputs":1,"noerr":0,"x":290,"y":460,"wires":[["d8d8e5bc.f76488"]]},{"id":"d8d8e5bc.f76488","type":"postgres","z":"460f2b8f.a23dbc","postgresdb":"5932c310.1c96d4","name":"","output":true,"outputs":1,"x":480,"y":460,"wires":[["b0f96526.7a4648"]]},{"id":"b0f96526.7a4648","type":"debug","z":"460f2b8f.a23dbc","name":"","active":true,"console":"false","complete":"false","x":679.5,"y":460,"wires":[]},{"id":"5932c310.1c96d4","type":"postgresdb","z":"","hostname":"localhost","port":"5432","db":"foo","ssl":false}]

node-red-contrib-postgres-multi's People

Contributors

brucefletcher avatar chainhead avatar krisdaniels avatar mrlight avatar sn0wcat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-red-contrib-postgres-multi's Issues

Wiring output of an INSERT operation.

It seems that, after an INSERT, there is no message on the output node. Therefore, as per Node-RED design, the flow stops. Whereas, I want to do few more operations after the INSERT is successful. How can we trigger next node after an INSERT?

[DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated

Hi,

thanks for your great node! I just tried it and at first I sent only one query in an object, instead of an array of objects. I fixed that immediately, but in the log file I found this scary deprecation warning:

Mär 12 21:39:39 rpi Node-RED[21990]: 12 Mar 21:39:39 - [error] [postgres:26057d45.591592] Error: msg.payload must be an array of queries
...
Mär 12 21:39:39 rpi Node-RED[21990]: (node:21990) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I don't know what "Unhandled promise rejections" are, but I guess it wouldn't be nice if Node-RED would be killed because of a simple user error.

Stopped working with Node-RED 1.3.5

Hi,

I upgraded to Node-RED 1.3.5 a few weeks ago, and node-red-contrib-postgres-multi doesn't work since then. It gives no error messages, it just doesn't write into the database.

not working?

I tried a thousand things to see the data of a postgres db on remote server, but I don't even see any error messages (connected? non connected? reading the table? etc).
I use the example flows but nothing works!

Unable to catch sql errors

As I wrote on the title: there's no way to catch on the flow errors coming from database (so for example error code / description) to provide (for example) personalized messages or similar.

You can't have both node-red-contrib-postgres and node-red-contrib-postgres-multi at the same time

It seems that you can't have both node-red-contrib-postgres and node-red-contrib-postgres-multi at the same time, because, according to NR logs:

[warn] [node-red-contrib-postgres-multi/PostgreSql] 'postgres' already registered by module node-red-contrib-postgres

And I can confirm, that in the nodes palette there's only one node - in my case ode-red-contrib-postgres, as it was installed first.

NodeRED 0.18.4

Data type numeric is converted to string

Hi,

I work for a long time without any problems with this node but now I face the problem, that the Postgres data type numeric is not recognized, i.e. converted to string. Is there any solution for this?

Connection terminated unexpectedly.

I am getting this error unpredictably in pm2 logs for Node-RED.

26 Jun 09:22:37 - [error] [postgres:DBQuery] Error: Connection terminated unexpectedly

I did some searches and found this:

What's happening now is one (or many) client(s) in your pool sits idle and connected to the backend database. A network partition happens or database failure, fail-over, forced disconnection from the backend, etc and the idle client(s) all notice and emit an error event. The pool listens for errors on idle clients. When an idle client has an error the pool destroys the client and removes it from the pool so the next time you request a client from the pool it will automatically create and connect a new client.

Should a pool.on('error', (err) { ... } be inserted after this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.