Skip to content

Commit ca31ecf

Browse files
maxbronnikov10m.bronnikov
authored andcommitted
feat: Add multihost support for native js driver
1 parent c78b302 commit ca31ecf

7 files changed

Lines changed: 936 additions & 59 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ dist
1010
/.eslintcache
1111
.vscode/
1212
manually-test-on-heroku.js
13+
.history

packages/pg/lib/client.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class Client extends EventEmitter {
8181
keepAlive: c.keepAlive || false,
8282
keepAliveInitialDelayMillis: c.keepAliveInitialDelayMillis || 0,
8383
encoding: this.connectionParameters.client_encoding || 'utf8',
84+
targetSessionAttrs: c.targetSessionAttrs || this.connectionParameters.targetSessionAttrs || null,
85+
trustParameterStatus: c.trustParameterStatus || false,
86+
Promise: this._Promise,
8487
})
8588
this._queryQueue = []
8689
this.binary = c.binary || defaults.binary
@@ -155,7 +158,7 @@ class Client extends EventEmitter {
155158
}
156159
}
157160

158-
if (this.host && this.host.indexOf('/') === 0) {
161+
if (!Array.isArray(this.host) && this.host && this.host.indexOf('/') === 0) {
159162
con.connect(this.host + '/.s.PGSQL.' + this.port)
160163
} else {
161164
con.connect(this.port, this.host)
@@ -542,7 +545,7 @@ class Client extends EventEmitter {
542545
if (client.activeQuery === query) {
543546
const con = this.connection
544547

545-
if (this.host && this.host.indexOf('/') === 0) {
548+
if (!Array.isArray(this.host) && this.host && this.host.indexOf('/') === 0) {
546549
con.connect(this.host + '/.s.PGSQL.' + this.port)
547550
} else {
548551
con.connect(this.port, this.host)

packages/pg/lib/connection-parameters.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class ConnectionParameters {
6767
this.database = this.user
6868
}
6969

70-
this.port = parseInt(val('port', config), 10)
70+
const rawPort = val('port', config)
71+
this.port = Array.isArray(rawPort) ? rawPort.map((p) => parseInt(p, 10)) : parseInt(rawPort, 10)
7172
this.host = val('host', config)
7273

7374
// "hiding" the password so it doesn't show up in stack traces
@@ -111,6 +112,8 @@ class ConnectionParameters {
111112
this.idle_in_transaction_session_timeout = val('idle_in_transaction_session_timeout', config, false)
112113
this.query_timeout = val('query_timeout', config, false)
113114

115+
this.targetSessionAttrs = val('targetSessionAttrs', config)
116+
114117
if (config.connectionTimeoutMillis === undefined) {
115118
this.connect_timeout = process.env.PGCONNECT_TIMEOUT || 0
116119
} else {

0 commit comments

Comments
 (0)