From 8b6f648c08bc4347366d6e19913fd4364dfcab2e Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 12 Feb 2022 16:13:51 -0500 Subject: [PATCH] fix: pg client logic --- src/database/pg-wrapper.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/database/pg-wrapper.js b/src/database/pg-wrapper.js index eed98ed..66897b8 100644 --- a/src/database/pg-wrapper.js +++ b/src/database/pg-wrapper.js @@ -12,7 +12,7 @@ const tableWhiteList = [...createFullName("latest", tableNamesWhitelist), ...cre /* Postgres database connection code */ -const databaseURL = process.env.DIGITALOCEAN_POSTGRES || getSecret("digitalocean-postgres") +const databaseURL = process.env.DIGITALOCEAN_POSTGRES || getSecret("digitalocean-postgres") // process.env.DATABASE_URL || getSecret("heroku-postgres") const readWritePool = new Pool({ connectionString: databaseURL, @@ -32,17 +32,18 @@ const readOnlyPool = new Pool({ // Helpers const runPgCommand = async ({ command, pool }) => { console.log(command) - try{ - const client = await pool.connect(); - const result = await client.query(command); - const results = { 'results': (result) ? result.rows : null }; - }catch(error){ + const client = await pool.connect(); + let result + try { + let response = await client.query(command); + result = { 'results': (response) ? response.rows : null }; + } catch (error) { console.log(error) - }finally{ + } finally { client.release(); } // console.log(results) - return results + return result } // Initialize @@ -149,12 +150,13 @@ export async function pgInsert({ datum, schema, tableName }) { JSON.stringify(datum.qualityindicators || []), JSON.stringify(datum.extra || []) ] - try{ - const client = await readWritePool.connect(); - const result = await client.query(text, values); - }catch(error){ + const client = await readWritePool.connect(); + let result + try { + result = await client.query(text, values); + } catch (error) { console.log(error) - }finally{ + } finally { client.release(); } // console.log(result)