Skip to content

Commit 90865cd

Browse files
committed
Rename timeout option to idle_timeout
1 parent e61e8f4 commit 90865cd

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const sql = postgres('postgres://username:password@host:port/database', {
5252
password : '', // Password of database user
5353
ssl : false, // True, or options for tls.connect
5454
max : 10, // Max number of connections
55-
timeout : 0, // Idle connection timeout in seconds
55+
idle_timeout: 0, // Idle connection timeout in seconds
5656
types : [], // Array of custom types, see more below
5757
onnotice : fn // Defaults to console.log
5858
onparameter : fn // (key, value) when server param change

lib/connection.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Connection(options = {}) {
1313
const {
1414
onparameter,
1515
transform,
16-
timeout,
16+
idle_timeout,
1717
onnotify,
1818
onnotice,
1919
parsers
@@ -113,7 +113,7 @@ function Connection(options = {}) {
113113
query.args = args
114114
query.result = []
115115
query.result.count = null
116-
timeout && clearTimeout(timer)
116+
idle_timeout && clearTimeout(timer)
117117

118118
typeof options.debug === 'function' && options.debug(id, str, args)
119119
const buffer = query.simple
@@ -157,7 +157,7 @@ function Connection(options = {}) {
157157

158158
function idle() {
159159
clearTimeout(timer)
160-
timer = setTimeout(socket.end, timeout * 1000)
160+
timer = setTimeout(socket.end, idle_timeout * 1000)
161161
}
162162

163163
function onready(err) {
@@ -181,7 +181,7 @@ function Connection(options = {}) {
181181
}
182182

183183
backend.query = backend.error = null
184-
timeout && queries.length === 0 && idle()
184+
idle_timeout && queries.length === 0 && idle()
185185

186186
if (!open) {
187187
messages.forEach(socket.write)

lib/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ function parseOptions(a, b) {
494494
max : o.max || url.query.max || 10,
495495
types : o.types || {},
496496
ssl : o.ssl || url.ssl || false,
497-
timeout : o.timeout,
497+
idle_timeout: o.idle_timeout || warn(o.timeout, 'The timeout option is deprecated, use idle_timeout instead'),
498498
onnotice : o.onnotice,
499499
onparameter : o.onparameter,
500500
transform : Object.assign({}, o.transform),
@@ -505,6 +505,11 @@ function parseOptions(a, b) {
505505
)
506506
}
507507

508+
function warn(x, message) {
509+
typeof x !== 'undefined' && console.log(message)
510+
return x
511+
}
512+
508513
function osUsername() {
509514
try {
510515
return require('os').userInfo().username // eslint-disable-line

tests/index.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const options = {
2929
db: 'postgres_js_test',
3030
user: login.user,
3131
pass: login.pass,
32-
timeout: 0.5,
32+
idle_timeout: 0.2,
3333
max: 1
3434
}
3535

@@ -273,7 +273,7 @@ t('Throw syntax error', async() =>
273273
t('Connect using uri', async() =>
274274
[true, await new Promise((resolve, reject) => {
275275
const sql = postgres('postgres://' + login.user + ':' + (login.pass || '') + '@localhost:5432/' + options.db, {
276-
timeout: 0.1
276+
idle_timeout: options.idle_timeout
277277
})
278278
sql`select 1`.then(() => resolve(true), reject)
279279
})]
@@ -282,7 +282,7 @@ t('Connect using uri', async() =>
282282
t('Fail with proper error on no host', async() =>
283283
['ECONNREFUSED', (await new Promise((resolve, reject) => {
284284
const sql = postgres('postgres://localhost:33333/' + options.db, {
285-
timeout: 0.1
285+
idle_timeout: options.idle_timeout
286286
})
287287
sql`select 1`.then(reject, resolve)
288288
})).code]
@@ -292,7 +292,7 @@ t('Connect using SSL', async() =>
292292
[true, (await new Promise((resolve, reject) => {
293293
postgres({
294294
ssl: { rejectUnauthorized: false },
295-
timeout: 0.1
295+
idle_timeout: options.idle_timeout
296296
})`select 1`.then(() => resolve(true), reject)
297297
}))]
298298
)
@@ -741,9 +741,7 @@ t('notice works', async() => {
741741
notice = x
742742
}
743743

744-
const sql = postgres({
745-
...options
746-
})
744+
const sql = postgres(options)
747745

748746
await sql`create table if not exists users()`
749747
await sql`create table if not exists users()`
@@ -904,7 +902,7 @@ t('Async stack trace', async() => {
904902
})
905903

906904
t('Debug has long async stack trace', async() => {
907-
const sql = postgres({ debug: true })
905+
const sql = postgres({ ...options, debug: true })
908906

909907
return [
910908
'watyo',

0 commit comments

Comments
 (0)