Skip to content

Commit

Permalink
teardown tests, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
cemremengu committed Sep 25, 2018
1 parent dc6540a commit 27ea9cf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,27 @@ fastify.register(require('fastify-postgres'), {

fastify.post('/user/:username', (req, reply) => {
fastify.pg.transact(async client => {
return client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username])
},
function onResult (err, result) {
reply.send(err || result)
try {
const id = await client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username])
reply.send(id)
} catch (err) {
reply.send(err)
}
)
})
})

/* or with a transaction callback
fastify.pg.transact(client => {
return client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username])
},
function onResult (err, result) {
reply.send(err || result)
}
})
*/

/* or with a commit callback
fastify.pg.transact((client, commit) => {
Expand Down
11 changes: 4 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ test('fastify.pg.test use transact util with promise', t => {

const fastify = Fastify()

t.tearDown(fastify.close.bind(fastify))

fastify.register(fastifyPostgres, {
name: 'test',
connectionString: 'postgres://postgres@localhost/postgres'
Expand All @@ -274,15 +276,12 @@ test('fastify.pg.test use transact util with promise', t => {
.query(`SELECT * FROM users WHERE username = 'with-promise'`)
.then(result => {
t.ok(result.rows[0].username === 'with-promise')
fastify.close()
}).catch(err => {
t.fail(err)
fastify.close()
})
})
.catch(err => {
t.fail(err)
fastify.close()
})
})
})
Expand All @@ -291,6 +290,7 @@ test('fastify.pg.test use transact util with callback', t => {
t.plan(4)

const fastify = Fastify()
t.tearDown(fastify.close.bind(fastify))

fastify.register(fastifyPostgres, {
name: 'test',
Expand All @@ -309,10 +309,8 @@ test('fastify.pg.test use transact util with callback', t => {
.query(`SELECT * FROM users WHERE username = 'with-callback'`)
.then(result => {
t.ok(result.rows[0].username === 'with-callback')
fastify.close()
}).catch(err => {
t.fail(err)
fastify.close()
})
})
})
Expand All @@ -322,6 +320,7 @@ test('fastify.pg.test use transact util with commit callback', t => {
t.plan(4)

const fastify = Fastify()
t.tearDown(fastify.close.bind(fastify))

fastify.register(fastifyPostgres, {
name: 'test',
Expand All @@ -343,10 +342,8 @@ test('fastify.pg.test use transact util with commit callback', t => {
.query(`SELECT * FROM users WHERE username = 'commit-callback'`)
.then(result => {
t.ok(result.rows[0].username === 'commit-callback')
fastify.close()
}).catch(err => {
t.fail(err)
fastify.close()
})
})
})
Expand Down

0 comments on commit 27ea9cf

Please sign in to comment.