Skip to content

Commit

Permalink
Updated to Fastify v4 (#131)
Browse files Browse the repository at this point in the history
* Updated to Fastify v4

Signed-off-by: Matteo Collina <[email protected]>

* fixup

Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored May 13, 2022
1 parent 70560a3 commit 05db2cf
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 106 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ jobs:
strategy:
matrix:
node-version:
- 10
- 12
- 14
- 16
- 18
services:
postgres:
image: postgres:11-alpine
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,6 @@ function fastifyPostgres (fastify, options, next) {
}

module.exports = fp(fastifyPostgres, {
fastify: '>=1.1.0',
name: 'fastify-postgres'
fastify: '4.x',
name: '@fastify/postgres'
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"devDependencies": {
"@tsconfig/node10": "^1.0.8",
"@types/pg": "^8.6.1",
"fastify": "^3.21.5",
"fastify": "^4.0.0-rc.2",
"pg": "^8.7.1",
"pg-native": "^3.0.0",
"standard": "^17.0.0",
Expand Down
103 changes: 45 additions & 58 deletions test/req-initialization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ test('When we use the fastify-postgres transaction route option', t => {
fastify.get('/count-users', async (req, reply) => {
const result = await fastify.pg.query('SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'')

reply.send(result)
return result
})

fastify.get('/pass', { pg: { transact: true } }, async (req, reply) => {
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in'])
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in'])
reply.send('complete')
return 'complete'
})

await fastify.inject({ url: '/pass' })
Expand All @@ -54,14 +54,14 @@ test('When we use the fastify-postgres transaction route option', t => {
fastify.get('/count-users', async (req, reply) => {
const result = await fastify.pg.test.query('SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'')

reply.send(result)
return result
})

fastify.get('/pass', { pg: { transact: 'test' } }, async (req, reply) => {
await req.pg.test.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in'])
await req.pg.test.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in'])

reply.send('complete')
return 'complete'
})

await fastify.inject({ url: '/pass' })
Expand Down Expand Up @@ -155,12 +155,11 @@ test('Should not add hooks with combinations of registration `options.name` and

fastify.register(fastifyPostgres, {
connectionString
}).after(() => {
fastify.get('/', (req, reply) => {
t.equal(req.pg, null)
})
})

fastify.get('/', (req, reply) => {
t.equal(req.pg, null)
})

fastify.inject({ url: '/' })
})

Expand All @@ -173,10 +172,10 @@ test('Should not add hooks with combinations of registration `options.name` and
fastify.register(fastifyPostgres, {
connectionString,
name: 'test'
})

fastify.get('/', (req, reply) => {
t.equal(req.pg, null)
}).after(() => {
fastify.get('/', (req, reply) => {
t.equal(req.pg, null)
})
})

fastify.inject({ url: '/' })
Expand All @@ -191,10 +190,10 @@ test('Should not add hooks with combinations of registration `options.name` and
fastify.register(fastifyPostgres, {
connectionString,
name: 'test'
})

fastify.get('/', { pg: { transact: true } }, (req, reply) => {
t.equal(req.pg, null)
}).after(() => {
fastify.get('/', { pg: { transact: true } }, (req, reply) => {
t.equal(req.pg, null)
})
})

fastify.inject({ url: '/' })
Expand All @@ -208,10 +207,10 @@ test('Should not add hooks with combinations of registration `options.name` and

fastify.register(fastifyPostgres, {
connectionString
})

fastify.get('/', { pg: { transact: 'test' } }, (req, reply) => {
t.equal(req.pg, null)
}).after(() => {
fastify.get('/', { pg: { transact: 'test' } }, (req, reply) => {
t.equal(req.pg, null)
})
})

fastify.inject({ url: '/' })
Expand All @@ -226,10 +225,10 @@ test('Should not add hooks with combinations of registration `options.name` and
fastify.register(fastifyPostgres, {
connectionString,
name: 'test'
})

fastify.get('/', { pg: { transact: 'different' } }, (req, reply) => {
t.equal(req.pg, null)
}).after(() => {
fastify.get('/', { pg: { transact: 'different' } }, (req, reply) => {
t.equal(req.pg, null)
})
})

fastify.inject({ url: '/' })
Expand All @@ -239,40 +238,34 @@ test('Should not add hooks with combinations of registration `options.name` and
})

test('Should throw errors with incorrect combinations of registration `options.name` and route options `pg.transact`', t => {
t.test('Should throw an error when `name` is set as reserved keyword', t => {
t.plan(2)

t.test('Should throw an error when `name` is set as reserved keyword', async t => {
const fastify = Fastify()
t.teardown(() => fastify.close())

const name = 'user'

fastify.register(fastifyPostgres, {
await fastify.register(fastifyPostgres, {
connectionString,
name
})

fastify.get('/', { pg: { transact: name } }, (req, reply) => {})

fastify.inject({ url: '/' }, (err, response) => {
t.error(err)
t.same(response.json(), {
statusCode: 500,
error: 'Internal Server Error',
message: `request client '${name}' does not exist`
})
const response = await fastify.inject({ url: '/' })
t.same(response.json(), {
statusCode: 500,
error: 'Internal Server Error',
message: `request client '${name}' does not exist`
})
})

t.test('Should throw an error when pg client has already been registered with the same name', t => {
t.plan(2)

t.test('Should throw an error when pg client has already been registered with the same name', async t => {
const fastify = Fastify()
t.teardown(() => fastify.close())

const name = 'test'

fastify.register(fastifyPostgres, {
await fastify.register(fastifyPostgres, {
connectionString,
name
})
Expand All @@ -281,37 +274,31 @@ test('Should throw errors with incorrect combinations of registration `options.n
})
fastify.get('/', { pg: { transact: name } }, (req, reply) => {})

fastify.inject({ url: '/' }, (err, response) => {
t.error(err)
t.same(response.json(), {
statusCode: 500,
error: 'Internal Server Error',
message: `request client '${name}' has already been registered`
})
const response = await fastify.inject({ url: '/' })
t.same(response.json(), {
statusCode: 500,
error: 'Internal Server Error',
message: `request client '${name}' has already been registered`
})
})

t.test('Should throw an error when pg client has already been registered', t => {
t.plan(2)

t.test('Should throw an error when pg client has already been registered', async t => {
const fastify = Fastify()
t.teardown(() => fastify.close())

fastify.register(fastifyPostgres, {
await fastify.register(fastifyPostgres, {
connectionString
})
fastify.addHook('onRequest', async (req, reply) => {
req.pg = await fastify.pg.connect()
})
fastify.get('/', { pg: { transact: true } }, (req, reply) => {})

fastify.inject({ url: '/' }, (err, response) => {
t.error(err)
t.same(response.json(), {
statusCode: 500,
error: 'Internal Server Error',
message: 'request client has already been registered'
})
const response = await fastify.inject({ url: '/' })
t.same(response.json(), {
statusCode: 500,
error: 'Internal Server Error',
message: 'request client has already been registered'
})
})

Expand Down
43 changes: 0 additions & 43 deletions test/transaction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,49 +418,6 @@ test('When fastify.pg.test namespace is used:', (t) => {
})
})

t.test('Should be able to use transact util with a commit callback', (t) => {
t.plan(4)

const fastify = Fastify()
t.teardown(() => fastify.close())

fastify.register(fastifyPostgres, {
connectionString,
name: 'test'
})

fastify.ready((err) => {
t.error(err)

fastify.pg.test.transact(
(client, commit) => {
client.query(
'INSERT INTO users(username) VALUES($1) RETURNING id',
['namespace-commit-callback'],
(err, id) => {
commit(err, id)
}
)
},
function (err, result) {
t.error(err)
t.equal(result.rows.length, 1)

const userId = result.rows[0].id

fastify.pg.test
.query('SELECT * FROM users WHERE id = $1', [userId])
.then((result) => {
t.equal(result.rows[0].username, 'namespace-commit-callback')
})
.catch((err) => {
t.fail(err)
})
}
)
})
})

t.test('Should trigger a rollback when something goes wrong (with callback)', (t) => {
t.plan(9)

Expand Down

0 comments on commit 05db2cf

Please sign in to comment.