-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try to run migration from introspection
- Loading branch information
1 parent
e4a4110
commit f7226d1
Showing
16 changed files
with
219 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { SimpleFeature } from "./types"; | ||
import { customType } from "."; | ||
import { | ||
Geometry, | ||
GeometryCollection, | ||
LineString, | ||
MultiLineString, | ||
MultiPoint, | ||
MultiPolygon, | ||
Point, | ||
Polygon, | ||
} from "geojson"; | ||
|
||
export const geography = | ||
<T extends Geometry>(sf: SimpleFeature) => | ||
(name: string, srid = 4326) => | ||
customType<{ data: T }>({ | ||
dataType() { | ||
return `geography(${sf}, ${srid})`; | ||
}, | ||
})(name); | ||
|
||
export const pointGeog = geography<Point>(SimpleFeature.POINT); | ||
|
||
export const multiPointGeog = geography<MultiPoint>(SimpleFeature.MULTI_POINT); | ||
|
||
export const lineStringGeog = geography<LineString>(SimpleFeature.LINE_STRING); | ||
|
||
export const multiLineStringGeog = geography<MultiLineString>( | ||
SimpleFeature.MULTI_LINE_STRING, | ||
); | ||
|
||
export const polygonGeog = geography<Polygon>(SimpleFeature.POLYGON); | ||
|
||
export const multiPolygonGeog = geography<MultiPolygon>( | ||
SimpleFeature.MULTI_POLYGON, | ||
); | ||
|
||
export const geometryCollectionGeog = geography<GeometryCollection>( | ||
SimpleFeature.GEOMETRY_COLLECTION, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { customType } from "."; | ||
import { | ||
Geometry, | ||
Point, | ||
LineString, | ||
MultiLineString, | ||
GeometryCollection, | ||
MultiPolygon, | ||
Polygon, | ||
MultiPoint, | ||
} from "geojson"; | ||
import { SimpleFeature } from "./types"; | ||
|
||
export const geometry = | ||
<T extends Geometry>(sf: SimpleFeature) => | ||
(name: string, srid = 3857) => | ||
customType<{ data: T }>({ | ||
dataType() { | ||
return `geometry(${sf},${srid})`; | ||
}, | ||
})(name); | ||
|
||
export const pointGeom = geometry<Point>(SimpleFeature.POINT); | ||
|
||
export const multiPointGeom = geometry<MultiPoint>(SimpleFeature.MULTI_POINT); | ||
|
||
export const lineStringGeom = geometry<LineString>(SimpleFeature.LINE_STRING); | ||
|
||
export const multiLineStringGeom = geometry<MultiLineString>( | ||
SimpleFeature.MULTI_LINE_STRING, | ||
); | ||
|
||
export const polygonGeom = geometry<Polygon>(SimpleFeature.POLYGON); | ||
|
||
export const multiPolygonGeom = geometry<MultiPolygon>( | ||
SimpleFeature.MULTI_POLYGON, | ||
); | ||
|
||
export const geometryCollectionGeom = geometry<GeometryCollection>( | ||
SimpleFeature.GEOMETRY_COLLECTION, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export { customType } from "drizzle-orm/pg-core"; | ||
|
||
export { | ||
geometry, | ||
pointGeom, | ||
multiPointGeom, | ||
lineStringGeom, | ||
multiLineStringGeom, | ||
polygonGeom, | ||
multiPolygonGeom, | ||
geometryCollectionGeom, | ||
} from "./geometry"; | ||
|
||
export { | ||
geography, | ||
pointGeog, | ||
multiPointGeog, | ||
lineStringGeog, | ||
multiLineStringGeog, | ||
polygonGeog, | ||
multiPolygonGeog, | ||
geometryCollectionGeog, | ||
} from "./geography"; | ||
|
||
export { ST_AsGeoJSON } from "./spatial-type"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
## Overview | ||
Custom types for postgis | ||
|
||
## Context for terms | ||
Geometry has slightly different meanings between the broader mapping community and the specific PostGIS typing system. The geojson specification uses Geometry to refer to any coordinates, regardless of whether those coordinates model the Earth as an ellipsoid or a plane. | ||
|
||
In contrast, PostGIS uses Geometry to refer specifically to coordinates that model the Earth as a plane. It uses Geography to refer specifically to coordinates that model the Earth as an ellipsoid. | ||
|
||
## Guidelines for using the types | ||
Within PostGIS, projected coordinate systems typically should be stored in Geometry columns. Conversely, geodetic coordinate systems should be stored in Geography columns. | ||
|
||
Following this guidance, WGS84 (EPSG:4326) should be stored in a Geography column. | ||
Its projected counterpart, Pseudo-Mercator (EPSG:3857), should be stored in a Geometry Column. This package encourages this convention by setting the default srid for geomtry types to 3857 and geography types to 4326. | ||
|
||
## WGS84 is special | ||
The Geometry type predates the Geography type in PostGIS. It also evolved with GeoJSON. GeoJSON uses WSG84 as the default spatial reference system for sharing coordinates. Consequently, the Geometry type historically supported WSG84 by applying [Plate Carée](https://en.wikipedia.org/wiki/Equirectangular_projection) projection. | ||
|
||
Converting from GeoJSON to Geometry types is also easier, thanks to the `ST_GeomFromGeoJSON` PostGIS function. There exists no equivalent function for converting directly from GeoJSON to Geography. Fortunately, the same effect can be achieved by casting the geometry to a geography: | ||
``` | ||
ST_GeomFromGeoJSON(geojson_feature)::geography | ||
``` | ||
|
||
## WGS84 isn't special | ||
Despite Geometry's historical support for WSG84, it is generally safer to move coordinates in this system to a Geography column. Spatial functions for Geometry types assume a flat surface for its calculations. PostGIS will not check whether the coordinates actually satisfy this assumption. Consequently, applying geometry functions to WGS84 coordinates will return meaningless results and PostGIS will fail to emit errors to flag these issues. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { PgColumn } from "drizzle-orm/pg-core"; | ||
import { Geometry } from "geojson"; | ||
import { sql } from "drizzle-orm"; | ||
|
||
/** | ||
* https://postgis.net/docs/ST_AsGeoJSON.html | ||
* @returns a geometry or geography as a GeoJSON "geometry" | ||
*/ | ||
export const ST_AsGeoJSON = ( | ||
feature: PgColumn<{ | ||
name: string; | ||
tableName: string; | ||
dataType: "custom"; | ||
columnType: "PgCustomColumn"; | ||
data: Geometry; | ||
driverParam: unknown; | ||
notNull: false; | ||
hasDefault: false; | ||
enumValues: undefined; | ||
baseColumn: never; | ||
}>, | ||
maxDecimalDigits = 9, | ||
) => sql<string>`ST_AsGeoJSON(${feature}, ${maxDecimalDigits})`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export enum SimpleFeature { | ||
POINT = "point", | ||
MULTI_POINT = "multiPoint", | ||
LINE_STRING = "lineString", | ||
MULTI_LINE_STRING = "multiLineString", | ||
POLYGON = "polygon", | ||
MULTI_POLYGON = "multiPolygon", | ||
GEOMETRY_COLLECTION = "geometryCollection", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Drizzle kit configuration | ||
import type { Config } from "drizzle-kit"; | ||
|
||
export default { | ||
schema: "./drizzle/migration/schema.ts", | ||
dialect: "postgresql", | ||
out: "./drizzle/migration", | ||
dbCredentials: { | ||
host: process.env.FLOW_DATABASE_HOST!, | ||
port: parseInt(process.env.FLOW_DATABASE_PORT!), | ||
user: process.env.FLOW_DATABASE_USER, | ||
password: process.env.FLOW_DATABASE_PASSWORD, | ||
database: process.env.FLOW_DATABASE_NAME!, | ||
ssl: process.env.FLOW_DATABASE_ENV !== "development" && { | ||
rejectUnauthorized: false, | ||
}, | ||
}, | ||
} satisfies Config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.