Geometric Types in PostgreSQL #2383
Replies: 13 comments 6 replies
-
Object types can be nice, but I prefer the concise way of defining them as tuples. In Payload, we opted to make the type an array with two numbers. We generate types with json-schema-to-typescript and that adds the /**
* @minItems 2
* @maxItems 2
*/
point: [number, number] Excited to hear you will be supporting these! |
Beta Was this translation helpful? Give feedback.
-
Excited to hear this is something you're exploring! We're currently using our own implementation of something like this using import { customType } from 'drizzle-orm/pg-core';
export type Point = {
lat: number;
lng: number;
};
type Options = {
srid: number;
};
export function decodePoint(value: string): Point {
const matches = value.match(/POINT\((?<lng>[\d.-]+) (?<lat>[\d.-]+)\)/);
const { lat, lng } = matches?.groups ?? {};
if (!matches) {
console.warn('Could not parse point value in function pointType.fromDriver', value);
}
return { lat: Number.parseFloat(String(lat)), lng: Number.parseFloat(String(lng)) };
}
export const point = (
name: string,
options?: Options,
) => customType<{ data: Point; driverData: string; }>({
dataType() {
return `geometry(point,${options?.srid ?? 4326})`;
},
toDriver(value: Point) {
return `SRID=4326;POINT(${value.lng} ${value.lat})`;
},
fromDriver(value: string) {
return decodePoint(value);
},
})(name); |
Beta Was this translation helpful? Give feedback.
-
I was working on some generic custom types for |
Beta Was this translation helpful? Give feedback.
-
Hi people, I need to use When I store polygon data as a polygon json and try to retreive it via drizzle I'm getting the So now I'm confronted with these questions:
Can polygons stored as tuples and then queried via drizzle? I feel like there are a couple of ways this could be handled. Ideally I want to leverage drizzle for building queries rather then building my own sql queries. insert: Context // - Northeast (NE) corner:
// - Latitude: 51.5051016
// - Longitude: 6.4081241
// - Southwest (SW) corner:
// - Latitude: 49.4969518
// - Longitude: 2.5240999
interface Viewport {
/** The bounding box of the viewport. */
northeast: {
lat: number;
lng: number;
};
southwest: {
lat: number;
lng: number;
};
} I then map it to a polygon json structure and use it for insertion interface PolygonJSON {
type: 'Polygon';
coordinates: [[number, number][]];
}
/**
* MAP VIEWPORT TO POLYGON
*
* @note examples
* INSERT: ST_GeomFromGeoJSON(${JSON.stringify(polygon)})
* SELECT: ST_AsGeoJSON(polygon)
*/
function mapViewportToPolygon(viewport: Viewport): PolygonJSON {
return {
type: 'Polygon',
coordinates: [
[
[viewport.northeast.lng, viewport.northeast.lat],
[viewport.northeast.lng, viewport.southwest.lat],
[viewport.southwest.lng, viewport.southwest.lat],
[viewport.southwest.lng, viewport.northeast.lat],
[viewport.northeast.lng, viewport.northeast.lat],
],
],
};
} Sorry for the long post but maybe this can shine some clarity onto the way we can manage this type of data via drizzle for others as well. Cheers |
Beta Was this translation helpful? Give feedback.
-
Hi there, just want to add up to @mattiasbonte as I have a project who makes extensive use of Polygons to represent buildings, rooms inside buildings and building plots. We currently use TypeORM to manage the entities with those spatial columns, but it would be nice to migrate to Drizzle. https://typeorm.io/indices#spatial-indices |
Beta Was this translation helpful? Give feedback.
-
hi @AndriiSherman . I use PostGIS a lot, most tables will use some sort of WKB form of geometry, ideally I would like a column type that could cover any and all of the WKT types. https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry |
Beta Was this translation helpful? Give feedback.
-
Same here, we use polygons as well and are having @mattiasbonte's same issue. Just like @vwnd we're still using typeORM when we need to manipulate polygons but it'd be nice to be able to remove TypeORM and completely migrate to Drizzle. If I can be of any assistance for these changes please let me know, I'm more than willing to help. |
Beta Was this translation helpful? Give feedback.
-
I'd also love to see polygons supported in Drizzle. I'm currently trying to get Drizzle working with polygons and I think I'm going to have to settle for raw SQL, which is never ideal. Support for passing WKT directly would also be nice, since I already have a function that spits out WKT (and I'd imagine a lot of other people do too, since this is what libraries like Python's GeoAlchemy use. |
Beta Was this translation helpful? Give feedback.
-
+1 for polygon support! Until support is added, does anyone have a working temp solution for defining a Drizzle schema that can generate the polygon type column in migrations? |
Beta Was this translation helpful? Give feedback.
-
Would also love to see support for polygons and multipolygons. In the meantime I was able to write a custom type:
and use it in my schema like this:
I haven't run it on a real Postgres database yet, but it seems to create the correct migration:
cc @chanmathew since you asked for a workaround |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
+1 For Polygons! |
Beta Was this translation helpful? Give feedback.
-
+1 for (Multi) Polygons with the ability to specify other SRIDs then 4326! |
Beta Was this translation helpful? Give feedback.
-
We are adding a few more types for PostgreSQL and need to get some insights about their production usage and how these types should be mapped in TypeScript. (Want to add it to 0.31.0 release, so we will have a few more indexes use cases supported natively)
For now, we are adding
point
andline
, and we will add other geo types later if users need them.We are considering two modes of mapping for these types:
mode: tuple
In this case, the type for this column will be a tuple:
(1, 2)
You would need to insert a tuple and will get back a tuple from the database.
mode: object
In this case, the type for this column will be an object:
{ x: 1, y: 2 }
for a point and{ a: 1, b: 2, c: 3 }
for a line.You would need to insert an object and will get back an object from the database
If a
line
is represented as two points, then the type will be the same as explained for apoint
:tuple -
[[number, number], [number, number]]
object -
{ point1: { x: number, y: number }, point2: { x: number, y: number } }
So for
line
there should be one more mode:linear_tuple
,linear_object
,points_tuple
,points_object
. Naming here is to be discussedIs there something missing? Is one
mode
enough, like atuple
or anobject
? Or should we have both?Beta Was this translation helpful? Give feedback.
All reactions