Skip to content

Commit b0b2efd

Browse files
committed
Uniformisation of content in msg.payload
Provide `msg.pgsql` for details. And more cleaning and documentation.
1 parent 4b165df commit b0b2efd

File tree

8 files changed

+130
-109
lines changed

8 files changed

+130
-109
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"rules": {
77
"comma-dangle": ["warn", "always-multiline"],
8-
"indent": ["error", "tab"],
8+
"indent": ["error", "tab", { "SwitchCase": 1 }],
99
"max-len": ["warn", 120],
1010
"new-cap": "warn",
1111
"object-curly-spacing": ["warn", "always"],

README.md

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ It supports *splitting* the resultset and *backpressure* (flow control), to allo
66

77
It supports *parameterized queries*.
88

9-
`msg.payload` will contain the result object of the query: (see also the documentation of the [underlying node-postgres library](https://node-postgres.com/api/result))
9+
## Outputs
1010

11-
* `command`: The SQL command that was executed (e.g. `SELECT`, `UPDATE`, etc.)
12-
* `rowCount`: The number of rows affected by the SQL statement
13-
* `oid`: The [oid](https://www.postgresql.org/docs/current/datatype-oid.html) returned
14-
* `rows`: An array of rows
11+
The response (rows) is provided in `msg.payload` as an array.
1512

16-
There is a template engine allowing parameterized queries:
13+
An exception is if the *Split results* option is enabled and the *Number of rows per message* set to **1**, then `msg.payload` is not an array but the single-row response.
14+
15+
Additional information is provided as `msg.pgsql.rowCount` and `msg.pgsql.command`. See the [underlying documentation](https://node-postgres.com/api/result) for details.
16+
17+
In the case of multiple queries, then `msg.pgsql` is an array.
18+
19+
## Inputs
20+
21+
### SQL query template
22+
23+
This node uses the [Mustache template system](https://github.com/janl/mustache.js) to generate queries based on the message:
1724

1825
```sql
1926
-- INTEGER id column
@@ -23,7 +30,7 @@ SELECT * FROM table WHERE id = {{{ msg.id }}};
2330
SELECT * FROM table WHERE id = '{{{ msg.id }}}';
2431
```
2532

26-
## Parameterized query
33+
### Parameterized query
2734

2835
Parameters for parameterized queries can be passed as a parameter array `params` of the `msg` object:
2936

@@ -34,7 +41,7 @@ msg.params = [ msg.id ];
3441

3542
```sql
3643
-- In this node, use a parameterized query
37-
SELECT * FROM table where name = $1;
44+
SELECT * FROM table WHERE id = $1;
3845
```
3946

4047
## Installation
@@ -44,11 +51,11 @@ SELECT * FROM table where name = $1;
4451
You can install [**node-red-contrib-postgresql**](https://flows.nodered.org/node/node-red-contrib-postgresql) directly using the editor:
4552
Select *Manage Palette* from the menu (top right), and then select the *Install* tab in the palette.
4653

47-
### Installing npm packaged nodes
54+
### Using npm
4855

49-
You can also install the [npm-packaged node](https://www.npmjs.com/package/node-red-contrib-postgresql):
56+
You can alternatively install the [npm-packaged node](https://www.npmjs.com/package/node-red-contrib-postgresql):
5057

51-
* Locally within your user data directory (by default, ```$HOME/.node-red```):
58+
* Locally within your user data directory (by default, `$HOME/.node-red`):
5259

5360
```sh
5461
cd $HOME/.node-red
@@ -74,19 +81,20 @@ So when the *Split results* option is enabled, this node will only output one me
7481
To make this behaviour potentially automatic (avoiding manual wires), this node declares its ability by exposing a truthy `node.tickConsumer` for downstream nodes to detect this feature, and a truthy `node.tickProvider` for upstream nodes.
7582
Likewise, this node detects upstream nodes using the same back-pressure convention, and automatically sends ticks.
7683

77-
## Sequences
84+
## Sequences for split results
7885

79-
When the *Split results* option is enabled, the messages contain some information following the conventions for [*messages sequences*](https://nodered.org/docs/user-guide/messages#message-sequences).
86+
When the *Split results* option is enabled (streaming), the messages contain some information following the conventions for [*messages sequences*](https://nodered.org/docs/user-guide/messages#message-sequences).
8087

8188
```js
8289
{
83-
payload: '...',
84-
parts: {
85-
id: 0.1234, // sequence ID from upstream or randomly generated (changes for every sequence)
86-
index: 5, // incremented for each message of the same sequence
87-
count: 6, // total number of message; only available in the last message of a sequence
88-
},
89-
complete: true, // True only for the last message of a sequence
90+
payload: '...',
91+
parts: {
92+
id: 0.1234, // sequence ID, randomly generated (changes for every sequence)
93+
index: 5, // incremented for each message of the same sequence
94+
count: 6, // total number of message; only available in the last message of a sequence
95+
parts: {}, // optional upstream parts information
96+
},
97+
complete: true, // True only for the last message of a sequence
9098
}
9199
```
92100

locales/en-US/postgresql.html

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
<script type="text/x-red" data-help-name="postgresql">
22
<p><a href="https://github.com/alexandrainst/node-red-contrib-postgresql">node-red-contrib-postgresql</a> is a Node-RED node to query a <a href="https://www.postgresql.org/">PostgreSQL</a> 🐘 database.</p>
3-
<p>
4-
<code>msg.payload</code> will contain the result object of the query with following properties:
5-
(see also the documentation of the <a href="https://node-postgres.com/api/result">underlying node-postgres library</a>.
6-
</p>
7-
<ul>
8-
<li><dfn>command</dfn>: The SQL command that was executed (e.g. "SELECT", "UPDATE", etc.)</li>
9-
<li><dfn>rowCount</dfn>: The number of rows affected by the SQL statement</li>
10-
<li><dfn>oid</dfn>: The oid returned</li>
11-
<li><dfn>rows</dfn>: An array of rows</li>
12-
</ul>
133

14-
<p>This node uses the <a href="https://github.com/janl/mustache.js">Mustache template system</a> to generate queries based on the message payload:</p>
4+
<h3>Outputs</h3>
5+
<p>The response (rows) is provided in <code>msg.payload</code> as an array.</p>
6+
<p>An exception is if the <em>Split results</em> option is enabled and the <em>Number of rows per message</em> set to 1, then <code>msg.payload</code> is not an array but the single-row response.</p>
7+
<p>Additional information is provided as <code>msg.pgsql.rowCount</code> and <code>msg.pgsql.command</code>. See the <a href="https://node-postgres.com/api/result">underlying documentation</a> for details.</p>
8+
<p>In the case of multiple queries, then <code>msg.pgsql</code> is an array.</p>
9+
10+
<h3>Inputs</h3>
11+
<h4>SQL query template</h4>
12+
<p>This node uses the <a href="https://github.com/janl/mustache.js">Mustache template system</a> to generate queries based on the message:</p>
1513
<pre>
1614
-- INTEGER id column
17-
SELECT * FROM table
18-
WHERE id = {{{ msg.id }}}
15+
SELECT * FROM table WHERE id = {{{ msg.id }}}
1916

2017
-- TEXT id column
21-
SELECT * FROM table
22-
WHERE id = '{{{ msg.id }}}'
18+
SELECT * FROM table WHERE id = '{{{ msg.id }}}'
2319
</pre>
2420

25-
<h3>Parameterized queries</h3>
21+
<h4>Parameterized queries</h4>
2622
<p>Parameters for parameterized queries can be passed as an array in the <code>msg.params</code> object:</p>
2723
<pre>
2824
// In a function, provide parameters for the parameterized query
@@ -31,37 +27,37 @@ <h3>Parameterized queries</h3>
3127

3228
<pre>
3329
-- In this node, use a parameterized query
34-
SELECT * FROM table
35-
WHERE id = $1
30+
SELECT * FROM table WHERE id = $1
3631
</pre>
3732

3833
<h3>Backpressure</h3>
3934
<p>
40-
This node supports <em>backpressure</em> / <em>flow control<em>:
35+
This node supports <em>backpressure</em> / <em>flow control</em>:
4136
when the <em>Split results</em> option is enabled, it waits for a <em>tick</em> before releasing the next batch of lines, to make sure the rest of your Node-RED flow is ready to process more data
4237
(instead of risking an out-of-memory condition), and also conveys this information upstream.
43-
38+
</p><p>
4439
So when the <em>Split results</em> option is enabled, this node will only output one message at first, and then awaits a message containing a truthy <code>msg.tick</code> before releasing the next message.
45-
40+
</p><p>
4641
To make this behaviour potentially automatic (avoiding manual wires), this node declares its ability by exposing a truthy <code>node.tickConsumer</code> for downstream nodes to detect this feature,
4742
and a truthy <code>node.tickProvider</code> for upstream nodes.
4843
Likewise, this node detects upstream nodes using the same back-pressure convention, and automatically sends ticks.
4944
</p>
5045

51-
<h3>Sequences</h3>
46+
<h3>Sequences for split results</h3>
5247
<p>
53-
When the <em>Split results</em> option is enabled, the messages contain some information following the conventions for <a href="https://nodered.org/docs/user-guide/messages#message-sequences"><em>messages sequences</em></a>.
48+
When the <em>Split results</em> option is enabled (streaming), the messages contain some information following the conventions for <a href="https://nodered.org/docs/user-guide/messages#message-sequences"><em>messages sequences</em></a>.
5449
</p>
5550

5651
<pre>
5752
{
58-
payload: '...',
59-
parts: {
60-
id: 0.1234, // sequence ID from upstream or randomly generated (changes for every sequence)
61-
index: 5, // incremented for each message of the same sequence
62-
count: 6, // total number of messages; only available in the last message of a sequence
63-
},
64-
complete: true, // True only for the last message of a sequence
53+
payload: '...',
54+
parts: {
55+
id: 0.1234, // sequence ID, randomly generated (changes for every sequence)
56+
index: 5, // incremented for each message of the same sequence
57+
count: 6, // total number of messages; only available in the last message of a sequence
58+
parts: {}, // optional upstream parts information
59+
},
60+
complete: true, // True only for the last message of a sequence
6561
}
6662
</pre>
6763

locales/en-US/postgresql.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
"server": "Server",
1616
"query": "Query",
1717
"split": "Split results in multiple messages",
18-
"rowsPerMsg": "Number of rows per message",
19-
"receiveOuput": "Receive output"
18+
"rowsPerMsg": "Number of rows per message"
2019
},
2120
"placeholder": {
2221
"name": "dbConnection",

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-contrib-postgresql",
3-
"version": "0.1.2",
3+
"version": "0.2.0",
44
"scripts": {
55
"test": "eslint postgresql.js"
66
},

postgresql.html

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@
274274
<i class="icon-tag"></i>
275275
<span data-i18n="postgresql.label.name"></span>
276276
</label>
277-
<input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name"> </div>
277+
<input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name" />
278+
</div>
278279
<div class="form-row">
279280
<label for="node-input-postgreSQLConfig">
280281
<i class="fa fa-server"></i>
@@ -283,13 +284,7 @@
283284
<input type="text" id="node-input-postgreSQLConfig" />
284285
</div>
285286
<div class="form-row">
286-
<input type="checkbox" id="node-input-output" style="display: inline-block; width: auto; vertical-align: top;">
287-
<label for="node-input-output" style="width: auto;">
288-
<span data-i18n="postgresql.label.receiveOuput"></span>
289-
</label>
290-
</div>
291-
<div class="form-row">
292-
<input type="checkbox" id="node-input-split" style="display: inline-block; width: auto; vertical-align: top;">
287+
<input type="checkbox" id="node-input-split" style="display: inline-block; width: auto; vertical-align: top;" />
293288
<label for="node-input-split" style="width: auto;">
294289
<span data-i18n="postgresql.label.split"></span>
295290
</label>
@@ -305,7 +300,8 @@
305300
<i class="fa fa-file-code-o"></i>
306301
<span data-i18n="postgresql.label.query"></span>
307302
</label>
308-
<input type="hidden" id="node-input-query" autofocus="autofocus"> </div>
303+
<input type="hidden" id="node-input-query" autofocus="autofocus" />
304+
</div>
309305
<div class="form-row node-text-editor-row">
310306
<div style="height: 300px; min-height: 150px;" class="node-text-editor" id="node-input-editor"></div>
311307
</div>
@@ -326,9 +322,6 @@
326322
type: 'postgreSQLConfig',
327323
required: true,
328324
},
329-
output: {
330-
value: true,
331-
},
332325
split: {
333326
value: false,
334327
},
@@ -349,9 +342,8 @@
349342
return this.name ? 'node_label_italic' : '';
350343
},
351344
oneditprepare: function () {
352-
$('#node-input-output').prop('checked', this.output);
353345
$('#node-input-split').prop('checked', this.split);
354-
$('#node-input-rowsPerMsg' ).value = this.rowsPerMsg;
346+
$('#node-input-rowsPerMsg').value = this.split ? this.rowsPerMsg : 1;
355347
this.editor = RED.editor.createEditor({
356348
id: 'node-input-editor',
357349
mode: 'ace/mode/sql',

0 commit comments

Comments
 (0)