import { Client } from "pg" ; import debug from "debug" ; const L = debug ( "hub:pg:query_params" ) ; interface Opts { client : Client ; query : string ; params : string [ ] ; pg_params : { [ key ] : string ; } ; cb : ( err ? , result ? ) => void ; } export async function do_query_with_pg_params ( opts ) { const { client , query , params , pg_params , cb } = opts ; try { await client . query ( "BEGIN" ) ; for ( const [ k , v ] of Object . entries ( pg_params ) ) { const q = `SET LOCAL ${ k } TO ${ v }` ; L ( `Setting query param: ${ k }=${ v }` ) ; await client . query ( q ) ; } const res = await client . query ( query , params ) ; await client . query ( "COMMIT" ) ; cb ( undefined , res ) ; } catch ( err ) { L ( `ROLLBACK -- err=${ err }` ) ; await client . query ( "ROLLBACK" ) ; cb ( err ) ; } }