Skip to content

Commit

Permalink
optimize query
Browse files Browse the repository at this point in the history
use limit and offset to retrieve a portion of large datasets, value can be set as env variable for customization
  • Loading branch information
doneill committed May 5, 2024
1 parent 3b36b34 commit c7c5ea0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/db/repo/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ class DataRepository {
}
}

async createGeoJson(id, geom, srid, values) {
async createGeoJson(id, geom, srid, values, limit, offset) {
try {
const result = await this.db.oneOrNone(sql.createGeoJson, {
id: id,
geom: geom,
srid: srid,
table: values,
limit: limit,
offset: offset
});

return result;
Expand Down
8 changes: 4 additions & 4 deletions src/db/sql/createGeoJson.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ SELECT jsonb_build_object(
) FROM (
SELECT jsonb_build_object(
'type', 'Feature',
'gid', $[id],
'geometry', ST_AsGeoJSON(ST_Transform($[geom:raw],$[srid:raw]))::jsonb,
'properties', to_jsonb(inputs) - $[geom]
'gid', ${id},
'geometry', ST_AsGeoJSON(ST_Transform(${geom:raw},${srid}))::jsonb,
'properties', to_jsonb(inputs) - ${geom}
) AS feature
FROM (SELECT * FROM $[table:raw]) inputs) features;
FROM (SELECT * FROM ${table:raw} WHERE ${id:raw} IN (SELECT ${id:raw} FROM ${table:raw} ORDER BY ${id:raw} LIMIT ${limit:raw} OFFSET ${offset:raw})) inputs) features;
5 changes: 4 additions & 1 deletion src/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Model {
const table = splitPath[1];

const id = process.env.PG_OBJECTID || 'gid';
const pgLimit = process.env.PG_LIMIT || 10000000;

if (!table)
throw new Error('The "id" parameter must be in the form of "schema.table"');
Expand All @@ -22,8 +23,10 @@ class Model {

const geom = result.f_geometry_column;
const srid = result.srid;
const limit = parseInt(pgLimit);
const offset = 0;

const geojsonResult = await db.data.createGeoJson(id, geom, srid, schema + '.' + table);
const geojsonResult = await db.data.createGeoJson(id, geom, srid, schema + '.' + table, limit, offset);
let geojson = geojsonResult.jsonb_build_object;

geojson.description = 'PG Koop Feature Service';
Expand Down

0 comments on commit c7c5ea0

Please sign in to comment.