Skip to content

Commit 4cc6167

Browse files
author
Shayon Mukherjee
committed
Support idle_in_transaction_session_timeout and statement_timeout for native driver
Right now when using `pg` with `pg-native` the config options `idle_in_transaction_session_timeout` and `statement_timeout` do not get applied. This is due to, when building the connection string for native driver these options aren't passed in. I took advantage of `options` conn parameter and passed both the config options into it using `-c` which postgres supports. This builds on top of the work happened in <TBD LINK>. Also added some tests. Very much open to feedback and comments :).
1 parent 95b5daa commit 4cc6167

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

packages/pg/lib/connection-parameters.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ var add = function (params, config, paramName) {
4545
}
4646
}
4747

48+
var escapeOptionValue = function (value) {
49+
return ('' + value).replace(/\\/g, '\\\\').replace(/ /g, '\\ ')
50+
}
51+
52+
var addOption = function (options, config, optionName) {
53+
if (!config[optionName]) {
54+
return
55+
}
56+
57+
var value = config[optionName]
58+
if (value !== undefined && value !== null) {
59+
options.push('-c ' + optionName + '=' + escapeOptionValue(value))
60+
}
61+
}
62+
4863
class ConnectionParameters {
4964
constructor(config) {
5065
// if a string is passed, it is a raw connection string so we parse it into a config
@@ -115,6 +130,8 @@ class ConnectionParameters {
115130

116131
getLibpqConnectionString(cb) {
117132
var params = []
133+
var pgOptions = []
134+
118135
add(params, this, 'user')
119136
add(params, this, 'password')
120137
add(params, this, 'port')
@@ -123,6 +140,16 @@ class ConnectionParameters {
123140
add(params, this, 'connect_timeout')
124141
add(params, this, 'options')
125142

143+
addOption(pgOptions, this, 'statement_timeout')
144+
addOption(pgOptions, this, 'idle_in_transaction_session_timeout')
145+
146+
if (this.options) {
147+
pgOptions.push(this.options)
148+
}
149+
if (pgOptions.length > 0) {
150+
params.push('options=' + quoteParamValue(pgOptions.join(' ')))
151+
}
152+
126153
var ssl = typeof this.ssl === 'object' ? this.ssl : this.ssl ? { sslmode: this.ssl } : {}
127154
add(params, ssl, 'sslmode')
128155
add(params, ssl, 'sslca')

packages/pg/test/unit/connection-parameters/creation-tests.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ test('libpq connection string building', function () {
149149
var checkForPart = function (array, part) {
150150
assert.ok(array.indexOf(part) > -1, array.join(' ') + ' did not contain ' + part)
151151
}
152+
var checkForOption = function (array, part) {
153+
assert.ok(array.indexOf(part) > -1, array.join(' ') + ' did not contain ' + part)
154+
}
152155

153156
test('builds simple string', function () {
154157
var config = {
@@ -172,6 +175,26 @@ test('libpq connection string building', function () {
172175
)
173176
})
174177

178+
test('builds conn string with options', function () {
179+
var config = {
180+
user: 'brian',
181+
password: 'xyz',
182+
port: 888,
183+
host: 'localhost',
184+
database: 'bam',
185+
statement_timeout: 5000,
186+
idle_in_transaction_session_timeout: 5000,
187+
}
188+
var subject = new ConnectionParameters(config)
189+
subject.getLibpqConnectionString(
190+
assert.calls(function (err, constring) {
191+
assert(!err)
192+
var parts = constring.split(/ (?=([^\']*\'[^\']*\')*[^\']*$)/)
193+
checkForPart(parts, "options='-c statement_timeout=5000 -c idle_in_transaction_session_timeout=5000'")
194+
})
195+
)
196+
})
197+
175198
test('builds dns string', function () {
176199
var config = {
177200
user: 'brian',

0 commit comments

Comments
 (0)