From 16385dd012628e0d228756f2d8477d524d781658 Mon Sep 17 00:00:00 2001 From: Amy Fromandi Date: Tue, 10 Sep 2024 10:57:31 -0500 Subject: [PATCH] updated to env auth, using client.query method for existing pg code, fixed elevation and intervals --- Makefile | 3 +-- v2/credentials.ts | 11 ++++++++--- v2/definitions/intervals.ts | 5 +---- v2/elevation.ts | 4 +--- v2/larkin.ts | 17 ++++++++++++++--- v2/mobile/map_query_v2.ts | 10 ++-------- 6 files changed, 27 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index 82fef5c3..43ee54eb 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,6 @@ DOCKER_TAG = latest run: docker build -t macrostrat-api . - docker run --rm -it -p 5550:5550 macrostrat-api + docker run --env-file .env --rm -it -p 5550:5550 macrostrat-api -#add env command to docker run diff --git a/v2/credentials.ts b/v2/credentials.ts index 7748265e..7c4ef6b7 100644 --- a/v2/credentials.ts +++ b/v2/credentials.ts @@ -8,12 +8,10 @@ exports.pg = { }; - - exports.postgresDatabases = { burwell: "macrostrat", geomacro: "geomacro", - elevation: "elevation" + elevation: "elevation", }; // This is the default Redis port @@ -25,3 +23,10 @@ exports.redis = { // Generate a hash by running: node -e "console.log(require('uuid/v4')())" // NOTE: this is outdated and may not be used exports.cacheRefreshKey = "put-hash-here"; + + +/* +exports.macrostratDatabaseUrl = process.env.MACROSTRAT_DATABASE_URL; +exports.elevationDatabaseUrl = process.env.ELEVATION_DATABASE_URL; +exports.cacheRefreshKey = process.env.CACHE_REFRESH_KEY; + */ diff --git a/v2/definitions/intervals.ts b/v2/definitions/intervals.ts index 0a979a22..aee1601c 100644 --- a/v2/definitions/intervals.ts +++ b/v2/definitions/intervals.ts @@ -98,10 +98,7 @@ module.exports = function (req, res, next, cb) { age_bottom AS b_age, interval_type AS int_type, ${color}, - json_agg(json_build_object( - 'timescale_id', timescales.id, - 'name', timescales.timescale - )) AS timescales + json_agg(json_build_object('timescale_id', timescales.id, 'name', timescales.timescale)) AS timescales FROM macrostrat.intervals LEFT JOIN macrostrat.timescales_intervals ON intervals.id = macrostrat.timescales_intervals.interval_id LEFT JOIN macrostrat.timescales ON macrostrat.timescales.id = macrostrat.timescales_intervals.timescale_id diff --git a/v2/elevation.ts b/v2/elevation.ts index bceca0d3..7a6008a1 100644 --- a/v2/elevation.ts +++ b/v2/elevation.ts @@ -6,8 +6,7 @@ module.exports = (req, res, next, cb) => { if (Object.keys(req.query).length < 1) { return larkin.info(req, res, next); } - console.log(req.query) - let param = {} + let param = {} if ((req.query.lat && req.query.lng) || "sample" in req.query) { let lat = req.query.lat || 43.07; @@ -33,7 +32,6 @@ module.exports = (req, res, next, cb) => { sql, param, (error, result) => { - console.log(result) if (error) { if (cb) return cb(error); return larkin.error(req, res, next, "Error fetching elevation data"); diff --git a/v2/larkin.ts b/v2/larkin.ts index ccaf4178..5a9545fb 100644 --- a/v2/larkin.ts +++ b/v2/larkin.ts @@ -87,20 +87,31 @@ const { Client, Pool } = require("pg"); /* Special case for elevation database (temporary) */ connectionDetails.database = 'elevation' } - console.log(connectionDetails) + const pool = new Pool(connectionDetails); pool.connect(function (err, client, done) { if (err) { larkin.log("error", "error connecting - " + err); callback(err); - } else { + } else if (typeof(params) === 'object') { //named uses yesql to modify the params dict and sql named parameters into an array before querying PG. //client.query can only accept numerical indices in sql syntax and an array for parameter values. const preparedQuery = named(sql)(params); console.log("Prepared Query Text:", preparedQuery.text); console.log("Prepared Query Values:", preparedQuery.values); - var query = client.query(preparedQuery.text, preparedQuery.values, function (err, result) { + client.query(preparedQuery.text, preparedQuery.values, function (err, result) { + done(); + if (err) { + larkin.log("error", err); + callback(err); + } else { + callback(null, result); + } + }); + } + else if (params.isArray) { + client.query(sql, params, function (err, result) { done(); if (err) { larkin.log("error", err); diff --git a/v2/mobile/map_query_v2.ts b/v2/mobile/map_query_v2.ts index 1dcc197e..a93b4d63 100644 --- a/v2/mobile/map_query_v2.ts +++ b/v2/mobile/map_query_v2.ts @@ -333,28 +333,22 @@ module.exports = (req, res, next) => { req.query.lng = larkin.normalizeLng(req.query.lng); req.query.z = parseInt(req.query.z || 0); - - req.query.lng = stringify(req.query.lng); - req.query.lat = stringify(req.query.lat); - req.query.z = stringify(req.query.z); console.log(params) async.parallel( { elevation: (cb) => { require("../elevation")(req, null, null, (error, data) => { - console.log(req) - console.log(data) - if (data && data.length) { console.log(data) - console.log(data.length) cb(null, data[0].elevation); } else { console.log("cb is null") cb(null, null); } }); + console.log('ELEVATION IS COMPLETE') + }, lines: (cb) => {