(to)
| 6 | const { Client } = require('pg'); |
| 7 | |
| 8 | function providePostgres(to) { |
| 9 | return async function(...args) { |
| 10 | const client = new Client(); |
| 11 | |
| 12 | await client.connect(); |
| 13 | await client.query('begin'); |
| 14 | if (typeof to.middleware === 'function') { |
| 15 | to.middleware([ |
| 16 | ...to.middleware(), |
| 17 | function addpg() { |
| 18 | return next => { |
| 19 | return req => { |
| 20 | req.getPostgresClient = async () => client; |
| 21 | return next(req); |
| 22 | }; |
| 23 | }; |
| 24 | } |
| 25 | ]); |
| 26 | } |
| 27 | |
| 28 | orm.setConnection(async () => { |
| 29 | return { |
| 30 | connection: client, |
| 31 | release() {} |
| 32 | }; |
| 33 | }); |
| 34 | |
| 35 | try { |
| 36 | await to(...args); |
| 37 | } finally { |
| 38 | await client.query('rollback'); |
| 39 | await client.end(); |
| 40 | orm.setConnection(() => { |
| 41 | throw new Error('no connection available'); |
| 42 | }); |
| 43 | } |
| 44 | }; |
| 45 | } |
no outgoing calls
no test coverage detected