( client: PoolClient, callback: (client: PoolClient) => Promise<T> )
| 63 | |
| 64 | /* Quickly becomes root, does the thing, and then reverts back to previous role */ |
| 65 | export const asRoot = async <T>( |
| 66 | client: PoolClient, |
| 67 | callback: (client: PoolClient) => Promise<T> |
| 68 | ): Promise<T> => { |
| 69 | const { |
| 70 | rows: [{ role }], |
| 71 | } = await client.query("select current_setting('role') as role"); |
| 72 | await client.query("reset role"); |
| 73 | try { |
| 74 | return await callback(client); |
| 75 | } finally { |
| 76 | try { |
| 77 | await client.query("select set_config('role', $1, true)", [role]); |
| 78 | } catch (e) { |
| 79 | // Transaction was probably aborted, don't clobber the error |
| 80 | } |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | /******************************************************************************/ |
| 85 |
no outgoing calls
no test coverage detected