Skip to content

Commit 52317fe

Browse files
author
Toni Sharpe
committed
WIP()
1 parent 90febc6 commit 52317fe

File tree

2 files changed

+67
-55
lines changed

2 files changed

+67
-55
lines changed

index.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,23 @@ function fastifyPostgres (fastify, options, next) {
116116
if (useTransaction) {
117117
const preHandler = async (req, reply) => {
118118
const client = await pool.connect()
119-
req.pg = client
119+
120+
if (name) {
121+
if (client[name]) {
122+
return next(new Error(`pg client '${name}' is a reserved keyword`))
123+
} else if (fastify.pg[name]) {
124+
return next(new Error(`request client '${name}' has already been registered`))
125+
}
126+
127+
req.pg[name] = client
128+
} else {
129+
if (req.pg && req.pg.query) {
130+
return next(new Error('request client has already been registered'))
131+
} else {
132+
req.pg = client
133+
}
134+
}
135+
120136
req.pg.query('BEGIN')
121137
}
122138

test/req-initialization.test.js

+50-54
Original file line numberDiff line numberDiff line change
@@ -9,78 +9,74 @@ const { connectionString } = require('./helpers')
99
const extractUserCount = response => parseInt(JSON.parse(response.payload).rows[0].userCount)
1010

1111
test('fastify postgress useTransaction route option - ', t => {
12-
test('set to true - ', t => {
13-
test('passing queries provided', async t => {
14-
const fastify = Fastify()
15-
t.teardown(() => fastify.close())
12+
test('queries that succeed provided', async t => {
13+
const fastify = Fastify()
14+
t.teardown(() => fastify.close())
1615

17-
await fastify.register(fastifyPostgres, {
18-
connectionString
19-
})
20-
21-
await fastify.pg.query('TRUNCATE users')
16+
await fastify.register(fastifyPostgres, {
17+
connectionString
18+
})
2219

23-
await fastify.get('/count-users', async (req, reply) => {
24-
const result = await fastify.pg.query('SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'')
20+
await fastify.pg.query('TRUNCATE users')
2521

26-
reply.send(result)
27-
})
22+
await fastify.get('/count-users', async (req, reply) => {
23+
const result = await fastify.pg.query('SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'')
2824

29-
await fastify.get('/pass', { pg: { transact: true } }, async (req, reply) => {
30-
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in'])
31-
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in'])
32-
reply.send('complete')
33-
})
25+
reply.send(result)
26+
})
3427

35-
await fastify.inject({
36-
method: 'GET',
37-
url: '/pass'
38-
})
28+
await fastify.get('/pass', { pg: { transact: true } }, async (req, reply) => {
29+
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in'])
30+
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in'])
31+
reply.send('complete')
32+
})
3933

40-
const response = await fastify.inject({
41-
method: 'GET',
42-
url: '/count-users'
43-
})
34+
await fastify.inject({
35+
method: 'GET',
36+
url: '/pass'
37+
})
4438

45-
t.is(extractUserCount(response), 2)
39+
const response = await fastify.inject({
40+
method: 'GET',
41+
url: '/count-users'
4642
})
47-
test('failing queries provided', async t => {
48-
const fastify = Fastify()
49-
t.teardown(() => fastify.close())
5043

51-
await fastify.register(fastifyPostgres, {
52-
connectionString
53-
})
44+
t.is(extractUserCount(response), 2)
45+
})
46+
test('queries that fail provided', async t => {
47+
const fastify = Fastify()
48+
t.teardown(() => fastify.close())
5449

55-
await fastify.pg.query('TRUNCATE users')
50+
await fastify.register(fastifyPostgres, {
51+
connectionString
52+
})
5653

57-
await fastify.get('/count-users', async (req, reply) => {
58-
const result = await fastify.pg.query('SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'fail-opt-in\'')
54+
await fastify.pg.query('TRUNCATE users')
5955

60-
reply.send(result)
61-
})
56+
await fastify.get('/count-users', async (req, reply) => {
57+
const result = await fastify.pg.query('SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'fail-opt-in\'')
6258

63-
await fastify.get('/fail', { pg: { transact: true } }, async (req, reply) => {
64-
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['fail-opt-in'])
65-
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['fail-opt-in'])
66-
await req.pg.query('INSERT INTO nope(username) VALUES($1) RETURNING id', ['fail-opt-in'])
67-
reply.send('complete')
68-
})
59+
reply.send(result)
60+
})
6961

70-
await fastify.inject({
71-
method: 'GET',
72-
url: '/fail'
73-
})
62+
await fastify.get('/fail', { pg: { transact: true } }, async (req, reply) => {
63+
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['fail-opt-in'])
64+
await req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['fail-opt-in'])
65+
await req.pg.query('INSERT INTO nope(username) VALUES($1) RETURNING id', ['fail-opt-in'])
66+
reply.send('complete')
67+
})
7468

75-
const response = await fastify.inject({
76-
method: 'GET',
77-
url: '/count-users'
78-
})
69+
await fastify.inject({
70+
method: 'GET',
71+
url: '/fail'
72+
})
7973

80-
t.is(extractUserCount(response), 0)
74+
const response = await fastify.inject({
75+
method: 'GET',
76+
url: '/count-users'
8177
})
8278

83-
t.end()
79+
t.is(extractUserCount(response), 0)
8480
})
8581

8682
t.end()

0 commit comments

Comments
 (0)