Skip to content

Commit 27ea9cf

Browse files
committed
teardown tests, update README
1 parent dc6540a commit 27ea9cf

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,27 @@ fastify.register(require('fastify-postgres'), {
107107

108108
fastify.post('/user/:username', (req, reply) => {
109109
fastify.pg.transact(async client => {
110-
return client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username])
111-
},
112-
function onResult (err, result) {
113-
reply.send(err || result)
110+
try {
111+
const id = await client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username])
112+
reply.send(id)
113+
} catch (err) {
114+
reply.send(err)
114115
}
115-
)
116+
})
116117
})
117118

119+
/* or with a transaction callback
120+
121+
fastify.pg.transact(client => {
122+
return client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username])
123+
},
124+
function onResult (err, result) {
125+
reply.send(err || result)
126+
}
127+
})
128+
129+
*/
130+
118131
/* or with a commit callback
119132
120133
fastify.pg.transact((client, commit) => {

test.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ test('fastify.pg.test use transact util with promise', t => {
259259

260260
const fastify = Fastify()
261261

262+
t.tearDown(fastify.close.bind(fastify))
263+
262264
fastify.register(fastifyPostgres, {
263265
name: 'test',
264266
connectionString: 'postgres://postgres@localhost/postgres'
@@ -274,15 +276,12 @@ test('fastify.pg.test use transact util with promise', t => {
274276
.query(`SELECT * FROM users WHERE username = 'with-promise'`)
275277
.then(result => {
276278
t.ok(result.rows[0].username === 'with-promise')
277-
fastify.close()
278279
}).catch(err => {
279280
t.fail(err)
280-
fastify.close()
281281
})
282282
})
283283
.catch(err => {
284284
t.fail(err)
285-
fastify.close()
286285
})
287286
})
288287
})
@@ -291,6 +290,7 @@ test('fastify.pg.test use transact util with callback', t => {
291290
t.plan(4)
292291

293292
const fastify = Fastify()
293+
t.tearDown(fastify.close.bind(fastify))
294294

295295
fastify.register(fastifyPostgres, {
296296
name: 'test',
@@ -309,10 +309,8 @@ test('fastify.pg.test use transact util with callback', t => {
309309
.query(`SELECT * FROM users WHERE username = 'with-callback'`)
310310
.then(result => {
311311
t.ok(result.rows[0].username === 'with-callback')
312-
fastify.close()
313312
}).catch(err => {
314313
t.fail(err)
315-
fastify.close()
316314
})
317315
})
318316
})
@@ -322,6 +320,7 @@ test('fastify.pg.test use transact util with commit callback', t => {
322320
t.plan(4)
323321

324322
const fastify = Fastify()
323+
t.tearDown(fastify.close.bind(fastify))
325324

326325
fastify.register(fastifyPostgres, {
327326
name: 'test',
@@ -343,10 +342,8 @@ test('fastify.pg.test use transact util with commit callback', t => {
343342
.query(`SELECT * FROM users WHERE username = 'commit-callback'`)
344343
.then(result => {
345344
t.ok(result.rows[0].username === 'commit-callback')
346-
fastify.close()
347345
}).catch(err => {
348346
t.fail(err)
349-
fastify.close()
350347
})
351348
})
352349
})

0 commit comments

Comments
 (0)