-
-
Notifications
You must be signed in to change notification settings - Fork 496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
It's possible use native data transformation features of the DB? #735
Comments
So I guess we need something like this: |
Would it be possible to access convertToDatabaseValue(value: any, knex: Knex): any {
return knex.raw("ST_GeomFromGeoJSON(?, 1, 4326)", [JSON.stringify(geometry || null)]);
} Or it might be better to modify the initialization of @singleton()
export default class OrmClient {
private mikroOrm: MikroORM<AbstractSqlDriver>;
private postgis: knexPostgis;
public constructor() {
this.mikroOrm = new MikroORM({
type: "mysql",
clientUrl: process.env.DATABASE_URL,
entities: ["dist/entities/*.js"],
entitiesTs: ["src/entities/*.ts"],
autoJoinOneToOneOwner: false,
forceUtcTimezone: true,
});
this.postgis = knexPostgis(this.mikroOrm.getKnexQB());
}
public async initialize(): Promise<void> {
await MikroORM.init(this.mikroOrm);
}
public get st(): knexPostgis {
return this.postgis;
}
} export default PointType extends Type {
private st: knexPostgis;
public constructor() {
super();
this.st = container.resolve(OrmClient).st;
}
convertToDatabaseValue(value: any): any {
return this.st.geomFromGeoJSON(value || null);
}
} |
@B4nan running into a similar issue myself. It's easy enough to implement convertToJSValue(value, platform) {
if (typeof value === 'string') {
const hexAry = wkb.match(/.{2}/g)
const intAry = []
for (const i in hexAry) {
intAry.push(parseInt(hexAry[i], 16))
}
const buf = Buffer.from(intAry)
const geom = wkx.Geometry.parse(buf)
return {
type: 'Point',
coordinates: [geom.x, geom.y],
}
}
return value
} vs just |
While I'm patiently waiting for native SQL selectors here's my not-so-cumbersome wkx implementation of a Point custom type: import { Type } from '@mikro-orm/core';
import { Point } from 'geojson';
import wkx from 'wkx';
export class GeometryType extends Type<Point, string> {
convertToDatabaseValue(value: Point): string {
return wkx.Geometry.parseGeoJSON(value).toWkb().toString('hex');
}
convertToJSValue(value: string): Point {
return wkx.Geometry.parse(Buffer.from(value, 'hex')).toGeoJSON() as Point;
}
} I'm using it for a @Property({
type: GeometryType,
columnType: 'geometry',
})
geometry: Point; |
Adds two methods to the custom type interface that allows to adjust the SQL fragment used to select or update the value. Example usage: ```ts export class PointType extends Type<Point, string> { convertToDatabaseValue(value: Point): string { return `point(${value.latitude} ${value.longitude})`; } convertToJSValue(value: string): Point { const m = value.match(/point\((\d+(\.\d+)?) (\d+(\.\d+)?)\)/i); return new Point(+m[1], +m[3]); } convertToJSValueSQL(key: string) { return `ST_AsText(${key})`; } convertToDatabaseValueSQL(key: string) { return `ST_PointFromText(${key})`; } getColumnType(): string { return 'point'; } } ``` Closes #735
Adds two methods to the custom type interface that allows to adjust the SQL fragment used to select or update the value. Example usage: ```ts export class PointType extends Type<Point, string> { convertToDatabaseValue(value: Point): string { return `point(${value.latitude} ${value.longitude})`; } convertToJSValue(value: string): Point { const m = value.match(/point\((\d+(\.\d+)?) (\d+(\.\d+)?)\)/i); return new Point(+m[1], +m[3]); } convertToJSValueSQL(key: string) { return `ST_AsText(${key})`; } convertToDatabaseValueSQL(key: string) { return `ST_PointFromText(${key})`; } getColumnType(): string { return 'point'; } } ``` Closes #735
Adds two methods to the custom type interface that allows to adjust the SQL fragment used to select or update the value. Example usage: ```ts export class PointType extends Type<Point, string> { convertToDatabaseValue(value: Point): string { return `point(${value.latitude} ${value.longitude})`; } convertToJSValue(value: string): Point { const m = value.match(/point\((\d+(\.\d+)?) (\d+(\.\d+)?)\)/i); return new Point(+m[1], +m[3]); } convertToJSValueSQL(key: string) { return `ST_AsText(${key})`; } convertToDatabaseValueSQL(key: string) { return `ST_PointFromText(${key})`; } getColumnType(): string { return 'point'; } } ``` Closes #735
Is your feature request related to a problem? Please describe.
Working with spatial DB features it's possible save and load data of the DB in several formats, in my case I am interesting in use
ST_GeomFromGeoJSON
andST_AsGeoJson
. So that I can do something as the next and get theposition
data as GeoJSON:Describe the solution you'd like
Some way to be able to use a function from the DB when saving or getting data from the DB.
Describe alternatives you've considered
Use a "custom type", but if I understand correctly, then I will need handle it manually using JS. Maybe can work to save data, using something as (I have to try it):
But I can't think of what to do when load data in a query.
The text was updated successfully, but these errors were encountered: