Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .taprc

This file was deleted.

3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,17 @@ function fastifyPostgres (fastify, options, next) {
}

if (client[name]) {
client.release()
throw new Error(`pg client '${name}' is a reserved keyword`)
} else if (req.pg[name]) {
client.release()
throw new Error(`request client '${name}' has already been registered`)
}

req.pg[name] = client
} else {
if (req.pg) {
client.release()
throw new Error('request client has already been registered')
} else {
req.pg = client
Expand Down
45 changes: 40 additions & 5 deletions test/initialization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const t = require('tap')
const test = t.test
const Fastify = require('fastify')
const pg = require('pg')
const fastifyPostgres = require('../index')
const { connectionString } = require('./helpers')

Expand All @@ -23,7 +24,41 @@ test('Should be able to use native module', (t) => {
fastify.pg
.query('SELECT 1 AS one')
.then((result) => {
t.is(result.rows[0].one, 1)
t.equal(result.rows[0].one, 1)
})
.catch((err) => {
t.fail(err)
})
})
})

test('Should print warning when native module not installed', (t) => {
t.plan(3)

const mockedFastifyPostgres = t.mock('../index', {
pg: { ...pg, native: null }
})
const realConsole = global.console
global.console.warn = (msg) => t.equal(msg, "pg-native not installed, can't use native option - fallback to pg module")

const fastify = Fastify()
t.teardown(() => {
fastify.close()
global.console = realConsole
})

fastify.register(mockedFastifyPostgres, {
connectionString,
native: true
})

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

fastify.pg
.query('SELECT 1 AS one')
.then((result) => {
t.equal(result.rows[0].one, 1)
})
.catch((err) => {
t.fail(err)
Expand All @@ -49,7 +84,7 @@ test('Should be able to use an alternative pg module', (t) => {
fastify.pg
.query('SELECT 1 AS one')
.then((result) => {
t.is(result.rows[0].one, 1)
t.equal(result.rows[0].one, 1)
})
.catch((err) => {
t.fail(err)
Expand Down Expand Up @@ -106,7 +141,7 @@ test('Should throw when trying to register multiple instances without giving a n

fastify.ready((err) => {
t.ok(err)
t.is((err || {}).message, 'fastify-postgres has already been registered')
t.equal((err || {}).message, 'fastify-postgres has already been registered')
})
})

Expand Down Expand Up @@ -149,7 +184,7 @@ test('Should throw when trying to register duplicate connection names', (t) => {

fastify.ready((err) => {
t.ok(err)
t.is((err || {}).message, `fastify-postgres '${name}' instance name has already been registered`)
t.equal((err || {}).message, `fastify-postgres '${name}' instance name has already been registered`)
})
})

Expand All @@ -167,7 +202,7 @@ test('Should throw when trying to register a named connection with a reserved ke

fastify.ready((err) => {
t.ok(err)
t.is((err || {}).message, `fastify-postgres '${name}' is a reserved keyword`)
t.equal((err || {}).message, `fastify-postgres '${name}' is a reserved keyword`)
})
})

Expand Down
10 changes: 5 additions & 5 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ test('When fastify.pg root namespace is used:', (t) => {
t.error(err)

fastify.pg.query('SELECT NOW()', (err, result) => {
t.is(result, undefined)
t.equal(result, undefined)
t.ok(err)
t.is(err.message, `database "${BAD_DB_NAME}" does not exist`)
t.equal(err.message, `database "${BAD_DB_NAME}" does not exist`)
})
})
}
Expand All @@ -127,7 +127,7 @@ test('When fastify.pg root namespace is used:', (t) => {
})
.catch((err) => {
t.ok(err)
t.is(err.message, `database "${BAD_DB_NAME}" does not exist`)
t.equal(err.message, `database "${BAD_DB_NAME}" does not exist`)
})
})
})
Expand Down Expand Up @@ -225,7 +225,7 @@ test('When fastify.pg custom namespace is used:', (t) => {
fastify.pg.test
.query('SELECT 1 AS one')
.then((result) => {
t.is(result.rows[0].one, 1)
t.equal(result.rows[0].one, 1)
})
.catch((err) => {
t.fail(err)
Expand Down Expand Up @@ -254,7 +254,7 @@ test('When fastify.pg custom namespace is used:', (t) => {
})
.catch((err) => {
t.ok(err)
t.is(err.message, `database "${BAD_DB_NAME}" does not exist`)
t.equal(err.message, `database "${BAD_DB_NAME}" does not exist`)
})
})
})
Expand Down
95 changes: 87 additions & 8 deletions test/req-initialization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ test('fastify postgress useTransaction route option', t => {
url: '/count-users'
})

t.is(extractUserCount(response), 2)
t.equal(extractUserCount(response), 2)
})
test('queries that succeed provided to a namespace', async t => {
const fastify = Fastify()
Expand Down Expand Up @@ -71,7 +71,7 @@ test('fastify postgress useTransaction route option', t => {
url: '/count-users'
})

t.is(extractUserCount(response), 2)
t.equal(extractUserCount(response), 2)
})
test('queries that fail provided', async t => {
const fastify = Fastify()
Expand Down Expand Up @@ -103,7 +103,7 @@ test('fastify postgress useTransaction route option', t => {
url: '/count-users'
})

t.is(extractUserCount(response), 0)
t.equal(extractUserCount(response), 0)
})

t.end()
Expand All @@ -121,7 +121,7 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
})

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

fastify.inject({ url: '/' })
Expand All @@ -138,7 +138,7 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
})

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

fastify.inject({ url: '/' })
Expand All @@ -155,7 +155,7 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
})

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

fastify.inject({ url: '/' })
Expand All @@ -171,7 +171,7 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
})

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

fastify.inject({ url: '/' })
Expand All @@ -188,10 +188,89 @@ test('combinations of registrationOptions.name and routeOptions.pg.transact that
})

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

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

test('incorrect combinations of registrationOptions.name and routeOptions.pg.transact should throw errors', t => {
t.test('name set as reserved keyword', t => {
t.plan(2)

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

const name = 'user'

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`
})
})
})

t.test('named pg client has already registered', t => {
t.plan(2)

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

const name = 'test'

fastify.register(fastifyPostgres, {
connectionString,
name
})
fastify.addHook('onRequest', async (req, reply) => {
req.pg = { [name]: await fastify.pg[name].connect() }
})
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`
})
})
})

t.test('pg client has already registered', t => {
t.plan(2)

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

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'
})
})
})
t.end()
})
Loading