diff --git a/.gitignore b/.gitignore index b87bd99f..267b263d 100755 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/v2/age_model.ts b/v2/age_model.ts index 4a824eed..4b5a138c 100644 --- a/v2/age_model.ts +++ b/v2/age_model.ts @@ -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 { diff --git a/v2/credentials.ts b/v2/credentials.ts new file mode 100644 index 00000000..fe97fd81 --- /dev/null +++ b/v2/credentials.ts @@ -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"; diff --git a/v2/definitions/autocomplete.ts b/v2/definitions/autocomplete.ts index ee74006f..cd9391c6 100644 --- a/v2/definitions/autocomplete.ts +++ b/v2/definitions/autocomplete.ts @@ -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) { diff --git a/v2/definitions/columns.ts b/v2/definitions/columns.ts index 733086f3..06f11688 100644 --- a/v2/definitions/columns.ts +++ b/v2/definitions/columns.ts @@ -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); diff --git a/v2/definitions/drilling_sites.ts b/v2/definitions/drilling_sites.ts index f4bc8544..101d8098 100644 --- a/v2/definitions/drilling_sites.ts +++ b/v2/definitions/drilling_sites.ts @@ -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); diff --git a/v2/definitions/econs.ts b/v2/definitions/econs.ts index 8a670e7b..761d8428 100644 --- a/v2/definitions/econs.ts +++ b/v2/definitions/econs.ts @@ -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); diff --git a/v2/definitions/environments.ts b/v2/definitions/environments.ts index 03efcac3..4c6d3ac1 100644 --- a/v2/definitions/environments.ts +++ b/v2/definitions/environments.ts @@ -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); diff --git a/v2/definitions/groups.ts b/v2/definitions/groups.ts index f798b7bf..1fd6695e 100644 --- a/v2/definitions/groups.ts +++ b/v2/definitions/groups.ts @@ -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 = {}; @@ -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) { diff --git a/v2/definitions/intervals.ts b/v2/definitions/intervals.ts index 687d8683..4ba6f776 100644 --- a/v2/definitions/intervals.ts +++ b/v2/definitions/intervals.ts @@ -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) { diff --git a/v2/definitions/lithologies.ts b/v2/definitions/lithologies.ts index d2a64ac8..61f3cf9d 100644 --- a/v2/definitions/lithologies.ts +++ b/v2/definitions/lithologies.ts @@ -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); diff --git a/v2/definitions/lithology_attributes.ts b/v2/definitions/lithology_attributes.ts index 35035c64..cb23da16 100644 --- a/v2/definitions/lithology_attributes.ts +++ b/v2/definitions/lithology_attributes.ts @@ -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) { diff --git a/v2/definitions/measurement_sources.ts b/v2/definitions/measurement_sources.ts index e153e571..4a85f41c 100644 --- a/v2/definitions/measurement_sources.ts +++ b/v2/definitions/measurement_sources.ts @@ -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); diff --git a/v2/definitions/measurements.ts b/v2/definitions/measurements.ts index 665f7505..2065bc0b 100644 --- a/v2/definitions/measurements.ts +++ b/v2/definitions/measurements.ts @@ -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); diff --git a/v2/definitions/minerals.ts b/v2/definitions/minerals.ts index 2a729d85..5f71bd31 100644 --- a/v2/definitions/minerals.ts +++ b/v2/definitions/minerals.ts @@ -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); diff --git a/v2/definitions/projects.ts b/v2/definitions/projects.ts index e7fb74c1..4bac4d9d 100644 --- a/v2/definitions/projects.ts +++ b/v2/definitions/projects.ts @@ -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); diff --git a/v2/definitions/refs.ts b/v2/definitions/refs.ts index 1a3f1f7e..dd5fb4d5 100644 --- a/v2/definitions/refs.ts +++ b/v2/definitions/refs.ts @@ -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); diff --git a/v2/definitions/strat_name_concepts.ts b/v2/definitions/strat_name_concepts.ts index bce0ca18..8216e083 100644 --- a/v2/definitions/strat_name_concepts.ts +++ b/v2/definitions/strat_name_concepts.ts @@ -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); diff --git a/v2/definitions/strat_names.ts b/v2/definitions/strat_names.ts index adabe021..7158c7b7 100644 --- a/v2/definitions/strat_names.ts +++ b/v2/definitions/strat_names.ts @@ -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) { diff --git a/v2/definitions/structures.ts b/v2/definitions/structures.ts index 731e5311..757f79b6 100644 --- a/v2/definitions/structures.ts +++ b/v2/definitions/structures.ts @@ -48,7 +48,7 @@ module.exports = function (req, res, next, cb) { ${limit} ` - larkin.queryPgMaria("macrostrat_two", + larkin.queryPg("burwell", sql, params, function (error, data) { diff --git a/v2/definitions/timescales.ts b/v2/definitions/timescales.ts index 74f6d144..d079e6fc 100644 --- a/v2/definitions/timescales.ts +++ b/v2/definitions/timescales.ts @@ -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); diff --git a/v2/editing/section.ts b/v2/editing/section.ts index 6c21ebf9..213d3652 100644 --- a/v2/editing/section.ts +++ b/v2/editing/section.ts @@ -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, diff --git a/v2/editing/units_update.ts b/v2/editing/units_update.ts index 75b48a8a..f6158bc5 100644 --- a/v2/editing/units_update.ts +++ b/v2/editing/units_update.ts @@ -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) { diff --git a/v2/eodp.ts b/v2/eodp.ts index a78109f0..fc731234 100644 --- a/v2/eodp.ts +++ b/v2/eodp.ts @@ -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 { diff --git a/v2/fossils.ts b/v2/fossils.ts index 5c231406..0421b454 100644 --- a/v2/fossils.ts +++ b/v2/fossils.ts @@ -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" : "") + diff --git a/v2/index.ts b/v2/index.ts index c032eac4..a5f0d67d 100644 --- a/v2/index.ts +++ b/v2/index.ts @@ -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(); diff --git a/v2/larkin.ts b/v2/larkin.ts index 8ed6505e..8f3b8bbb 100644 --- a/v2/larkin.ts +++ b/v2/larkin.ts @@ -1,4 +1,4 @@ -var mysql = require("mysql"), +var //mysql = require("mysql"), async = require("async"), _ = require("underscore"), credentials = require("./credentials"), @@ -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); @@ -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; @@ -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({ @@ -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( @@ -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) { @@ -164,7 +167,7 @@ const { Client, Pool } = require("pg"); ); }.bind(this), ); - }; + }; */ larkin.sendImage = function (req, res, next, data, isCached) { // console.log(data) @@ -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); }, diff --git a/v2/measurements.ts b/v2/measurements.ts index 39505c81..66d8e118 100644 --- a/v2/measurements.ts +++ b/v2/measurements.ts @@ -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 { diff --git a/v2/mobile/fossil_collections.ts b/v2/mobile/fossil_collections.ts index 16f6962b..fcaa8b0e 100644 --- a/v2/mobile/fossil_collections.ts +++ b/v2/mobile/fossil_collections.ts @@ -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