Skip to content

Commit a2f82f6

Browse files
author
Toni Sharpe
committed
feature(automatic-transactions): Creates a decorator to make transactions easier for the developer
Resolves #75
1 parent 1c78d07 commit a2f82f6

7 files changed

+1103
-4
lines changed

index.js

+7
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ function fastifyPostgres (fastify, options, next) {
102102
}
103103
}
104104

105+
if (!fastify.hasRequestDecorator('pg')) {
106+
fastify.decorateRequest('pg', null)
107+
fastify.addHook('onRequest', async (req, reply) => {
108+
req.pg = fastify.pg
109+
})
110+
}
111+
105112
next()
106113
}
107114

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "index.js",
66
"types": "index.d.ts",
77
"scripts": {
8+
"testonly": "tap -J test/*.test.js && npm run test:typescript",
89
"test": "standard && tap -J test/*.test.js && npm run test:typescript",
910
"test:typescript": "tsd",
1011
"test:report": "standard && tap -J --coverage-report=html test/*.test.js",

test/initialization.test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ test('Should throw when trying to register multiple instances without giving a n
110110
})
111111
})
112112

113-
test('Should not throw when registering a named instance and an unnamed instance)', (t) => {
113+
test('Should not throw when registering a named instance and an unnamed instance', (t) => {
114114
t.plan(1)
115115

116116
const fastify = Fastify()
@@ -191,8 +191,8 @@ test('fastify.pg namespace should exist', (t) => {
191191
})
192192
})
193193

194-
test('fastify.pg.test namespace should exist', (t) => {
195-
t.plan(6)
194+
test('fastify.pg custom namespace should exist if a name is set', (t) => {
195+
t.plan(7)
196196

197197
const fastify = Fastify()
198198
t.teardown(() => fastify.close())
@@ -210,5 +210,6 @@ test('fastify.pg.test namespace should exist', (t) => {
210210
t.ok(fastify.pg.test.connect)
211211
t.ok(fastify.pg.test.pool)
212212
t.ok(fastify.pg.test.Client)
213+
t.ok(fastify.hasRequestDecorator('pg'))
213214
})
214215
})

test/query.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const t = require('tap')
44
const test = t.test
55
const Fastify = require('fastify')
66
const fastifyPostgres = require('../index')
7+
78
const {
89
BAD_DB_NAME,
910
connectionString,
@@ -134,7 +135,7 @@ test('When fastify.pg root namespace is used:', (t) => {
134135
t.end()
135136
})
136137

137-
test('When fastify.pg.test namespace is used:', (t) => {
138+
test('When fastify.pg custom namespace is used:', (t) => {
138139
t.test('Should be able to connect and perform a query', (t) => {
139140
t.plan(4)
140141

test/req-initialization.test.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const test = t.test
5+
const Fastify = require('fastify')
6+
const fastifyPostgres = require('../index')
7+
const { connectionString } = require('./helpers')
8+
9+
test('fastify request should have the correct decoration', t => {
10+
t.plan(5)
11+
const fastify = Fastify()
12+
t.teardown(() => fastify.close())
13+
14+
fastify.register(fastifyPostgres, {
15+
connectionString
16+
})
17+
18+
fastify.route({
19+
method: 'GET',
20+
url: '/users',
21+
handler: (req, reply) => {
22+
reply.send('response!')
23+
t.ok(req.pg)
24+
t.ok(req.pg.query)
25+
}
26+
})
27+
28+
fastify.inject({
29+
method: 'GET',
30+
url: '/users'
31+
}, (err, res) => {
32+
t.error(err)
33+
})
34+
35+
fastify.ready((err) => {
36+
t.error(err)
37+
38+
t.ok(fastify.hasRequestDecorator('pg'))
39+
})
40+
})
41+
42+
test('fastify request should have the correct decoration at the custom namespace', t => {
43+
t.plan(5)
44+
const fastify = Fastify()
45+
t.teardown(() => fastify.close())
46+
47+
fastify.register(fastifyPostgres, {
48+
connectionString,
49+
name: 'test'
50+
})
51+
52+
fastify.route({
53+
method: 'GET',
54+
url: '/users',
55+
handler: (req, reply) => {
56+
reply.send('response!')
57+
t.ok(req.pg.test)
58+
t.ok(req.pg.test.query)
59+
}
60+
})
61+
62+
fastify.inject({
63+
method: 'GET',
64+
url: '/users'
65+
}, (err, res) => {
66+
t.error(err)
67+
})
68+
69+
fastify.ready((err) => {
70+
t.error(err)
71+
72+
t.ok(fastify.hasRequestDecorator('pg'))
73+
})
74+
})

0 commit comments

Comments
 (0)