Skip to content

Commit d783fb9

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

File tree

4 files changed

+257
-1
lines changed

4 files changed

+257
-1
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

+69-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ test('fastify.pg namespace should exist', (t) => {
192192
})
193193

194194
test('fastify.pg.test namespace should exist', (t) => {
195-
t.plan(6)
195+
t.plan(7)
196196

197197
const fastify = Fastify()
198198
t.teardown(() => fastify.close())
@@ -210,5 +210,73 @@ 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'))
214+
})
215+
})
216+
217+
test('fastify request should have the correct decoration', t => {
218+
t.plan(5)
219+
const fastify = Fastify()
220+
t.teardown(() => fastify.close())
221+
222+
fastify.register(fastifyPostgres, {
223+
connectionString
224+
})
225+
226+
fastify.route({
227+
method: 'GET',
228+
url: '/users',
229+
handler: (req, reply) => {
230+
reply.send('response!')
231+
t.ok(req.pg)
232+
t.ok(req.pg.query)
233+
}
234+
})
235+
236+
fastify.inject({
237+
method: 'GET',
238+
url: '/users'
239+
}, (err, res) => {
240+
t.error(err)
241+
})
242+
243+
fastify.ready((err) => {
244+
t.error(err)
245+
246+
t.ok(fastify.hasRequestDecorator('pg'))
247+
})
248+
})
249+
250+
test('fastify request should have the correct decoration at the specified namespace', t => {
251+
t.plan(5)
252+
const fastify = Fastify()
253+
t.teardown(() => fastify.close())
254+
255+
fastify.register(fastifyPostgres, {
256+
connectionString,
257+
name: 'test'
258+
})
259+
260+
fastify.route({
261+
method: 'GET',
262+
url: '/users',
263+
handler: (req, reply) => {
264+
reply.send('response!')
265+
t.ok(req.pg.test)
266+
t.ok(req.pg.test.query)
267+
}
268+
})
269+
270+
fastify.inject({
271+
method: 'GET',
272+
url: '/users'
273+
}, (err, res) => {
274+
t.error(err)
275+
})
276+
277+
fastify.ready((err) => {
278+
t.error(err)
279+
280+
t.ok(fastify.hasRequestDecorator('pg'))
213281
})
214282
})

test/query.test.js

+180
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,
@@ -260,3 +261,182 @@ test('When fastify.pg.test namespace is used:', (t) => {
260261

261262
t.end()
262263
})
264+
265+
test('When req.pg root namespace is used:', (t) => {
266+
t.test('Should be able to connect and perform a query', (t) => {
267+
t.plan(4)
268+
const fastify = Fastify()
269+
t.teardown(() => fastify.close())
270+
271+
fastify.register(fastifyPostgres, {
272+
connectionString,
273+
name: 'test'
274+
})
275+
276+
fastify.route({
277+
method: 'GET',
278+
url: '/users',
279+
handler: (req, reply) => {
280+
reply.send('response!')
281+
t.ok(req.pg.test)
282+
req.pg.test.query('SELECT NOW()', (err, result) => {
283+
t.error(err)
284+
t.ok(result.rows)
285+
})
286+
}
287+
})
288+
289+
fastify.inject({
290+
method: 'GET',
291+
url: '/users'
292+
}, (err, res) => {
293+
t.error(err)
294+
})
295+
})
296+
297+
t.test('Should be able to use query util with a callback', (t) => {
298+
t.plan(4)
299+
const fastify = Fastify()
300+
t.teardown(() => fastify.close())
301+
302+
fastify.register(fastifyPostgres, {
303+
connectionString,
304+
name: 'test'
305+
})
306+
307+
fastify.route({
308+
method: 'GET',
309+
url: '/users',
310+
handler: (req, reply) => {
311+
reply.send('response!')
312+
t.ok(req.pg.test)
313+
req.pg.test.query('SELECT NOW()', (err, result) => {
314+
t.error(err)
315+
t.ok(result.rows)
316+
})
317+
}
318+
})
319+
320+
fastify.inject({
321+
method: 'GET',
322+
url: '/users'
323+
}, (err, res) => {
324+
t.error(err)
325+
})
326+
})
327+
328+
t.test('Should be able to use query util with promises', (t) => {
329+
t.plan(3)
330+
331+
const fastify = Fastify()
332+
t.teardown(() => fastify.close())
333+
334+
fastify.register(fastifyPostgres, {
335+
connectionString,
336+
name: 'test'
337+
})
338+
339+
fastify.route({
340+
method: 'GET',
341+
url: '/users',
342+
handler: (req, reply) => {
343+
reply.send('response!')
344+
t.ok(req.pg.test)
345+
346+
req.pg.test
347+
.query('SELECT NOW()')
348+
.then((result) => {
349+
t.ok(result.rows)
350+
})
351+
.catch((err) => {
352+
t.fail(err)
353+
})
354+
}
355+
})
356+
357+
fastify.inject({
358+
method: 'GET',
359+
url: '/users'
360+
}, (err, res) => {
361+
t.error(err)
362+
})
363+
})
364+
365+
t.test('Should be able to use native module', (t) => {
366+
t.plan(3)
367+
368+
const fastify = Fastify()
369+
t.teardown(() => fastify.close())
370+
371+
fastify.register(fastifyPostgres, {
372+
connectionString,
373+
name: 'test',
374+
native: true
375+
})
376+
377+
fastify.route({
378+
method: 'GET',
379+
url: '/users',
380+
handler: (req, reply) => {
381+
reply.send('response!')
382+
t.ok(req.pg.test)
383+
384+
req.pg.test
385+
.query('SELECT NOW()')
386+
.then((result) => {
387+
t.ok(result.rows)
388+
})
389+
.catch((err) => {
390+
t.fail(err)
391+
})
392+
}
393+
})
394+
395+
fastify.inject({
396+
method: 'GET',
397+
url: '/users'
398+
}, (err, res) => {
399+
t.error(err)
400+
})
401+
})
402+
403+
t.test('Should throw when pg fails to perform an operation with promises', (t) => {
404+
t.plan(4)
405+
406+
const fastify = Fastify()
407+
t.teardown(() => fastify.close())
408+
409+
fastify.register(fastifyPostgres, {
410+
connectionString: connectionStringBadDbName,
411+
name: 'test'
412+
})
413+
414+
fastify.route({
415+
method: 'GET',
416+
url: '/users',
417+
handler: (req, reply) => {
418+
reply.send('response!')
419+
t.ok(req.pg.test)
420+
421+
req.pg.test
422+
.query('SELECT NOW()')
423+
.then((result) => {
424+
t.fail(result)
425+
})
426+
.catch((err) => {
427+
t.ok(err)
428+
t.is(err.message, `database "${BAD_DB_NAME}" does not exist`)
429+
})
430+
}
431+
})
432+
433+
fastify.inject({
434+
method: 'GET',
435+
url: '/users'
436+
}, (err, res) => {
437+
t.error(err)
438+
})
439+
})
440+
441+
t.end()
442+
})

0 commit comments

Comments
 (0)