-
-
Notifications
You must be signed in to change notification settings - Fork 36
feature(automatic-transactions): Creates a decorator to make transactions easier for the developer #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature(automatic-transactions): Creates a decorator to make transactions easier for the developer #76
Changes from 5 commits
3de300e
90febc6
95623b9
3d5c5ca
b9a417f
91fc85d
9513db6
3fea7fe
f4b730d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| module.exports = (existingHandler, newHandler) => { | ||
jsumners marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (Array.isArray(existingHandler)) { | ||
| return [ | ||
| ...existingHandler, | ||
| newHandler | ||
| ] | ||
| } else if (typeof existingHandler === 'function') { | ||
| return [existingHandler, newHandler] | ||
| } else { | ||
| return [newHandler] | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,10 @@ | |
| const defaultPg = require('pg') | ||
| const fp = require('fastify-plugin') | ||
|
|
||
| const addHandler = require('./add-handler.js') | ||
|
|
||
| const transactionFailedSymbol = Symbol('transactionFailed') | ||
|
|
||
| function transactionUtil (pool, fn, cb) { | ||
| pool.connect((err, client, done) => { | ||
| if (err) return cb(err) | ||
|
|
@@ -52,6 +56,18 @@ function transact (fn, cb) { | |
| }) | ||
| } | ||
|
|
||
| function extractRequestClient (req, transact) { | ||
| if (transact.length) { | ||
|
||
| const requestClient = req.pg[transact] | ||
| if (!requestClient) { | ||
| throw new Error(`request client '${transact}' does not exist`) | ||
| } | ||
| return req.pg[transact] | ||
| } | ||
|
|
||
| return req.pg | ||
| } | ||
|
|
||
| function fastifyPostgres (fastify, options, next) { | ||
| let pg = defaultPg | ||
|
|
||
|
|
@@ -102,6 +118,70 @@ function fastifyPostgres (fastify, options, next) { | |
| } | ||
| } | ||
|
|
||
| if (!fastify.hasRequestDecorator('pg')) { | ||
| fastify.decorateRequest('pg', null) | ||
| } | ||
|
|
||
| fastify.addHook('onRoute', routeOptions => { | ||
| const transact = routeOptions && routeOptions.pg && routeOptions.pg.transact | ||
|
|
||
| if (!transact) { | ||
| return | ||
| } | ||
| if (typeof transact === 'string' && transact !== name) { | ||
| return | ||
| } | ||
| if (name && transact === true) { | ||
| return | ||
| } | ||
|
|
||
| const preHandler = async (req, reply) => { | ||
| const client = await pool.connect() | ||
|
|
||
| if (name) { | ||
| if (!req.pg) { | ||
| req.pg = {} | ||
mcollina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (client[name]) { | ||
| throw new Error(`pg client '${name}' is a reserved keyword`) | ||
| } else if (req.pg[name]) { | ||
| throw new Error(`request client '${name}' has already been registered`) | ||
| } | ||
|
|
||
| req.pg[name] = client | ||
| } else { | ||
| if (req.pg) { | ||
| throw new Error('request client has already been registered') | ||
| } else { | ||
| req.pg = client | ||
| } | ||
| } | ||
|
|
||
| extractRequestClient(req, transact).query('BEGIN') | ||
|
||
| } | ||
|
|
||
| const onError = (req, reply, error, done) => { | ||
| req[transactionFailedSymbol] = true | ||
| extractRequestClient(req, transact).query('ROLLBACK', done) | ||
| } | ||
|
|
||
| const onSend = async (req) => { | ||
| const requestClient = extractRequestClient(req, transact) | ||
| try { | ||
| if (!req[transactionFailedSymbol]) { | ||
| await requestClient.query('COMMIT') | ||
| } | ||
| } finally { | ||
| requestClient.release() | ||
| } | ||
| } | ||
|
|
||
| routeOptions.preHandler = addHandler(routeOptions.preHandler, preHandler) | ||
| routeOptions.onError = addHandler(routeOptions.onError, onError) | ||
| routeOptions.onSend = addHandler(routeOptions.onSend, onSend) | ||
| }) | ||
|
|
||
| next() | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| "main": "index.js", | ||
| "types": "index.d.ts", | ||
| "scripts": { | ||
| "testonly": "tap -J test/*.test.js && npm run test:typescript", | ||
|
||
| "test": "standard && tap -J test/*.test.js && npm run test:typescript", | ||
| "test:typescript": "tsd", | ||
| "test:report": "standard && tap -J --coverage-report=html test/*.test.js", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| 'use strict' | ||
|
|
||
| const t = require('tap') | ||
| const test = t.test | ||
| const addHandler = require('../add-handler') | ||
|
|
||
| test('addHandler - ', t => { | ||
| test('when existing handler is not defined', t => { | ||
| t.plan(1) | ||
|
|
||
| const handlers = addHandler( | ||
| undefined, | ||
| 'test' | ||
| ) | ||
|
|
||
| t.same(handlers, ['test']) | ||
| }) | ||
| test('when existing handler is a array', t => { | ||
| t.plan(1) | ||
|
|
||
| const handlers = addHandler( | ||
| ['test'], | ||
| 'again' | ||
| ) | ||
|
|
||
| t.same(handlers, ['test', 'again']) | ||
| }) | ||
| test('when existing handler is a function', t => { | ||
| t.plan(2) | ||
|
|
||
| const stub = () => 'test' | ||
|
|
||
| const handlers = addHandler( | ||
| stub, | ||
| 'again' | ||
| ) | ||
|
|
||
| t.same(handlers[0](), 'test') | ||
| t.same(handlers[1], 'again') | ||
| }) | ||
|
|
||
| t.end() | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use strict is missing.
Maybe it's better to move this file inside
lib/