import * as pg from "pg" ; import { Logger , BasicPgClient } from "./types" ; export function withConnection < T > ( log , f ) { return async ( client ) : Promise < T > => { try { try { log ( "Connecting to database..." ) ; await client . connect ( ) ; log ( "... connected to database" ) ; } catch ( e ) { log ( `Error connecting to database: ${ e . message }` ) ; throw e ; } const result = await f ( client ) ; return result ; } catch ( e ) { log ( `Error using connection: ${ e . message }` ) ; throw e ; } finally { try { log ( "Closing connection..." ) ; await client . end ( ) ; log ( "... connection closed" ) ; } catch ( e ) { log ( `Error closing the connection: ${ e . message }` ) ; } } } ; }