Skip to content

Commit f45b1a6

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

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-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: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ test('fastify.pg.test use transact util with promise', t => {
258258
t.plan(3)
259259

260260
const fastify = Fastify()
261+
t.tearDown(fastify.close.bind(fastify))
261262

262263
fastify.register(fastifyPostgres, {
263264
name: 'test',
@@ -274,15 +275,12 @@ test('fastify.pg.test use transact util with promise', t => {
274275
.query(`SELECT * FROM users WHERE username = 'with-promise'`)
275276
.then(result => {
276277
t.ok(result.rows[0].username === 'with-promise')
277-
fastify.close()
278278
}).catch(err => {
279279
t.fail(err)
280-
fastify.close()
281280
})
282281
})
283282
.catch(err => {
284283
t.fail(err)
285-
fastify.close()
286284
})
287285
})
288286
})
@@ -291,6 +289,7 @@ test('fastify.pg.test use transact util with callback', t => {
291289
t.plan(4)
292290

293291
const fastify = Fastify()
292+
t.tearDown(fastify.close.bind(fastify))
294293

295294
fastify.register(fastifyPostgres, {
296295
name: 'test',
@@ -309,10 +308,8 @@ test('fastify.pg.test use transact util with callback', t => {
309308
.query(`SELECT * FROM users WHERE username = 'with-callback'`)
310309
.then(result => {
311310
t.ok(result.rows[0].username === 'with-callback')
312-
fastify.close()
313311
}).catch(err => {
314312
t.fail(err)
315-
fastify.close()
316313
})
317314
})
318315
})
@@ -322,6 +319,7 @@ test('fastify.pg.test use transact util with commit callback', t => {
322319
t.plan(4)
323320

324321
const fastify = Fastify()
322+
t.tearDown(fastify.close.bind(fastify))
325323

326324
fastify.register(fastifyPostgres, {
327325
name: 'test',
@@ -343,10 +341,8 @@ test('fastify.pg.test use transact util with commit callback', t => {
343341
.query(`SELECT * FROM users WHERE username = 'commit-callback'`)
344342
.then(result => {
345343
t.ok(result.rows[0].username === 'commit-callback')
346-
fastify.close()
347344
}).catch(err => {
348345
t.fail(err)
349-
fastify.close()
350346
})
351347
})
352348
})

0 commit comments

Comments
 (0)