Skip to content

Commit f566e3f

Browse files
authored
Merge pull request #1 from porsager/master
Merge latest changes from base repo
2 parents 801350a + 0e91989 commit f566e3f

File tree

7 files changed

+89
-43
lines changed

7 files changed

+89
-43
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- 🚯 1250 LOC - 0 dependencies
55
- 🏷 ES6 Tagged Template Strings at the core
66
- 🏄‍♀️ Simple surface API
7-
- 💬 Chat on [Gitter](https://badges.gitter.im/porsager/postgres.svg)
7+
- 💬 Chat on [Gitter](https://gitter.im/porsager/postgres)
88

99
<br>
1010

@@ -33,10 +33,10 @@ module.exports = sql
3333
// other.js
3434
const sql = require('./db.js')
3535

36-
await sql`
36+
const users = await sql`
3737
select name, age from users
3838
`
39-
// > [{ name: 'Murray', age: 68 }, { name: 'Walter', age 78 }]
39+
// users: [{ name: 'Murray', age: 68 }, { name: 'Walter', age: 78 }]
4040
```
4141

4242
## Connection options `postgres([url], [options])`
@@ -440,7 +440,7 @@ sql.begin(async sql => {
440440

441441
return [user, account]
442442
})
443-
.then(([user, account])) => {
443+
.then(([user, account]) => {
444444
// great success - COMMIT succeeded
445445
})
446446
.catch(() => {

lib/connection.js

+43-28
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function Connection(options = {}) {
3838
, connection = { send, end, destroy }
3939

4040
const socket = postgresSocket(options, {
41-
ready: () => socket.write(frontend.connect(options)),
41+
ready,
4242
data,
4343
error,
4444
close
@@ -66,8 +66,8 @@ function Connection(options = {}) {
6666
? socket.write(frontend.Close())
6767
: socket.write(frontend.Execute(backend.query.cursor.rows))
6868
}).catch(err => {
69-
socket.write(frontend.Close())
7069
backend.query.reject(err)
70+
socket.write(frontend.Close())
7171
})
7272
}
7373

@@ -96,7 +96,7 @@ function Connection(options = {}) {
9696
ended = () => resolve(socket.end())
9797
})
9898

99-
process.nextTick(() => ready && ended())
99+
process.nextTick(() => (ready || !backend.query) && ended())
100100

101101
return promise
102102
}
@@ -114,26 +114,31 @@ function Connection(options = {}) {
114114
}
115115

116116
function send(query, { sig, str, args = [] }) {
117-
query.str = str
118-
query.args = args
119-
query.result = []
120-
query.result.count = null
121-
idle_timeout && clearTimeout(timer)
122-
123-
typeof options.debug === 'function' && options.debug(id, str, args)
124-
const buffer = query.simple
125-
? simple(str, query)
126-
: sig in statements
127-
? prepared(statements[sig], args, query)
128-
: prepare(sig, str, args, query)
129-
130-
ready
131-
? (backend.query = query, ready = false)
132-
: queries.push(query)
133-
134-
open
135-
? socket.write(buffer)
136-
: (messages.push(buffer), connect())
117+
try {
118+
query.str = str
119+
query.args = args
120+
query.result = []
121+
query.result.count = null
122+
idle_timeout && clearTimeout(timer)
123+
124+
typeof options.debug === 'function' && options.debug(id, str, args)
125+
const buffer = query.simple
126+
? simple(str, query)
127+
: sig in statements
128+
? prepared(statements[sig], args, query)
129+
: prepare(sig, str, args, query)
130+
131+
ready
132+
? (backend.query = query, ready = false)
133+
: queries.push(query)
134+
135+
open
136+
? socket.write(buffer)
137+
: (messages.push(buffer), connect())
138+
} catch (err) {
139+
query.reject(err)
140+
idle()
141+
}
137142
}
138143

139144
function connect() {
@@ -174,8 +179,10 @@ function Connection(options = {}) {
174179
}
175180

176181
function idle() {
177-
clearTimeout(timer)
178-
timer = setTimeout(socket.end, idle_timeout * 1000)
182+
if (idle_timeout && !backend.query && queries.length === 0) {
183+
clearTimeout(timer)
184+
timer = setTimeout(socket.end, idle_timeout * 1000)
185+
}
179186
}
180187

181188
function onready(err) {
@@ -200,7 +207,7 @@ function Connection(options = {}) {
200207
}
201208

202209
backend.query = backend.error = null
203-
idle_timeout && queries.length === 0 && idle()
210+
idle()
204211

205212
if (!open) {
206213
messages.forEach(socket.write)
@@ -244,7 +251,6 @@ function Connection(options = {}) {
244251
function postgresSocket(options, {
245252
error,
246253
close,
247-
ready,
248254
data
249255
}) {
250256
let socket
@@ -295,6 +301,15 @@ function postgresSocket(options, {
295301
socket.once('close', onclose)
296302
}
297303

304+
function ready() {
305+
try {
306+
socket.write(frontend.connect(options))
307+
} catch (e) {
308+
error(e)
309+
socket.end()
310+
}
311+
}
312+
298313
const x = {
299314
write: x => {
300315
buffer = buffer ? Buffer.concat([buffer, x]) : Buffer.from(x)
@@ -307,7 +322,7 @@ function postgresSocket(options, {
307322
return Promise.resolve()
308323
},
309324
end: () => {
310-
return new Promise(r => socket ? socket.end(r) : r())
325+
return new Promise(r => socket && !closed ? (socket.once('close', r), socket.end()) : r())
311326
},
312327
connect
313328
}

lib/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ function Postgres(a, b) {
201201
function nested(first, rest) {
202202
const o = Object.create(notPromise)
203203
o.first = first
204-
o.rest = rest.flat()
204+
o.rest = rest.reduce((acc, val) => acc.concat(val), [])
205205
return o
206206
}
207207

@@ -354,7 +354,7 @@ function Postgres(a, b) {
354354
onclose: () => {
355355
Object.entries(listeners).forEach(([channel, fns]) => {
356356
delete listeners[channel]
357-
Promise.allSettled(fns.map(fn => listen(channel, fn)))
357+
Promise.all(fns.map(fn => listen(channel, fn).catch(() => { /* noop */ })))
358358
})
359359
}
360360
},

lib/types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const types = module.exports.types = {
1010
},
1111
number: {
1212
to: 0,
13-
from: [21, 23, 26, 700],
13+
from: [21, 23, 26, 700, 701],
1414
serialize: x => '' + x,
1515
parse: x => +x
1616
},

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postgres",
3-
"version": "2.0.0-beta.0",
3+
"version": "2.0.0-beta.2",
44
"description": "Fastest full featured PostgreSQL client for Node.js",
55
"main": "lib/index.js",
66
"types": "types/index.d.ts",

tests/index.js

+29-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const { t, not, ot } = require('./test.js') // eslint-disable-line
44
const cp = require('child_process')
55
const path = require('path')
6+
const net = require('net')
67

78
/** @type {import('../types')} */
89
const postgres = require('../lib')
@@ -47,7 +48,7 @@ t('Connects with no options', async() => {
4748
const sql = postgres()
4849

4950
const result = (await sql`select 1 as x`)[0].x
50-
sql.end()
51+
await sql.end()
5152

5253
return [1, result]
5354
})
@@ -990,6 +991,7 @@ t('connect_timeout works', async() => {
990991
throw e
991992
end = Date.now()
992993
})
994+
server.close()
993995
return [connect_timeout, Math.floor((end - start) / 100) / 10]
994996
})
995997

@@ -1065,13 +1067,37 @@ t('Insert array in sql()', async() => {
10651067
})
10661068

10671069
t('Automatically creates prepared statements', async() => {
1068-
const sql = postgres({ no_prepare: false })
1070+
const sql = postgres({ ...options, no_prepare: false })
10691071
const result = await sql`select * from pg_prepared_statements`
10701072
return [result[0].statement, 'select * from pg_prepared_statements']
10711073
})
10721074

10731075
t('no_prepare: true disables prepared transactions', async() => {
1074-
const sql = postgres({ no_prepare: true })
1076+
const sql = postgres({ ...options, no_prepare: true })
10751077
const result = await sql`select * from pg_prepared_statements`
10761078
return [0, result.count]
10771079
})
1080+
1081+
t('Catches connection config errors', async() => {
1082+
const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' })
1083+
1084+
return [
1085+
'wat',
1086+
await sql`select 1`.catch((e) => e.message)
1087+
]
1088+
})
1089+
1090+
t('Catches connection config errors with end', async() => {
1091+
const sql = postgres({ ...options, user: { toString: () => { throw new Error('wat') } }, database: 'prut' })
1092+
1093+
return [
1094+
'wat',
1095+
await sql`select 1`.catch((e) => e.message),
1096+
await sql.end()
1097+
]
1098+
})
1099+
1100+
t('Catches query format errors', async() => [
1101+
'wat',
1102+
await sql.unsafe({ toString: () => { throw new Error('wat') } }).catch((e) => e.message)
1103+
])

tests/test.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@ function exit() {
6363
}
6464
})
6565

66-
ignored && console.error('⚠️', ignored, 'ignored test' + (ignored === 1 ? '' : 's', '\n'))
67-
!only && success && !ignored
68-
? console.log('All good')
69-
: process.exit(1) // eslint-disable-line
66+
only
67+
? console.error('⚠️', 'Not all tests were run')
68+
: ignored
69+
? console.error('⚠️', ignored, 'ignored test' + (ignored === 1 ? '' : 's', '\n'))
70+
: success
71+
? console.log('All good')
72+
: console.error('⚠️', 'Not good')
73+
74+
!process.exitCode && (!success || only || ignored) && (process.exitCode = 1)
7075
}

0 commit comments

Comments
 (0)