Skip to content

feat(dashboard): support PostGIS geometry/geography in the database data grid - #5190

Merged
robertkasza merged 14 commits into
nhost:mainfrom
sidgaikwad:fix/dashboard-postgis-data-grid
Nov 25, 2026
Merged

robertkasza merged 14 commits into
nhost:mainfrom
sidgaikwad:fix/dashboard-postgis-data-grid

Conversation

@sidgaikwad

@sidgaikwad sidgaikwad commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Closes #4540

Problem

Geometry columns rendered as [object Object] in the record form and could not be edited. Geography columns showed as hex EWKB.

Approach

As suggested in review, PostGIS converts values in SQL. This is what the Hasura console and the GraphQL API do, so the dashboard no longer guesses formats on the client.

Read

  • fetchTable now selects an explicit column list (formatSelectList). Spatial columns are read as:

    CASE WHEN ST_GeometryType(col::geometry) IN (<curve and surface types>)
         THEN to_json(ST_AsEWKT(col))
         ELSE ST_AsGeoJSON(col, 15, 4)::json
    END
    • 15 digits keeps full precision. Option 4 always includes the CRS, so the SRID survives a round-trip through ST_GeomFromGeoJSON.
    • Curve and surface types (ST_CircularString, ST_CompoundCurve, ST_CurvePolygon, ST_MultiCurve, ST_MultiSurface, ST_PolyhedralSurface, ST_Tin, ST_Triangle) cannot be represented as GeoJSON, so they are read as EWKT. ST_GeometryType is used instead of GeometryType because the latter appends M for measured geometries.
  • Filtering, ordering and limit run in an inner subquery on the raw columns. The conversion only happens in the outer select, so sorting by a spatial column still sorts on the geometry.

  • Without column metadata the select list falls back to *.

