Skip to content

Commit f978966

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 f978966

File tree

4 files changed

+266
-1
lines changed

4 files changed

+266
-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

+189
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,6 +135,10 @@ test('When fastify.pg root namespace is used:', (t) => {
134135
t.end()
135136
})
136137

138+
139+
140+
141+
137142
test('When fastify.pg.test namespace is used:', (t) => {
138143
t.test('Should be able to connect and perform a query', (t) => {
139144
t.plan(4)
@@ -260,3 +265,187 @@ test('When fastify.pg.test namespace is used:', (t) => {
260265

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

0 commit comments

Comments
 (0)