Skip to content

Commit 52f67d3

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 80c500f commit 52f67d3

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

packages/pg/lib/connection-parameters.js

+27
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
@@ -120,6 +135,8 @@ class ConnectionParameters {
120135

121136
getLibpqConnectionString(cb) {
122137
var params = []
138+
var pgOptions = []
139+
123140
add(params, this, 'user')
124141
add(params, this, 'password')
125142
add(params, this, 'port')
@@ -128,6 +145,16 @@ class ConnectionParameters {
128145
add(params, this, 'connect_timeout')
129146
add(params, this, 'options')
130147

148+
addOption(pgOptions, this, 'statement_timeout')
149+
addOption(pgOptions, this, 'idle_in_transaction_session_timeout')
150+
151+
if (this.options) {
152+
pgOptions.push(this.options)
153+
}
154+
if (pgOptions.length > 0) {
155+
params.push('options=' + quoteParamValue(pgOptions.join(' ')))
156+
}
157+
131158
var ssl = typeof this.ssl === 'object' ? this.ssl : this.ssl ? { sslmode: this.ssl } : {}
132159
add(params, ssl, 'sslmode')
133160
add(params, ssl, 'sslca')

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

+20
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,26 @@ test('libpq connection string building', function () {
172172
)
173173
})
174174

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

0 commit comments

Comments
 (0)