|
3 | 3 | const defaultPg = require('pg') |
4 | 4 | const fp = require('fastify-plugin') |
5 | 5 |
|
| 6 | +const addHandler = require('./lib/add-handler.js') |
| 7 | + |
| 8 | +const transactionFailedSymbol = Symbol('transactionFailed') |
| 9 | + |
6 | 10 | function transactionUtil (pool, fn, cb) { |
7 | 11 | pool.connect((err, client, done) => { |
8 | 12 | if (err) return cb(err) |
@@ -52,6 +56,18 @@ function transact (fn, cb) { |
52 | 56 | }) |
53 | 57 | } |
54 | 58 |
|
| 59 | +function extractRequestClient (req, transact) { |
| 60 | + if (typeof transact !== 'string') { |
| 61 | + return req.pg |
| 62 | + } |
| 63 | + |
| 64 | + const requestClient = req.pg[transact] |
| 65 | + if (!requestClient) { |
| 66 | + throw new Error(`request client '${transact}' does not exist`) |
| 67 | + } |
| 68 | + return requestClient |
| 69 | +} |
| 70 | + |
55 | 71 | function fastifyPostgres (fastify, options, next) { |
56 | 72 | let pg = defaultPg |
57 | 73 |
|
@@ -102,6 +118,70 @@ function fastifyPostgres (fastify, options, next) { |
102 | 118 | } |
103 | 119 | } |
104 | 120 |
|
| 121 | + if (!fastify.hasRequestDecorator('pg')) { |
| 122 | + fastify.decorateRequest('pg', null) |
| 123 | + } |
| 124 | + |
| 125 | + fastify.addHook('onRoute', routeOptions => { |
| 126 | + const transact = routeOptions && routeOptions.pg && routeOptions.pg.transact |
| 127 | + |
| 128 | + if (!transact) { |
| 129 | + return |
| 130 | + } |
| 131 | + if (typeof transact === 'string' && transact !== name) { |
| 132 | + return |
| 133 | + } |
| 134 | + if (name && transact === true) { |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + const preHandler = async (req, reply) => { |
| 139 | + const client = await pool.connect() |
| 140 | + |
| 141 | + if (name) { |
| 142 | + if (!req.pg) { |
| 143 | + req.pg = {} |
| 144 | + } |
| 145 | + |
| 146 | + if (client[name]) { |
| 147 | + throw new Error(`pg client '${name}' is a reserved keyword`) |
| 148 | + } else if (req.pg[name]) { |
| 149 | + throw new Error(`request client '${name}' has already been registered`) |
| 150 | + } |
| 151 | + |
| 152 | + req.pg[name] = client |
| 153 | + } else { |
| 154 | + if (req.pg) { |
| 155 | + throw new Error('request client has already been registered') |
| 156 | + } else { |
| 157 | + req.pg = client |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + client.query('BEGIN') |
| 162 | + } |
| 163 | + |
| 164 | + const onError = (req, reply, error, done) => { |
| 165 | + req[transactionFailedSymbol] = true |
| 166 | + extractRequestClient(req, transact).query('ROLLBACK', done) |
| 167 | + } |
| 168 | + |
| 169 | + const onSend = async (req) => { |
| 170 | + const requestClient = extractRequestClient(req, transact) |
| 171 | + try { |
| 172 | + if (!req[transactionFailedSymbol]) { |
| 173 | + await requestClient.query('COMMIT') |
| 174 | + } |
| 175 | + } finally { |
| 176 | + requestClient.release() |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + routeOptions.preHandler = addHandler(routeOptions.preHandler, preHandler) |
| 181 | + routeOptions.onError = addHandler(routeOptions.onError, onError) |
| 182 | + routeOptions.onSend = addHandler(routeOptions.onSend, onSend) |
| 183 | + }) |
| 184 | + |
105 | 185 | next() |
106 | 186 | } |
107 | 187 |
|
|
0 commit comments