Skip to content

Commit

Permalink
fix connection to database
Browse files Browse the repository at this point in the history
  • Loading branch information
sdobbert authored and sdobbert committed Feb 21, 2024
1 parent 4d05bf0 commit 623b85b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 41 deletions.
25 changes: 17 additions & 8 deletions src/create-contour/add-to-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
};
25 changes: 0 additions & 25 deletions src/create-contour/connect.js

This file was deleted.

24 changes: 16 additions & 8 deletions src/create-contour/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,29 @@ 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,
geom geometry,
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
Expand Down

0 comments on commit 623b85b

Please sign in to comment.