Skip to content

Commit

Permalink
repointed api to use one PG connection string. Removed the rest of an…
Browse files Browse the repository at this point in the history
…y mysql connections
  • Loading branch information
amyfromandi committed Aug 14, 2024
1 parent 5d9d20f commit 3ba9409
Show file tree
Hide file tree
Showing 33 changed files with 84 additions and 58 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ v1/utilities/scripts/credentials.py
.idea
venv

v2/credentials.ts
credentials.ts
!v2/credentials.ts
!credentials.ts
!v1/credentials.ts
v2/utilities/scripts/credentials.py

Expand Down
2 changes: 1 addition & 1 deletion v2/age_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = function (req, res, next) {
? req.query.format
: "json";

larkin.queryPgMaria("macrostrat_two", sql, params, function (error, response) {
larkin.queryPg("burwell", sql, params, function (error, response) {
if (error) {
larkin.error(req, res, next, error);
} else {
Expand Down
21 changes: 21 additions & 0 deletions v2/credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
exports.pgTwo = {
host: process.env.host,
port: process.env.port,
user: process.env.user,
password: process.env.password,
database: process.env.database
};

exports.postgresDatabases = {
burwell: "macrostrat",
geomacro: "geomacro"
};

// This is the default Redis port
// NOTE: Redis is not configured at the moment
exports.redis = {
port: 6379,
};

// Generate a hash by running: node -e "console.log(require('uuid/v4')())"
exports.cacheRefreshKey = "put-hash-here";
2 changes: 1 addition & 1 deletion v2/definitions/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = function (req, res, next) {
query =
"sample" in req.query ? "ma%" : req.query.query.toLowerCase() + "%";

larkin.queryPgMaria("macrostrat_two",
larkin.queryPg("burwell",
"SELECT id::integer, name, type, category FROM macrostrat_temp.autocomplete WHERE name ILIKE :query AND type = ANY(:types) LIMIT :limit",
{ query: query, types: types, limit: limit },
function (error, result) {
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module.exports = function (req, res, next, cb) {
sql += " LIMIT 5";
}

larkin.queryPgMaria("macrostrat_two", sql, params, function (error, result) {
larkin.queryPg("burwell", sql, params, function (error, result) {
if (error) {
if (cb) {
cb(error);
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/drilling_sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module.exports = function (req, res, next, cb) {
console.log(params)


larkin.queryPgMaria("macrostrat_two", sql, params, function (error, result) {
larkin.queryPg"burwell", sql, params, function (error, result) {
if (error) {
if (cb) {
cb(error);
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/econs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = function (req, res, next, cb) {
sql += "\nLIMIT 5";
}

larkin.queryPgMaria("macrostrat_two", sql, params, function (error, data) {
larkin.queryPg("burwell", sql, params, function (error, data) {
if (error) {
if (cb) {
cb(error);
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = function (req, res, next, cb) {
if ("sample" in req.query) {
sql += " LIMIT 5";
}
larkin.queryPgMaria("macrostrat_two", sql, environ, function (error, data) {
larkin.queryPg("burwell", sql, environ, function (error, data) {
if (error) {
if (cb) {
cb(error);
Expand Down
4 changes: 2 additions & 2 deletions v2/definitions/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function (req, res, next, cb) {
//change params from dict to array. add params.push to array
//add schema to sql table in the from and join clauses
//change sql where syntax to = ANY($1)
//update larkin.querypgmaria function
//update larkin.queryPg function

//changing params from array back to dict
let params = {};
Expand Down Expand Up @@ -46,7 +46,7 @@ module.exports = function (req, res, next, cb) {
sql += " LIMIT 5";
}

larkin.queryPgMaria("macrostrat_two",
larkin.queryPg("burwell",
sql,
params,
function (error, data) {
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/intervals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ module.exports = function (req, res, next, cb) {
sql += " LIMIT 5";
}

larkin.queryPgMaria("macrostrat_two",
larkin.queryPg("burwell",
sql,
params, function (error, result) {
if (error) {
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/lithologies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module.exports = function (req, res, next, cb) {
sql += " LIMIT 5";
}

larkin.queryPgMaria("macrostrat_two", sql, params, function (error, data) {
larkin.queryPg("burwell", sql, params, function (error, data) {
if (error) {
if (cb) {
cb(error);
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/lithology_attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = function (req, res, next, cb) {
sql += " LIMIT 5";
}

larkin.queryPgMaria("macrostrat_two", sql, params, function (error, data) {
larkin.queryPg("burwell", sql, params, function (error, data) {
/*TODO Update t_units data type from str to bigint*/
if (error) {
if (cb) {
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/measurement_sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ FROM macrostrat_temp.refs
}


larkin.queryPgMaria("macrostrat_two", sql, params, function (error, data) {
larkin.queryPg("burwell", sql, params, function (error, data) {
if (error) {
if (cb) {
return cb(error);
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/measurements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ FROM macrostrat_temp.measurements
sql += " LIMIT 5";
}

larkin.queryPgMaria("macrostrat_two", sql, params, function (error, data) {
larkin.queryPg("burwell", sql, params, function (error, data) {
if (error) {
if (error) {
return cb(error);
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/minerals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = function (req, res, next, cb) {
sql += " LIMIT 5";
}

larkin.queryPgMaria("macrostrat_two", sql, params, function (error, data) {
larkin.queryPg("burwell", sql, params, function (error, data) {
if (error) {
if (cb) {
cb(error);
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = function (req, res, next, cb) {
sql += "\nGROUP BY projects.id, in_proc.c, obs.co";


larkin.queryPgMaria("macrostrat_two", sql, params, function (error, data) {
larkin.queryPg("burwell", sql, params, function (error, data) {
if (error) {
if (cb) {
return cb(error);
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ FROM macrostrat_temp.refs
sql += " LIMIT 5";
}

larkin.queryPgMaria("macrostrat_two", sql, params, function (error, data) {
larkin.queryPg("burwell", sql, params, function (error, data) {
if (error) {
if (cb) {
return cb(error);
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/strat_name_concepts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = function (req, res, next, cb) {
sql += " LIMIT 5";
}

larkin.queryPgMaria("macrostrat_two", sql, params, function (error, result) {
larkin.queryPg("burwell", sql, params, function (error, result) {
if (error) {
if (cb) {
return cb(error);
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/strat_names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ module.exports = function (req, res, next, cb) {
sql += " LIMIT 5";
}

larkin.queryPgMaria("macrostrat_two", sql, params, function (error, response) {
larkin.queryPg("burwell", sql, params, function (error, response) {
if (error) {
console.log(error);
if (cb) {
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/structures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = function (req, res, next, cb) {
${limit}
`

larkin.queryPgMaria("macrostrat_two",
larkin.queryPg("burwell",
sql,
params,
function (error, data) {
Expand Down
2 changes: 1 addition & 1 deletion v2/definitions/timescales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function (req, res, next, cb) {
sql += " LIMIT 5";
}

larkin.queryPgMaria("macrostrat_two", sql, [], function (error, data) {
larkin.queryPg("burwell", sql, [], function (error, data) {
if (error) {
if (cb) {
return cb(error);
Expand Down
2 changes: 1 addition & 1 deletion v2/editing/section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function (req, res, next) {
if (!req.query.section_id) {
res.json("Need a section_id");
} else {
larkin.queryPgMaria("macrostrat_two",
larkin.queryPg("burwell",
`SELECT ub.id,
u1.strat_name AS below,
u2.strat_name AS above,
Expand Down
2 changes: 1 addition & 1 deletion v2/editing/units_update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function (req, res, next) {
async.each(
req.body.units,
function (each, callback) {
larkin.queryPgMaria("macrostrat_two",
larkin.queryPg("burwell",
"UPDATE macrostrat_temp.units SET position_bottom = :position_bottom WHERE id = :units",
[each.position_bottom, each.unit],
function (error, result) {
Expand Down
2 changes: 1 addition & 1 deletion v2/eodp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = function (req, res, next) {
}


larkin.queryPgMaria("macrostrat_two",sql, params, function (error, response) {
larkin.queryPg("burwell",sql, params, function (error, response) {
if (error) {
larkin.error(req, res, next, error);
} else {
Expand Down
2 changes: 1 addition & 1 deletion v2/fossils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ module.exports = function (req, res, next) {
//TODO there is no pbdb table, so I removed LEFT JOIN pbdb.occ_matrix ON pbdb.coll_matrix.collection_no = pbdb.occ_matrix.collection_no
//I also removed LEFT JOIN pbdb.taxon_lower ON pbdb.occ_matrix.orig_no = pbdb.taxon_lower.orig_no
//removed JOIN pbdb.coll_matrix ON pbdb_matches.collection_no = pbdb.coll_matrix.collection_no
larkin.queryPgMaria("macrostrat_two",
larkin.queryPg("burwell",
"SELECT pbdb_matches.collection_no AS cltn_id, collection_name AS cltn_name, lookup_unit_intervals.t_age, lookup_unit_intervals.b_age, n_occs AS pbdb_occs, COALESCE(GROUP_CONCAT(distinct pbdb.taxon_lower.genus_no), '') AS genus_no, COALESCE(GROUP_CONCAT(distinct pbdb.occ_matrix.taxon_no), '') AS taxon_no, \
pbdb_matches.unit_id, cols.id as col_id, CONCAT(pbdb_matches.ref_id, '|') AS refs " +
(geo ? ", AsWKT(pbdb_matches.coordinate) AS geometry" : "") +
Expand Down
2 changes: 1 addition & 1 deletion v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var api = require("./api"),
larkin = require("./larkin");

// Establish a connection to the database
larkin.connectMySQL();
//larkin.connectMySQL();

// Set up the column and unit cache
larkin.setupCache();
Expand Down
41 changes: 23 additions & 18 deletions v2/larkin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var mysql = require("mysql"),
var //mysql = require("mysql"),
async = require("async"),
_ = require("underscore"),
credentials = require("./credentials"),
Expand All @@ -14,7 +14,7 @@ const { Client, Pool } = require("pg");
(function () {
var larkin = {};

larkin.connectMySQL = function () {
/* larkin.connectMySQL = function () {
// Non-blocking FTW
this.pool = mysql.createPool(credentials.mysql);
Expand All @@ -26,9 +26,10 @@ const { Client, Pool } = require("pg");
);
}
});
};
};*/

larkin.queryPg = function (db, sql, params, callback) {

/*larkin.queryPg = function (db, sql, params, callback) {
const nameMapping = credentials.postgresDatabases ?? {};
const dbName = nameMapping[db] ?? db;
Expand All @@ -41,7 +42,7 @@ const { Client, Pool } = require("pg");
}
if (dbName == "elevation") {
/** Special case for elevation database (temporary) */
//Special case for elevation database (temporary)
connectionString = credentials.elevationDatabase;
}
const pool = new Pool({
Expand All @@ -66,16 +67,17 @@ const { Client, Pool } = require("pg");
}
});
};
*/

//added new method to query from Maria data in the new PG database after migration
larkin.queryPgMaria = function (db, sql, params, callback) {
larkin.queryPg = function (db, sql, params, callback) {
//add console.logs for debug mode in the future
console.log(sql)
console.log(params)
const nameMapping = credentials.postgresDatabases ?? {};
const dbName = nameMapping[db] ?? db;

let { connectionString, ...otherConnectionDetails } = credentials.pgMaria;
let { connectionString, ...otherConnectionDetails } = credentials.pg;

if (dbName == "geomacro") {
console.warn(
Expand Down Expand Up @@ -135,6 +137,7 @@ const { Client, Pool } = require("pg");
return [sql, newParams];
};

/*
larkin.query = function (sql, params, callback) {
//console.warn(`Deprecated MySQL query:\n${sql}`);
if (sql.indexOf(":") > -1 && Object.keys(params).length > 0) {
Expand Down Expand Up @@ -164,7 +167,7 @@ const { Client, Pool } = require("pg");
);
}.bind(this),
);
};
}; */

larkin.sendImage = function (req, res, next, data, isCached) {
// console.log(data)
Expand Down Expand Up @@ -557,23 +560,25 @@ const { Client, Pool } = require("pg");
),
);



// Macrostrat refs
if (key === "refs" || key === "ref_id") {
larkin.query(
"SELECT refs.id AS ref_id, pub_year, author, ref, doi, url, COUNT(DISTINCT units_sections.unit_id) AS t_units FROM refs LEFT JOIN col_refs ON col_refs.ref_id = refs.id LEFT JOIN units_sections ON units_sections.col_id = col_refs.col_id WHERE refs.id IN (:ref_id) GROUP BY refs.id",
larkin.queryPg("burwell",
"SELECT refs.id AS ref_id, pub_year, author, ref, doi, url, COUNT(DISTINCT units_sections.unit_id) AS t_units FROM macrostrat_temp.refs LEFT JOIN macrostrat_temp.col_refs ON col_refs.ref_id = refs.id LEFT JOIN macrostrat_temp.units_sections ON units_sections.col_id = col_refs.col_id WHERE refs.id = ANY(:ref_id) GROUP BY refs.id",
{ ref_id: ref_ids },
function (error, data) {
var refs = {};
if (!data) {
if (!data.rows) {
return callback(null);
}
for (var i = 0; i < data.length; i++) {
refs[data[i]["ref_id"]] =
larkin.normalizeRefField(data[i].author) +
larkin.normalizeRefField(data[i].ref) +
larkin.normalizeRefField(data[i].pub_year) +
larkin.normalizeRefField(data[i].doi) +
larkin.normalizeRefField(data[i].url);
for (var i = 0; i < data.rows.length; i++) {
refs[data.rows[i]["ref_id"]] =
larkin.normalizeRefField(data.rows[i].author) +
larkin.normalizeRefField(data.rows[i].ref) +
larkin.normalizeRefField(data.rows[i].pub_year) +
larkin.normalizeRefField(data.rows[i].doi) +
larkin.normalizeRefField(data.rows[i].url);
}
callback(refs);
},
Expand Down
2 changes: 1 addition & 1 deletion v2/measurements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ module.exports = function (req, res, next) {
`;
// console.log(sql)

larkin.queryPgMaria("macrostrat_two", sql, params, function (error, response) {
larkin.queryPg("burwell", sql, params, function (error, response) {
if (error) {
larkin.error(req, res, next, error);
} else {
Expand Down
2 changes: 1 addition & 1 deletion v2/mobile/fossil_collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function (req, res, next) {
if ("sample" in req.query) {
req.query.unit_id = 15176;
}
larkin.queryPgMaria("macrostrat_two",
larkin.queryPg("burwell",
"SELECT DISTINCT collection_no AS cltn_id, collection_name AS cltn_name FROM macrostrat_temp.pbdb_matches WHERE unit_id = ? AND occs > 0 and release_date<NOW()",
[req.query.unit_id],
function (error, result) {
Expand Down
Loading

0 comments on commit 3ba9409

Please sign in to comment.