Skip to content

Commit 05ff977

Browse files
authored
Dynamic PostgreSQL client config (#9)
* Dynamic PostgreSQL client config * Minor readme * Minor documentation * Additional notes
1 parent 99bf392 commit 05ff977

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,32 @@ msg.params = [ msg.id ];
4747
SELECT * FROM table WHERE id = $1;
4848
```
4949

50+
### Dynamic PostgreSQL connection parameters
51+
52+
If the information about which database server to connect and how needs to be dynamic,
53+
it is possible to pass a [custom client configuration](https://node-postgres.com/api/client) in the message:
54+
55+
```js
56+
msg.pgConfig = {
57+
user?: string, // default process.env.PGUSER || process.env.USER
58+
password?: string, //or function, default process.env.PGPASSWORD
59+
host?: string, // default process.env.PGHOST
60+
database?: string, // default process.env.PGDATABASE || process.env.USER
61+
port?: number, // default process.env.PGPORT
62+
connectionString?: string, // e.g. postgres://user:password@host:5432/database
63+
ssl?: any, // passed directly to node.TLSSocket, supports all tls.connect options
64+
types?: any, // custom type parsers
65+
statement_timeout?: number, // number of milliseconds before a statement in query will time out, default is no timeout
66+
query_timeout?: number, // number of milliseconds before a query call will timeout, default is no timeout
67+
application_name?: string, // The name of the application that created this Client instance
68+
connectionTimeoutMillis?: number, // number of milliseconds to wait for connection, default is no timeout
69+
idle_in_transaction_session_timeout?: number, // number of milliseconds before terminating any session with an open idle transaction, default is no timeout
70+
};
71+
```
72+
73+
However, this does not use a [connection pool](https://node-postgres.com/features/pooling), and is therefore less efficient.
74+
It is therefore recommended in most cases not to use `msg.pgConfig` at all and instead stick to the built-in configuration node.
75+
5076
## Installation
5177

5278
### Using the Node-RED Editor

locales/en-US/postgresql.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@ <h4>Parameterized queries</h4>
3939
SELECT * FROM table WHERE id = $1
4040
</pre>
4141

42+
<h4>Dynamic PostgreSQL connection parameters</h4>
43+
<p>
44+
If the information about which database server to connect and how needs to be dynamic,
45+
it is possible to pass a <a href="https://node-postgres.com/api/client">custom client configuration</a> in the message:
46+
</p>
47+
48+
<pre>
49+
msg.pgConfig = {
50+
user?: string, // default process.env.PGUSER || process.env.USER
51+
password?: string, //or function, default process.env.PGPASSWORD
52+
host?: string, // default process.env.PGHOST
53+
database?: string, // default process.env.PGDATABASE || process.env.USER
54+
port?: number, // default process.env.PGPORT
55+
connectionString?: string, // e.g. postgres://user:password@host:5432/database
56+
ssl?: any, // passed directly to node.TLSSocket, supports all tls.connect options
57+
types?: any, // custom type parsers
58+
statement_timeout?: number, // number of milliseconds before a statement in query will time out, default is no timeout
59+
query_timeout?: number, // number of milliseconds before a query call will timeout, default is no timeout
60+
application_name?: string, // The name of the application that created this Client instance
61+
connectionTimeoutMillis?: number, // number of milliseconds to wait for connection, default is no timeout
62+
idle_in_transaction_session_timeout?: number, // number of milliseconds before terminating any session with an open idle transaction, default is no timeout
63+
};
64+
</pre>
65+
<p>
66+
However, this does not use a <a href="https://node-postgres.com/features/pooling">connection pool</a>, and is therefore less efficient.
67+
It is therefore recommended in most cases not to use `msg.pgConfig` at all and instead stick to the built-in configuration node.
68+
</p>
69+
4270
<h3>Backpressure</h3>
4371
<p>
4472
This node supports <em>backpressure</em> / <em>flow control</em>:

postgresql.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function findInputNodeId(toNode, filter = null) {
2929

3030
module.exports = function (RED) {
3131
const Mustache = require('mustache');
32-
const Pool = require('pg').Pool;
32+
const { Client, Pool } = require('pg');
3333
const Cursor = require('pg-cursor');
3434

3535
function getField(node, kind, value) {
@@ -129,13 +129,17 @@ module.exports = function (RED) {
129129

130130
let client = null;
131131

132-
const handleDone = () => {
132+
const handleDone = async () => {
133133
if (cursor) {
134134
cursor.close();
135135
cursor = null;
136136
}
137137
if (client) {
138-
client.release(true);
138+
if (client.release) {
139+
client.release(true);
140+
} else if (client.end) {
141+
await client.end();
142+
}
139143
client = null;
140144
}
141145
getNextRows = null;
@@ -159,7 +163,12 @@ module.exports = function (RED) {
159163
downstreamReady = true;
160164

161165
try {
162-
client = await node.config.pgPool.connect();
166+
if (msg.pgConfig) {
167+
client = new Client(msg.pgConfig);
168+
await client.connect();
169+
} else {
170+
client = await node.config.pgPool.connect();
171+
}
163172

164173
if (node.split) {
165174
let partsIndex = 0;

0 commit comments

Comments
 (0)