Refactor DB interface to use setSurvey()

This commit is contained in:
D. Berge
2020-08-16 10:46:54 +02:00
parent 170312efd6
commit 608fd9d3cd
10 changed files with 49 additions and 38 deletions

View File

@@ -1,7 +1,7 @@
const { pool } = require('./connection');
const { setSurvey } = require('./connection');
async function get (projectId, path, opts = {}) {
await pool.query("CALL set_survey($1);", [projectId]);
const client = await setSurvey(projectId);
const text = `
SELECT data
@@ -10,7 +10,8 @@ async function get (projectId, path, opts = {}) {
WHERE f.path LIKE '%.yaml';
`;
const res = await pool.query(text);
const res = await client.query(text);
client.release();
const config = res.rows.length == 1
? res.rows[0].data

View File

@@ -1,7 +1,7 @@
const { pool } = require('../connection');
const { setSurvey } = require('../connection');
async function list (projectId, opts = {}) {
await pool.query("CALL set_survey($1);", [projectId]);
const client = await setSurvey(projectId);
const sortFields = [ "sequence", "shot_number", "ts0", "ts1", "label", "remarks" ];
const sortKey = opts.sortBy && sortFields.includes(opts.sortBy) && opts.sortBy || "ts0";
@@ -27,7 +27,8 @@ async function list (projectId, opts = {}) {
ORDER BY ${sortKey} ${sortDir};
`;
const res = await pool.query(text, filter[1]);
const res = await client.query(text, filter[1]);
client.release();
return res.rows;
}

View File

@@ -1,14 +1,15 @@
const { pool } = require('../../connection');
const { setSurvey } = require('../../connection');
async function bbox (projectId) {
await pool.query("CALL set_survey($1);", [projectId]);
const client = await setSurvey(projectId);
const srid = "(SELECT ST_SRID(geometry) FROM preplot_lines LIMIT 1)"
const text = `SELECT ST_AsGeoJSON(ST_Transform(ST_SetSRID(ST_Extent(geometry), ${srid}), 4326)) AS bbox FROM preplot_lines;`
const res = await pool.query(text);
const res = await client.query(text);
client.release();
if (res.rows && res.rows.length) {
return res.rows[0].bbox;
} else {

View File

@@ -1,10 +1,10 @@
const { pool } = require('../../connection');
const { setSurvey } = require('../../connection');
async function lines (projectId) {
await pool.query("CALL set_survey($1);", [projectId]);
const client = await setSurvey(projectId);
const text = `
SELECT ST_AsGeoJSON(s.*) geojson
@@ -15,7 +15,8 @@ async function lines (projectId) {
) s;
`;
const res = await pool.query(text);
const res = await client.query(text);
client.release();
if (res.rows && res.rows.length) {
return res.rows.map(r => JSON.parse(r.geojson));
@@ -26,7 +27,7 @@ async function lines (projectId) {
async function points (projectId, options = {}) {
await pool.query("CALL set_survey($1);", [projectId]);
const client = await setSurvey(projectId);
const values = [
"bbox" in options,
@@ -49,7 +50,8 @@ async function points (projectId, options = {}) {
) t
`;
const res = await pool.query(text, values);
const res = await client.query(text, values);
client.release();
if (res.rows && res.rows.length) {
return res.rows.map(r => JSON.parse(r.geojson));

View File

@@ -1,9 +1,9 @@
const { pool } = require('../../connection');
const { setSurvey } = require('../../connection');
async function lines (projectId, options = {}) {
await pool.query("CALL set_survey($1);", [projectId]);
const client = await setSurvey(projectId);
const values = [
"bbox" in options,
@@ -34,7 +34,8 @@ async function lines (projectId, options = {}) {
) f;
`;
const res = await pool.query(text, values);
const res = await client.query(text, values);
client.release();
if (res.rows && res.rows.length) {
return res.rows[0].geojson;
@@ -45,7 +46,7 @@ async function lines (projectId, options = {}) {
async function points (projectId, options = {}) {
await pool.query("CALL set_survey($1);", [projectId]);
const client = await setSurvey(projectId);
const values = [
"bbox" in options,
@@ -76,7 +77,8 @@ async function points (projectId, options = {}) {
) f;
`;
const res = await pool.query(text, values);
const res = await client.query(text, values);
client.release();
if (res.rows && res.rows.length) {
return res.rows[0].geojson;

View File

@@ -1,10 +1,10 @@
const { pool } = require('../../connection');
const { setSurvey } = require('../../connection');
async function lines (projectId, options = {}) {
await pool.query("CALL set_survey($1);", [projectId]);
const client = await setSurvey(projectId);
const text = `
SELECT ST_AsGeoJSON(s.*) geojson
@@ -16,7 +16,8 @@ async function lines (projectId, options = {}) {
`;
const res = await pool.query(text);
const res = await client.query(text);
client.release();
if (res.rows && res.rows.length) {
return res.rows.map(r => JSON.parse(r.geojson));
@@ -27,7 +28,7 @@ async function lines (projectId, options = {}) {
async function points (projectId, options = {}) {
await pool.query("CALL set_survey($1);", [projectId]);
const client = await setSurvey(projectId);
const values = [
"bbox" in options,
@@ -52,7 +53,8 @@ async function points (projectId, options = {}) {
) t
`;
const res = await pool.query(text, values);
const res = await client.query(text, values);
client.release();
if (res.rows && res.rows.length) {
return res.rows.map(r => [

View File

@@ -1,7 +1,7 @@
const { pool } = require('../connection');
const { setSurvey } = require('../connection');
async function list (projectId, opts = {}) {
await pool.query("CALL set_survey($1);", [projectId]);
const client = await setSurvey(projectId);
const text = `
SELECT *
@@ -9,7 +9,8 @@ async function list (projectId, opts = {}) {
ORDER BY name;
`;
const res = await pool.query(text);
const res = await client.query(text);
client.release();
return res.rows;
}

View File

@@ -1,7 +1,7 @@
const { pool } = require('./connection');
const { setSurvey } = require('./connection');
async function list (projectId, opts = {}) {
await pool.query("CALL set_survey($1);", [projectId]);
const client = await setSurvey(projectId);
const sortFields = [ "line", "length", "azimuth", "fsp", "lsp", "num_points", "incr", "remarks" ];
const sortKey = opts.sortBy && sortFields.includes(opts.sortBy) && opts.sortBy || "line";
@@ -37,7 +37,8 @@ async function list (projectId, opts = {}) {
LIMIT $2;
`;
const res = await pool.query(text, [offset, limit]);
const res = await client.query(text, [offset, limit]);
client.release();
return res.rows;
}

View File

@@ -1,4 +1,4 @@
const { pool } = require('./connection');
const { setSurvey, pool } = require('./connection');
async function list () {
const res = await pool.query("SELECT * FROM public.projects;");
@@ -6,7 +6,7 @@ async function list () {
}
async function summary (projectId, opts = {}) {
await pool.query("CALL set_survey($1);", [projectId]);
const client = await setSurvey(projectId);
const sortFields = [
@@ -32,7 +32,8 @@ SELECT * FROM projects,
WHERE current_setting('search_path') LIKE projects.schema||'%';
`;
const res = await pool.query(text);
const res = await client.query(text);
client.release();
return res.rows[0];
}

View File

@@ -1,7 +1,7 @@
const { pool } = require('./connection');
const { setSurvey } = require('./connection');
async function list (projectId, opts = {}) {
await pool.query("CALL set_survey($1);", [projectId]);
const client = await setSurvey(projectId);
const sortFields = [
"sequence", "line", "length", "azimuth", "fsp", "lsp",
@@ -20,9 +20,8 @@ async function list (projectId, opts = {}) {
LIMIT $2;
`;
const res = await pool.query(text, [offset, limit]);
console.log(text, [offset, limit]);
console.log(opts);
const res = await client.query(text, [offset, limit]);
client.release();
return res.rows;
}