From 623b85b59291e1366ea96c0890533abd766e3046 Mon Sep 17 00:00:00 2001 From: sdobbert Date: Wed, 21 Feb 2024 16:19:00 +0100 Subject: [PATCH] fix connection to database --- src/create-contour/add-to-table.js | 25 +++++++++++++++++-------- src/create-contour/connect.js | 25 ------------------------- src/create-contour/index.js | 24 ++++++++++++++++-------- 3 files changed, 33 insertions(+), 41 deletions(-) delete mode 100644 src/create-contour/connect.js diff --git a/src/create-contour/add-to-table.js b/src/create-contour/add-to-table.js index acc30fc..3076293 100644 --- a/src/create-contour/add-to-table.js +++ b/src/create-contour/add-to-table.js @@ -21,12 +21,21 @@ export const addData = async ( } // Add to table - const client = await getClient(); - // TODO process.argv can potentially be removed - const timestamp = process.argv[2] ?? datasetTimestamp; - const geom = process.argv[2] ?? contourLine.geometry; - const temp = process.argv[2] ?? contourLine.properties.TEMP; - let insertRow = await client.query(`INSERT INTO ${region}_contourLines(timestamp, geom, temp) VALUES($1);`, [timestamp, geom, temp]); - logger.info(`Inserted ${insertRow.rowCount} row`); - await client.end(); + let client; + try { + client = await getClient(); + // TODO process.argv can potentially be removed + const timestamp = process.argv[2] ?? datasetTimestamp; + const geom = process.argv[2] ?? contourLine.geometry; + const temp = process.argv[2] ?? contourLine.properties.TEMP; + let insertRow = await client.query(`INSERT INTO ${region}_contourLines(timestamp, geom, temp) VALUES($1);`, [timestamp, geom, temp]); + logger.info(`Inserted ${insertRow.rowCount} row`); + } catch (e) { + logger.error(e); + throw 'SQL execution aborted: ' + e; + } finally { + if (client) { + await client.end(); + } + } }; diff --git a/src/create-contour/connect.js b/src/create-contour/connect.js deleted file mode 100644 index 2133dc3..0000000 --- a/src/create-contour/connect.js +++ /dev/null @@ -1,25 +0,0 @@ -const pgHost = process.env.POSTGRES_HOST -const pgPort = process.env.POSTGRES_PORT -const pgSchema = process.env.POSTGRES_SCHEMA -const pgDatabase = process.env.POSTGRES_DB -const pgUser = process.env.POSTGRES_USER -const pgPassword = process.env.POSTGRES_PASSWORD - -const { Client } = require('pg'); -require('dotenv').config(); - -(async () => { - const client = new Client({ - host: pgHost, - port: pgPort, - schema: pgSchema, - user: pgUser, - password: pgPassword, - database: pgDatabase, - ssl: true, - }); - await client.connect(); - const res = await client.query('SELECT $1::text as connected', ['Connection to postgres successful!']); - console.log(res.rows[0].connected); - await client.end(); -})(); diff --git a/src/create-contour/index.js b/src/create-contour/index.js index e06a207..224d261 100644 --- a/src/create-contour/index.js +++ b/src/create-contour/index.js @@ -44,9 +44,11 @@ const contourLinesWorker = async (workerJob, inputs) => { // Create table // TODO check if this can be moved to seperate file - (async () => { - const client = await getClient(); - let createTableQuery = ` + let client; + try { + (async () => { + client = await getClient(); + let createTableQuery = ` CREATE TABLE IF NOT EXISTS ${region}_contourLines( id BIGSERIAL PRIMARY KEY NOT NULL , timestamp timestamp without timezone, @@ -54,11 +56,17 @@ const contourLinesWorker = async (workerJob, inputs) => { temp number, ); `; - const res = await client.query(createTableQuery); - logger.info(`Created table.`); - logger.info(res.rows[0].connected); - await client.end(); - })(); + await client.query(createTableQuery); + logger.info(`Created table.`); + })(); + } catch (e) { + logger.error(e); + throw 'SQL execution aborted: ' + e; + } finally { + if (client) { + await client.end(); + } + } // Add rows to table // array aus multiLines als geoJSON