Skip to content

Commit 933e397

Browse files
committed
Remove onconnect
1 parent 03df7c1 commit 933e397

File tree

3 files changed

+33
-84
lines changed

3 files changed

+33
-84
lines changed

lib/connection.js

+19-21
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ export default function Connection(options = {}) {
2121
let timer
2222
let id = 1
2323
let ended
24+
let ready = false
2425

2526
const queries = Queue()
2627
, statements = new Map()
27-
, connection = { send, end, destroy, ready: false, active: false }
28+
, connection = { send, end, destroy }
2829

2930
const socket = postgresSocket(options, {
31+
ready: () => socket.write(frontend.connect(options)),
3032
data,
31-
ready,
3233
error,
3334
close
3435
})
@@ -60,13 +61,13 @@ export default function Connection(options = {}) {
6061
function resolve(x) {
6162
backend.query.resolve(x)
6263
backend.query = null
63-
timeout && connection.active && queries.length === 0 && idle()
64+
timeout && queries.length === 0 && idle()
6465
}
6566

6667
function reject(err) {
6768
backend.query ? backend.query.reject(err) : error(err)
6869
backend.query = null
69-
timeout && connection.active && queries.length === 0 && idle()
70+
timeout && queries.length === 0 && idle()
7071
}
7172

7273
function end() {
@@ -96,19 +97,21 @@ export default function Connection(options = {}) {
9697
query.result = []
9798
query.result.count = null
9899
timeout && clearTimeout(timer)
99-
!connection.ready || backend.query
100-
? queries.push(query)
101-
: (backend.query = query)
102100

103101
const buffer = query.simple
104102
? simple(str, query)
105103
: statements.has(sig)
106104
? prepared(statements.get(sig), args, query)
107105
: prepare(sig, str, args, query)
108106

109-
connection.ready
110-
? socket.write(buffer)
111-
: (messages.push(buffer), socket.connect())
107+
if (ready && !backend.query) {
108+
backend.query = query
109+
socket.write(buffer)
110+
} else {
111+
queries.push(query)
112+
messages.push(buffer)
113+
!ready && socket.connect()
114+
}
112115
}
113116

114117
function simple(str, query) {
@@ -135,18 +138,17 @@ export default function Connection(options = {}) {
135138
}
136139

137140
function onready() {
138-
if (!backend.query)
139-
backend.query = queries.shift()
140-
141141
if (!backend.query && queries.length === 0 && ended)
142142
return ended()
143143

144-
if (!connection.ready) {
144+
if (!ready) {
145145
messages.forEach(socket.write)
146146
messages = []
147-
connection.ready = true
148-
connection.onconnect && connection.onconnect()
147+
ready = true
149148
}
149+
150+
if (!backend.query)
151+
backend.query = queries.shift()
150152
}
151153

152154
function data(x) {
@@ -164,14 +166,10 @@ export default function Connection(options = {}) {
164166
}
165167
}
166168

167-
function ready() {
168-
socket.write(frontend.connect(options))
169-
}
170-
171169
function close() {
172170
error(errors.connection('CLOSED', options))
173171
statements.clear()
174-
connection.ready = connection.active = false
172+
ready = false
175173
}
176174

177175
function unknown() {

lib/index.js

+2-34
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,7 @@ export default function Postgres(url, options) {
184184
function getConnection(reserve) {
185185
const connection = --max >= 0 ? createConnection(options) : connections.shift()
186186
!options.fifo && !reserve && connection && connections.push(connection)
187-
connection && (connection.active = !(options.onconnect && !connection.ready))
188-
return options.onconnect && !connection.ready
189-
? instance(options.onconnect, connection)
190-
: connection
187+
return connection
191188
}
192189

193190
function createConnection(options) {
@@ -198,34 +195,6 @@ export default function Postgres(url, options) {
198195
return connection
199196
}
200197

201-
function instance(fn, connection) {
202-
let queries = 0
203-
, container
204-
205-
addTypes(scoped, connection)
206-
connection.onconnect = onconnect
207-
208-
function scoped(xs, ...args) {
209-
queries++
210-
const promise = query({}, connection, xs, args)
211-
promise.then(finished, finished)
212-
return promise
213-
}
214-
215-
function onconnect() {
216-
container = fetchArrayTypes().then(() => fn(scoped))
217-
queries === 0 && finished(queries++)
218-
}
219-
220-
function finished() {
221-
--queries === 0 && Promise.resolve(container).then(() => {
222-
connections.push(connection)
223-
connection.active = true
224-
next()
225-
})
226-
}
227-
}
228-
229198
function array(value) {
230199
return {
231200
type: inferType(value) || 25,
@@ -495,10 +464,9 @@ function parseOptions(uri = {}, options) {
495464
types : o.types || {},
496465
ssl : o.ssl || url.ssl || false,
497466
timeout : o.timeout,
498-
onconnect : o.onconnect,
499467
onnotice : o.onnotice,
500468
onparameter : o.onparameter,
501-
none : crypto.randomBytes(18).toString('base64'),
469+
nonce : crypto.randomBytes(18).toString('base64'),
502470
transform : { ...o.transform },
503471
connection : { application_name: 'postgres.js', ...o.connection },
504472
...mergeUserTypes(o.types)

tests/index.js

+12-29
Original file line numberDiff line numberDiff line change
@@ -355,36 +355,8 @@ t('responds with server parameters (application_name)', async() =>
355355
})`select 1`.catch(reject))]
356356
)
357357

358-
t('onconnect', async() => {
359-
const sql = postgres({
360-
...options,
361-
onconnect: () => 'connected'
362-
})
363-
364-
return [1, (await sql`select 1 as x`)[0].x]
365-
})
366-
367-
t('onconnect runs first', async() => {
368-
const results = []
369-
const sql = postgres({
370-
...options,
371-
onconnect: sql => sql`select 1`.then(() => results.push('onconnect'))
372-
})
373-
374-
const x = await sql`select 1 as x`
375-
results.push(x)
376-
377-
return ['onconnect', results[0]]
378-
})
379-
380358
t('has server parameters', async() => {
381-
return ['postgres.js', await new Promise((resolve, reject) => {
382-
const sql = postgres({
383-
...options,
384-
onconnect: () => resolve(sql.parameters.application_name)
385-
})
386-
sql`select 1`.catch(reject)
387-
})]
359+
return ['postgres.js', (await sql`select 1`.then(() => sql.parameters.application_name))]
388360
})
389361

390362
t('big query body', async() => {
@@ -498,3 +470,14 @@ t('dynamic select args', async () => {
498470
await sql`insert into test (a, b) values (42, 'yay')`
499471
return ['yay', (await sql`select ${ sql('a', 'b') } from test`)[0].b]
500472
}, () => sql`drop table test`)
473+
474+
ot('connection parameters', async() => {
475+
const sql = postgres({
476+
...options,
477+
connection: {
478+
'some.var': 'yay'
479+
}
480+
})
481+
482+
return ['yay', (await sql`select current_setting('some.var') as x`)[0].x]
483+
})

0 commit comments

Comments
 (0)