Write

  • createRecord and updateRecord send a GeoJSON value (an object, or text starting with {) as ST_GeomFromGeoJSON(value), with ::geography for geography columns.
  • Anything else is sent as a plain literal. PostGIS parses WKT, EWKT and hex EWKB itself, and its errors are shown as they are.
  • The row read back after an update uses the same select list, so the grid shows GeoJSON after saving.

Form and grid

  • Spatial values are pretty-printed as JSON in a multiline input in the record form and in the multiline cell editor in the grid, like json and jsonb.
  • The edit diff moved out of EditRecordForm into getColumnUpdateOptions. Spatial values are read as objects but edited as text, so the text is parsed and compared with the original object. An unchanged geometry is not sent on save.

Compared with the first version of this PR, the client-side validation, the WKT and EWKB patterns and geography-as-hex are gone.

Out of scope: geometry[] arrays and spatial filter operators in the grid.

Testing

  • Vitest covers spatialTypeHelpers, formatSelectList, recordFormValues (including getColumnUpdateOptions), fetchTable, createRecord, updateRecord and DataGridTextCell.
  • 1015 tests in database/dataGrid and the 4 DataGridTextCell tests pass locally.

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Vercel Deployment Info - dashboard

@dbarrosop

Copy link
Copy Markdown
Member

Hello, thanks for the PR! I'm not sure about the approach though. The grid currently reads rows with SELECT * + row_to_json, so the PR compensates on the client side (GeoJSON detection, structural diffing, WKT/EWKB patterns, geography stays hex EWKB). I think we should instead let PostGIS do the conversion in SQL, which is also what the Hasura console and the GraphQL API do (GeoJSON for both geometry and geography).

Proposal:

  • Read (fetchTable.ts): select an explicit column list and wrap spatial columns in ST_AsGeoJSON(col, 15, 4)::json (15 digits = no precision loss, 4 = always include the CRS so the SRID survives a round-trip). GeoJSON can't represent curve types, so fall back to ST_AsEWKT for those.
  • Write (updateRecord.ts / insert): if the value starts with {, send ST_GeomFromGeoJSON(%L) (plus ::geography for geography columns); otherwise send it as a plain literal, since PostGIS already accepts WKT/EWKT/hex EWKB.
  • With that, the client-side format validation and GeoJSON-specific diffing can go; PostGIS errors are clear enough. The [object Object] fix in the form is still needed.
Test schema and data
SET ROLE postgres;
CREATE EXTENSION IF NOT EXISTS postgis;

CREATE SCHEMA IF NOT EXISTS gis;

CREATE TABLE gis.places (
  id          serial PRIMARY KEY,
  name        text NOT NULL,
  description text,
  location    geometry(Point, 4326) NOT NULL,
  entrance    geometry(Point, 4326) DEFAULT ST_SetSRID(ST_MakePoint(0, 0), 4326)
);

CREATE TABLE gis.parcels (
  id       serial PRIMARY KEY,
  label    text,
  boundary geometry(Polygon, 3857)
);

CREATE TABLE gis.shapes (
  id    serial PRIMARY KEY,
  kind  text,
  shape geometry
);

CREATE TABLE gis.peaks (
  id     serial PRIMARY KEY,
  name   text,
  summit geometry(PointZ, 4326)
);

CREATE TABLE gis.airports (
  id       serial PRIMARY KEY,
  code     text,
  location geography(Point, 4326),
  route    geography(LineString, 4326)
);

-- curve types can't be represented as GeoJSON
CREATE TABLE gis.curves (
  id    serial PRIMARY KEY,
  curve geometry
);

INSERT INTO gis.places (name, description, location, entrance) VALUES
  ('Brandenburg Gate', 'Berlin', 'SRID=4326;POINT(13.377704 52.516275)', NULL),
  ('Eiffel Tower', 'Paris', ST_GeomFromGeoJSON('{"type":"Point","coordinates":[2.294481,48.858370]}'), DEFAULT),
  ('Precision test', 'many decimals', 'SRID=4326;POINT(13.404953812345678 52.520006598765432)', NULL);

INSERT INTO gis.parcels (label, boundary) VALUES
  ('Lot A', ST_Transform('SRID=4326;POLYGON((13.37 52.51,13.38 52.51,13.38 52.52,13.37 52.52,13.37 52.51))'::geometry, 3857)),
  ('Lot with hole', ST_Transform('SRID=4326;POLYGON((0 0,10 0,10 10,0 10,0 0),(2 2,4 2,4 4,2 4,2 2))'::geometry, 3857));

INSERT INTO gis.shapes (kind, shape) VALUES
  ('point, no SRID',    'POINT(1 2)'),
  ('linestring',        'SRID=4326;LINESTRING(0 0,1 1,2 1)'),
  ('multipolygon',      'SRID=4326;MULTIPOLYGON(((0 0,1 0,1 1,0 0)),((5 5,6 5,6 6,5 5)))'),
  ('multipoint',        'SRID=4326;MULTIPOINT((0 0),(1 1))'),
  ('collection',        'SRID=4326;GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(0 0,1 1))'),
  ('empty',             'POLYGON EMPTY'),
  ('3D line',           'SRID=4326;LINESTRING Z (0 0 0,1 1 10)'),
  ('measured line (M)', 'SRID=4326;LINESTRING M (0 0 1,1 1 2)'),
  ('null',              NULL);

INSERT INTO gis.peaks (name, summit) VALUES
  ('Zugspitze', 'SRID=4326;POINT Z (10.9863 47.4211 2962)'),
  ('Mont Blanc', 'SRID=4326;POINT Z (6.8652 45.8326 4808)');

INSERT INTO gis.airports (code, location, route) VALUES
  ('BER', 'SRID=4326;POINT(13.5033 52.3667)', 'SRID=4326;LINESTRING(13.5033 52.3667,2.5479 49.0097)'),
  ('CDG', 'SRID=4326;POINT(2.5479 49.0097)', NULL);

INSERT INTO gis.curves (curve) VALUES
  ('CIRCULARSTRING(0 0,1 1,2 0)');

Reading, ST_AsGeoJSON works the same for geometry and geography:

SELECT row_to_json(t)
FROM (
  SELECT id, code,
         ST_AsGeoJSON(location, 15, 4)::json AS location,
         ST_AsGeoJSON(route, 15, 4)::json    AS route
  FROM gis.airports
) t;

-- curves: fall back to EWKT (use GeometryType(col::geometry) for geography columns)
SELECT row_to_json(t)
FROM (
  SELECT id,
         CASE
           WHEN GeometryType(curve) IN ('CIRCULARSTRING', 'COMPOUNDCURVE', 'CURVEPOLYGON', 'MULTICURVE', 'MULTISURFACE')
             THEN to_json(ST_AsEWKT(curve))
           ELSE ST_AsGeoJSON(curve, 15, 4)::json
         END AS curve
  FROM gis.curves
) t;

Writing, GeoJSON goes through ST_GeomFromGeoJSON, anything else as a plain literal:

-- geography: cast the result
UPDATE gis.airports
SET route = ST_GeomFromGeoJSON('{"type":"LineString","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG::4326"}},"coordinates":[[13.5033,52.3667],[11.7861,48.3538]]}')::geography
WHERE id = 1;

-- geometry with a non-4326 SRID: the crs member carries it
INSERT INTO gis.parcels (label, boundary) VALUES (
  'Lot B',
  ST_GeomFromGeoJSON('{"type":"Polygon","crs":{"type":"name","properties":{"name":"EPSG:3857"}},"coordinates":[[[0,0],[100,0],[100,100],[0,100],[0,0]]]}')
);

-- WKT/EWKT (e.g. curves shown as EWKT) as a plain literal
UPDATE gis.curves SET curve = 'CIRCULARSTRING(0 0,1 2,2 0)' WHERE id = 1;

Arrays of spatial types (geometry[]) can be left out of scope for now.

Add isSpatialType, isGeographyType, isGeoJSONGeometry and
getSpatialValueError for the database data grid. They recognise
geometry and geography columns (also when PostGIS lives in its own
schema) and check that a value is in a text format PostGIS accepts:
GeoJSON, WKT/EWKT or hex EWKB for geometry, and WKT/EWKT or hex EWKB for
geography, whose input function does not parse GeoJSON.
Geometry values come back from row_to_json as GeoJSON objects, so the
record form rendered them as "[object Object]". The form now:

- pretty-prints GeoJSON for geometry columns, as it does for json/jsonb,
  and uses a multiline input for geometry and geography columns
- validates values with getSpatialValueError and explains that geography
  columns need WKT/EWKT or hex EWKB because PostGIS does not parse GeoJSON
  for geography
- sends edited GeoJSON as text and skips columns whose GeoJSON did not
  change, so saving a row no longer rewrites untouched geometry values

The edit diff moves from EditRecordForm into getColumnUpdateOptions so it
can be unit tested.
Geometry and geography cells now open the multiline editor used for
json/jsonb, so GeoJSON and longer WKT values can be edited inline in the
data grid instead of in a single-line input.

Closes nhost#4540
Select columns explicitly and read geometry and geography as GeoJSON with ST_AsGeoJSON(col, 15, 4), falling back to EWKT for the types GeoJSON cannot represent. Write GeoJSON through ST_GeomFromGeoJSON, cast for geography, and send WKT/EWKT/hex EWKB as plain literals. Drop the client-side spatial validation and GeoJSON diffing.
@sidgaikwad
sidgaikwad force-pushed the fix/dashboard-postgis-data-grid branch from 8e6c1e4 to 6972fc0 Compare October 30, 2026 16:25
@sidgaikwad

sidgaikwad commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the proposal and the test schema. I reworked the branch to match it.

Spatial columns are now read through ST_AsGeoJSON(col, 15, 4)::json, with ST_AsEWKT as the fallback for curve and surface types. Filtering and ordering stay on the raw columns in an inner subquery. On write, GeoJSON goes through ST_GeomFromGeoJSON, cast to geography where needed, and WKT, EWKT and hex EWKB are sent as plain literals.

The client-side validation and GeoJSON diffing are gone. The [object Object] fix stays, and geography now shows as GeoJSON too.

One deviation: the CASE uses ST_GeometryType instead of GeometryType, since the latter appends M for measured geometries.

The PR description is updated. The checks on the new head need a safe_to_test approval.

@dbarrosop

dbarrosop commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Thanks, and sorry for the delay, we will be reviewing it soon.

@github-actions

Copy link
Copy Markdown
Contributor

@github-actions

Copy link
Copy Markdown
Contributor

@github-actions

Copy link
Copy Markdown
Contributor

@robertkasza
robertkasza disabled auto-merge November 25, 2026 07:57
@robertkasza
robertkasza merged commit 20c1255 into nhost:main Nov 25, 2026
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(dashboard): support PostGIS geometry/geography and GeoJSON data types in Database DataGrid

3 participants