Created
January 17, 2024 17:57
-
-
Save vaso991/5459150d0bbf1fd229bd3f3caba87730 to your computer and use it in GitHub Desktop.
MikroORM Postgis
This file contains hidden or 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
export class Point { | |
constructor(lng: number, lat: number) { | |
this.lng = lng; | |
this.lat = lat; | |
} | |
lng!: number; | |
lat!: number; | |
} | |
export class PostGISGeometryType extends Type<Point | undefined, string> { | |
convertToJSValue(value: string, _platform: Platform): Point | undefined { | |
const m = value?.match(/point\((-?\d+(\.\d+)?) (-?\d+(\.\d+)?)\)/i); | |
if (!m) { | |
return undefined; | |
} | |
return new Point(+m[1], +m[3]); | |
} | |
convertToJSValueSQL(key: string, platform: Platform): string { | |
return `ST_AsText(${key})`; | |
} | |
convertToDatabaseValue(value: Point, _platform: Platform): string { | |
return `POINT(${value.lng} ${value.lat})`; | |
} | |
convertToDatabaseValueSQL(value: string, platform: Platform): string { | |
return `ST_SetSRID(ST_GeomFromText(${value}),4326)`; | |
} | |
getColumnType(_prop: EntityProperty, _platform: Platform): string { | |
return 'GEOMETRY(Point,4326)'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment