Skip to content

Commit f64fbd4

Browse files
author
Toni Sharpe
committed
Allows namespacing on request
As with the base fastify instance, the pg decoration will use namespaces. For each namespaced called to the main function, the namespace will be added to the req.pg object too, with similar checks The option to use a chosen namespace with the routeOptions is provided and we make sure that this namespace is used to extract the correct client from req.pg A test also covers this for queries wrapped in a route transaction Resolves #75
1 parent d865560 commit f64fbd4

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

index.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ function transact (fn, cb) {
5656
})
5757
}
5858

59+
function extractRequestClient (req, connectionId) {
60+
return connectionId.length ? req.pg[connectionId] : req.pg
61+
}
62+
5963
function fastifyPostgres (fastify, options, next) {
6064
let pg = defaultPg
6165

@@ -111,13 +115,14 @@ function fastifyPostgres (fastify, options, next) {
111115
}
112116

113117
fastify.addHook('onRoute', routeOptions => {
114-
const useTransaction = routeOptions.pg && routeOptions.pg.transact
118+
const transactionConnection = routeOptions.pg && routeOptions.pg.transact
115119

116-
if (useTransaction) {
120+
if (transactionConnection) {
117121
const preHandler = async (req, reply) => {
118122
const client = await pool.connect()
119123

120124
if (name) {
125+
req.pg = {}
121126
if (client[name]) {
122127
throw new Error(`pg client '${name}' is a reserved keyword`)
123128
} else if (req.pg[name]) {
@@ -133,21 +138,22 @@ function fastifyPostgres (fastify, options, next) {
133138
}
134139
}
135140

136-
name ? req.pg[name].query('BEGIN') : req.pg.query('BEGIN')
141+
extractRequestClient(req, transactionConnection).query('BEGIN')
137142
}
138143

139144
const onError = (req, reply, error, done) => {
140145
req[transactionFailedSymbol] = true
141-
name ? req.pg[name].query('ROLLBACK', done) : req.pg.query('ROLLBACK', done)
146+
extractRequestClient(req, transactionConnection).query('ROLLBACK', done)
142147
}
143148

144149
const onSend = async (req) => {
150+
const requestClient = extractRequestClient(req, transactionConnection)
145151
try {
146152
if (!req[transactionFailedSymbol]) {
147-
await name ? req.pg[name].query('COMMIT') : req.pg.query('COMMIT')
153+
await requestClient.query('COMMIT')
148154
}
149155
} finally {
150-
name ? req.pg[name].release() : req.pg.release()
156+
requestClient.release()
151157
}
152158
}
153159

test/req-initialization.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,42 @@ test('fastify postgress useTransaction route option - ', t => {
4343

4444
t.is(extractUserCount(response), 2)
4545
})
46+
test('queries that succeed provided to a namespace', async t => {
47+
const fastify = Fastify()
48+
t.teardown(() => fastify.close())
49+
50+
await fastify.register(fastifyPostgres, {
51+
connectionString,
52+
name: 'test'
53+
})
54+
55+
await fastify.pg.test.query('TRUNCATE users')
56+
57+
await fastify.get('/count-users', async (req, reply) => {
58+
const result = await fastify.pg.test.query('SELECT COUNT(*) AS "userCount" FROM users WHERE username=\'pass-opt-in\'')
59+
60+
reply.send(result)
61+
})
62+
63+
await fastify.get('/pass', { pg: { transact: 'test' } }, async (req, reply) => {
64+
await req.pg.test.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in'])
65+
await req.pg.test.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['pass-opt-in'])
66+
67+
reply.send('complete')
68+
})
69+
70+
await fastify.inject({
71+
method: 'GET',
72+
url: '/pass'
73+
})
74+
75+
const response = await fastify.inject({
76+
method: 'GET',
77+
url: '/count-users'
78+
})
79+
80+
t.is(extractUserCount(response), 2)
81+
})
4682
test('queries that fail provided', async t => {
4783
const fastify = Fastify()
4884
t.teardown(() => fastify.close())

0 commit comments

Comments
 (0)