From 27ea9cfba8855d0dcd197784c0e5449fb6ef34cb Mon Sep 17 00:00:00 2001 From: cemremengu Date: Tue, 25 Sep 2018 12:42:57 +0300 Subject: [PATCH] teardown tests, update README --- README.md | 23 ++++++++++++++++++----- test.js | 11 ++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 03bb129..1ca213b 100644 --- a/README.md +++ b/README.md @@ -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) => { diff --git a/test.js b/test.js index a85416c..cf408ee 100644 --- a/test.js +++ b/test.js @@ -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' @@ -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() }) }) }) @@ -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', @@ -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() }) }) }) @@ -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', @@ -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() }) }) })