Skip to content

Commit 1fab7a1

Browse files
feat: Add multihost support for native js driver
1 parent c78b302 commit 1fab7a1

File tree

7 files changed

+1043
-59
lines changed

7 files changed

+1043
-59
lines changed

.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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,16 @@ 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

74+
const hosts = Array.isArray(this.host) ? this.host : [this.host]
75+
const ports = Array.isArray(this.port) ? this.port : [this.port]
76+
if (ports.length !== 1 && ports.length !== hosts.length) {
77+
throw new Error(`ports must have either 1 entry or the same number of entries as hosts (${hosts.length})`)
78+
}
79+
7380
// "hiding" the password so it doesn't show up in stack traces
7481
// or if the client is console.logged
7582
Object.defineProperty(this, 'password', {
@@ -111,6 +118,17 @@ class ConnectionParameters {
111118
this.idle_in_transaction_session_timeout = val('idle_in_transaction_session_timeout', config, false)
112119
this.query_timeout = val('query_timeout', config, false)
113120

121+
this.targetSessionAttrs = val('targetSessionAttrs', config)
122+
123+
const validTargetSessionAttrs = ['any', 'read-write', 'read-only', 'primary', 'standby', 'prefer-standby']
124+
if (this.targetSessionAttrs && !validTargetSessionAttrs.includes(this.targetSessionAttrs)) {
125+
throw new Error(
126+
`invalid targetSessionAttrs value: "${this.targetSessionAttrs}". Must be one of: ${validTargetSessionAttrs.join(
127+
', '
128+
)}`
129+
)
130+
}
131+
114132
if (config.connectionTimeoutMillis === undefined) {
115133
this.connect_timeout = process.env.PGCONNECT_TIMEOUT || 0
116134
} else {

0 commit comments

Comments
 (0)