Fix PostGIS geom population: use parseGpxAsync + raw SQL

The initial approach (#150) didn't work because:
1. routes.server.ts used parseGpx (sync, needs browser DOMParser)
   instead of parseGpxAsync (uses linkedom on Node.js) — GPX parsing
   silently failed, so stats AND geom were never populated
2. Drizzle's customType doesn't pass SQL expressions through toDriver

Fixes:
- Switch to parseGpxAsync for all server-side GPX parsing
- Use db.execute() with raw ST_GeomFromGeoJSON SQL after insert
- Remove unused lineStringFromCoords helper from schema
- Share setGeomFromGpx between routes and activities
- Add unit tests for GPX→GeoJSON coordinate extraction

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ullrich Schäfer 2026-04-02 17:32:07 +01:00
parent feeca58064
commit 6ed828ef9c
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9
4 changed files with 104 additions and 52 deletions

View file

@ -7,7 +7,6 @@ import {
jsonb,
customType,
} from "drizzle-orm/pg-core";
import { sql, type SQL } from "drizzle-orm";
const bytea = customType<{ data: Buffer }>({
dataType() {
@ -15,27 +14,12 @@ const bytea = customType<{ data: Buffer }>({
},
});
const lineString = customType<{ data: string | SQL }>({
const lineString = customType<{ data: string }>({
dataType() {
return "geometry(LineString, 4326)";
},
toDriver(value) {
return value;
},
});
/**
* Build a SQL expression to create a PostGIS LineString from coordinate pairs.
* Coordinates are [lon, lat] (GeoJSON order).
*/
export function lineStringFromCoords(coords: [number, number][]): SQL {
const geojson = JSON.stringify({
type: "LineString",
coordinates: coords,
});
return sql`ST_GeomFromGeoJSON(${geojson})`;
}
export const journalSchema = pgSchema("journal");
export const users = journalSchema.table("users", {